SetWindowsHookEx(WH_KEYBOARD) - 64-bit and 32-bit hooks don't work simultaneously

Mikola Akbal̆ 75 Reputation points
2023-03-04T05:09:27.96+00:00

Hi! I try to install hook with SetWindowsHookEx(WH_KEYBOARD), and I see that 32-bit hook and 64-bit hook can not work simultaneously. If I install both, only 64-bit hook works, 32-bit doesn't.

I solved this problem by algorithm that chooses appropriate bitness depending on bitness of process that owns foreground window. One of hooks I turn off, and another I turn on. At the moment, only 32-bit hook is installed, or only 64-bit hook is installed. That solves, but it's difficult.

Documentation of SetWindowsHookEx() says that both - 32-bit and 64-bit - can work simultaneously. Did I understand it correctly?

https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexw To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version.

So, why doesn't it work?

Windows development | Windows API - Win32
{count} votes

Accepted answer
  1. Xiaopo Yang - MSFT 12,731 Reputation points Microsoft External Staff
    2023-04-18T06:39:50.41+00:00

    After discussing, it’s better to use with specific threads instead of all threads(global). There is no evidence that WH_KEYBOARD has an issue. If the issue has a big impact on you, please open an incident.

    As this issue is complex, please open an incident via 'Contact us' tab at link below so that our engineer can work with you closely: https://developer.microsoft.com/en-us/windows/support/ and please choose the 'Technical Support - Coding/Debugging' for Windows SDK for this issue.

    In-addition, if the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RLWA32 49,666 Reputation points
    2023-03-06T12:15:47.9766667+00:00

    You may have better success using a WH_GETMESSAGE hook to handle WM_KEYDOWN and WM_KEYUP. This hook is allowed to change the message it is passed so you can set it to WM_NULL to prevent subsequent processing of the keyboard input. In my tests both 32-bit and 64-bit hooks were handled in the context of a hooked process of like bitness. Neither hook was called in the context of the installing thread.

    2 people found this answer helpful.

  2. Mikola Akbal̆ 75 Reputation points
    2023-03-08T08:06:44.32+00:00

    At the moment, there is no another solution that I found by myself. WH_KEYBOARD, really, doesn't work with both bitnesses simultaneously. Microsoft, please, solve this problem.

    The solution is switching of bitnesses as described in discussing. For example, it can be monitoring of foreground window. When you know bitness of this process, you unhook one bitness and hook another. That works.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.