I’ve written this C++ program using VS2010 to detect when the user is making a double middle mouse button click, switch to the next window (as Alt+Tab does) and stop the hook chain. Here’s my code:
DLL:
extern "C"__declspec (dllexport)
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode<0)
return CallNextHookEx(NULL,nCode,wParam,lParam);
if (wParam == WM_MBUTTONDBLCLK)
{
PostMessage(GetActiveWindow(),WM_SYSCOMMAND,SC_NEXTWINDOW,0);
}
else
return CallNextHookEx(NULL,nCode,wParam,lParam);
// if EVERYTHING_IS_OK
return TRUE;
}
EXE:
do{
nMenu = choose();
switch (nMenu)
{
case 1:
hLib = LoadLibrary(cLibName);
hProc = (HOOKPROC) GetProcAddress(hLib, "HookProc");
hHook = SetWindowsHookEx(WH_MOUSE, hProc, hLib, NULL);
break;
case 2:
UnhookWindowsHookEx(hHook);
break;
case 0:
;
}
} while (nMenu);
I launch the program and set the hook, then the program hangs and mouse stops working in most apps (keeps working in Chrome though). What am I doing wrong?
Hooks are evil if they are not used as notifications only. The MSDN highly recommends calling
CallNextHookEx()for a reason. You cannot know at which point in the calling chain you are. This makes your code fragile even though it might appear to work at first.You depend on other users of the hook API to be nice to you, i.e. to call you. If someone (like you) does not do it, your code breaks.
Without going into further detail where your code breaks, I think that it is not very surprising that
if you shut up the other users of the mouse hook by returning
TRUE, is it?