c++ 键盘钩子向主程序发送消息的问题 20
我写了一个简单的键盘钩子,为什么当我在keyboardProc里访问hHook和hMain都是0?我不会C++,代码是查了很多资料以后写出来的,难道是全局变量定义有问题?...
我写了一个简单的键盘钩子,为什么当我在keyboardProc里访问hHook和hMain都是0?我不会C++,代码是查了很多资料以后写出来的,难道是全局变量定义有问题?
HMODULE hMod;
HHOOK hHook;
HWND hMain;
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(nCode <= 0)
{
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
SendMessage(hMain, WM_USER + 1, wParam, lParam);
char buffer[256];
itoa((int)hMain,buffer,10);
MessageBoxA(NULL,buffer,"",0);
char buffer1[256];
itoa((int)hHook,buffer1,10);
MessageBoxA(NULL,buffer1,"",0);
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
int SetHook(HWND hMainForm, DWORD dwThreadId)
{
hMain = hMainForm;
hHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hMod,dwThreadId);
if(hHook == 0)
{
return GetLastError();
}
return 0;
} 展开
HMODULE hMod;
HHOOK hHook;
HWND hMain;
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if(nCode <= 0)
{
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
SendMessage(hMain, WM_USER + 1, wParam, lParam);
char buffer[256];
itoa((int)hMain,buffer,10);
MessageBoxA(NULL,buffer,"",0);
char buffer1[256];
itoa((int)hHook,buffer1,10);
MessageBoxA(NULL,buffer1,"",0);
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
int SetHook(HWND hMainForm, DWORD dwThreadId)
{
hMain = hMainForm;
hHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,hMod,dwThreadId);
if(hHook == 0)
{
return GetLastError();
}
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询