问题
I have hooked WM_SETFOCUS message by calling API
hhookCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, HookCallWndProc, hInst, threadID);
Hook Procedure is
extern "C" LRESULT _declspec(dllexport) __stdcall CALLBACK HookCallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HC_ACTION) {
CWPSTRUCT* info = (CWPSTRUCT*) lParam;
if(info->message == WM_SETFOCUS )
{
if(info->hwnd == hControl)
{
MessageBox(NULL,L"Focus on control",L"Focus",MB_OK);
}
}
}
return CallNextHookEx(hhookCallWndProc , nCode, wParam, lParam);
}
Now when I focus on the control, this hook procedure is getting called . MessageBox is shown. But as soon as I click on Ok , another message pops up.
Messages keep on popping up infinitely. I want to get messagebox only once whenever I focus on control, but here I am getting messages infinitely.
Anything I am doing wrong.
回答1:
Quick guess - doesn't closing a message box force a re-focus of the control and therefore call your function again?
回答2:
I think the problem is that with the message box you take away the focus and when clicking ok you give back the focus to the control, so your hook is called again. I would recommend to try print out something using the OutputDebugString instead of using a message box.
回答3:
Whenever debugging your application better create log file to save the information. You have commented that keyboard event is tracked more than four times.
- This is mainly depends on Where u r using the SetWindowsHookEx() function.
- you can eradicate this problem by properly uninstall the hook function.
来源:https://stackoverflow.com/questions/2144805/after-hooking-hook-procedure-is-called-infinitely