title: Global hook in DLL loaded in python renders firefox unresponsive, but only if the bitness doesn't match
body: <p>I am writing a program to control a robotic sample changer, which uses a barcode scanner to check which sample was loaded. The scanner is read as a keyboard, and I want to be able to capture its input and prevent it from showing up as keystrokes in other programs. I used <a href="https://www.codeproject.com/Articles/716591/Combining-Raw-Input-and-keyboard-Hook-to-selective" rel="nofollow noreferrer">this solution</a>, using ctypes to create the raw input half and c++ to code the hook in a dll so it can be global. It works, but I noticed it was making Firefox and MobaXterm unresponsive. Investigating further, I saw that running 64 bit python only worked with 64 bit Firefox, and 32 bit python only works with 32 bit Firefox. I've isolated it to the dll itself, the rest of the program running without it causes no issues. Code for the dll:</p>
  <pre class="lang-cpp prettyprint-override"><code>// dllmain.cpp : Defines the entry point for the DLL application.
  #include &quot;pch.h&quot;
  #include &quot;myhook.h&quot;
  
  #pragma data_seg(&quot;.KBH&quot;)
  HWND hWndServer = NULL;
  #pragma data_seg()
  #pragma comment(linker, &quot;/section:.KBH,rws&quot;)
  
  HINSTANCE hInstance;
  UINT UWM_KBHOOK;
  HHOOK hook;
  
  static LRESULT CALLBACK msghook(int nCode, WPARAM wParam, LPARAM lParam);
  
  BOOL APIENTRY DllMain( HMODULE hModule,
                         DWORD  ul_reason_for_call,
                         LPVOID lpReserved
                       )
  {
      switch (ul_reason_for_call)
      {
      case DLL_PROCESS_ATTACH:
          hInstance = (HINSTANCE)hModule;
          UWM_KBHOOK = RegisterWindowMessage(UWM_KBHOOK_MSG);
          return TRUE;
      case DLL_PROCESS_DETACH:
          if (hWndServer != NULL)
              clearMyHook(hWndServer);
          return TRUE;
      case DLL_THREAD_ATTACH:
      case DLL_THREAD_DETACH:
          break;
      }
      return TRUE;
  }
  
  extern &quot;C&quot; KEYHOOKDLL_API BOOL WINAPI setMyHook(HWND hWnd) {
      if (hWndServer != NULL)
          return FALSE;
  
      hook = SetWindowsHookEx(
          WH_KEYBOARD,
          (HOOKPROC)msghook,
          hInstance,
          0
      );
  
      if (hook != NULL) {
          hWndServer = hWnd;
          return TRUE;
      }
      return FALSE;
  }
  
  extern &quot;C&quot; KEYHOOKDLL_API BOOL clearMyHook(HWND hWnd) {
      if (hWnd != hWndServer)
          return FALSE;
      BOOL unhooked = UnhookWindowsHookEx(hook);
      if (unhooked)
          hWndServer = NULL;
      return unhooked;
  }
  
  static LRESULT CALLBACK msghook(int nCode, WPARAM wParam, LPARAM lParam) {
      if (SendMessage(hWndServer, UWM_KBHOOK, wParam, lParam)) 
          return 1;
      return CallNextHookEx(hook, nCode, wParam, lParam);
  }
  
  extern &quot;C&quot; KEYHOOKDLL_API LPCSTR message_string() {
      return (LPCSTR)UWM_KBHOOK_MSG;
  }
  </code></pre>
  <p>And the header file:</p>
  <pre class="lang-cpp prettyprint-override"><code>#pragma once
  #include &lt;Windows.h&gt;
  #include &lt;tchar.h&gt;
  
  #ifdef KEYHOOKDLL_EXPORTS
  #define KEYHOOKDLL_API __declspec(dllexport)
  #else
  #define KEYHOOKDLL_API __declspec(dllimport)
  #endif
  
  #define UWM_KBHOOK_MSG _T(&quot;UMW_KBHOOK-{B30856F0-D3DD-11d4-A00B-006067718D04}&quot;)
  
  extern &quot;C&quot; KEYHOOKDLL_API BOOL WINAPI setMyHook(HWND hWnd);
  
  extern &quot;C&quot; KEYHOOKDLL_API BOOL clearMyHook(HWND hWnd);
  
  extern &quot;C&quot; KEYHOOKDLL_API LPCSTR message_string();
  </code></pre>
  <p>I have no idea what is happening here. I'm guessing the global hook is somehow interfering with hooks in Firefox when the bitness doesn't match, is this a known problem? Is there anything I am doing here that might be messing with the hook chain? I noticed typing in a Firefox browser window sends out a double message for some reason.</p>