#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);
}
I create thread with _beginthread. This thread simulate mouse’s moves and call Sleep(2000). I can’t move mouse after simulating moves in thread. But if I comment this lines in void thread(void *param)
UnhookWindowsHookEx(MouseHook);
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL);`
program will work right, without mouse ‘blocking’. Can everybody explain reason for it.
A thread that calls SetWindowsHookEx() to set a low-level mouse hook must pump a message loop. Required so that Windows can call the callback you registered so it can notify you of the mouse message. Which is your problem, the thread you started sets a hook but doesn’t pump a message loop.
Windows has protection against misbehaving programs like this, it automatically destroys the hook when it has to wait too long to make the callback. But your thread sleeps for 2 seconds, not long enough to trigger the timeout. You then call UnhookWindowsHookEx() which unblocks Windows. But immediately hook again.
There’s just no point in calling SetWindowsHookEx() in that thread. Just remove it and the hook that you have set in the main thread will run as normal. Hard to see the point of that thread in general btw.