title: Setting WH_KEYBOARD hook fails with error 1428
body: <p>This is my C++ code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  
  HHOOK hook;
  
  LRESULT CALLBACK keyboardHook(int nCode, WPARAM wParam, LPARAM lParam)
  {
      std::cout &lt;&lt; "Hook callback" &lt;&lt; std::endl;
      return CallNextHookEx(hook, nCode, wParam, lParam);
  }
  
  int main(int argc, char **argv)
  {
      hook = SetWindowsHookEx(WH_KEYBOARD, keyboardHook, NULL, NULL);
      if (hook == NULL) {
          std::cout &lt;&lt; "Error " &lt;&lt; GetLastError() &lt;&lt; std::endl;
          return 1;
      }
      std::cout &lt;&lt; "Hook set" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>I compile it using Visual Studio like this:</p>
  
  <pre><code>cl /D_WIN32_WINNT=0x0401 foo.cc /link user32.lib
  </code></pre>
  
  <p>When I run it, I get error 1428.</p>
  
  <pre><code>C:\&gt;foo.exe
  Error 1428
  </code></pre>
  
  <p>This is the meaning of error 1428.</p>
  
  <pre><code>C:\nocaps&gt;net helpmsg 1428
  
  Cannot set nonlocal hook without a module handle.
  </code></pre>
  
  <p>Could you please help me to understand this error and get this code working? It would be great if you could provide me a working code that works and invokes the callback?</p>
  
  <p>I see that if I use WH_KEYBOARD_LL hook instead, it works fine. But I need to understand how WH_KEYBOARD hook can be made to work.</p>