开发者

After hooking hook procedure is called infinitely

开发者 https://www.devze.com 2022-12-18 00:40 出处:网络
I have hooked WM_SETFOCUS message by calling API hhookCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, HookCallWndProc, hInst, threadID);

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)
     开发者_StackOverflow       {
                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.


Quick guess - doesn't closing a message box force a re-focus of the control and therefore call your function again?


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号