钩子和线程,鼠标阻塞
Posted
技术标签:
【中文标题】钩子和线程,鼠标阻塞【英文标题】:hooks and threads, mouse blocking 【发布时间】:2012-01-24 09:36:51 【问题描述】:#include <windows.h>
#include <process.h>
HWND MainHwnd;
HHOOK MouseHook;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
static wchar_t szAppName[]=L"hooks";
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
MainHwnd=hwnd=CreateWindow(szAppName,L"hooks",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_MAXIMIZE);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
TranslateMessage(&msg);
DispatchMessage(&msg);
return msg.wParam;
LRESULT CALLBACK LowLevelMouseProc(int nCode,WPARAM wParam,LPARAM lParam)
if (nCode==HC_ACTION)
((LPMSLLHOOKSTRUCT)lParam)->flags=0;
return CallNextHookEx(NULL,nCode,wParam,lParam);
void thread(void *param)
for (int i=0;i<3;i++)
UnhookWindowsHookEx(MouseHook);
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL);
for (int j=0;j<100;j++)
mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE, (j)*(65535.0/500),(j)*(65535.0/500),0,0);
Sleep(10);
Sleep(2000);
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
HDC hdc;
PAINTSTRUCT ps;
switch(iMsg)
case WM_CREATE:
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),NULL);
_beginthread(thread,0,0);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
UnhookWindowsHookEx(MouseHook);
PostQuitMessage(0);
return 0;
return DefWindowProc(hwnd,iMsg,wParam,lParam);
我用_beginthread
创建线程。该线程模拟鼠标的移动并调用Sleep(2000)
。模拟线程中的移动后我无法移动鼠标。但如果我在void thread(void *param)
中评论这行代码
UnhookWindowsHookEx(MouseHook);
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL);`
程序将正常运行,没有鼠标“阻塞”。大家能解释一下原因吗。
【问题讨论】:
【参考方案1】:调用 SetWindowsHookEx() 来设置低级鼠标钩子的线程必须泵送消息循环。必需,以便 Windows 可以调用您注册的回调,以便通知您鼠标消息。哪个是你的问题,你启动的线程设置了一个钩子,但没有发送消息循环。
Windows 可以防止此类行为不端的程序,当它必须等待太长时间才能进行回调时,它会自动销毁挂钩。但是您的线程休眠了 2 秒,不足以触发超时。然后调用 UnhookWindowsHookEx() 来解除对 Windows 的阻塞。但马上又挂了。
在该线程中调用 SetWindowsHookEx() 毫无意义。只需将其删除,您在主线程中设置的钩子就会正常运行。顺便说一句,很难看出该线程的意义。
【讨论】:
以上是关于钩子和线程,鼠标阻塞的主要内容,如果未能解决你的问题,请参考以下文章