title: SetWindowsHookEx fails with error 126
body: <p>I'm trying to use the <a href="http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C">Gma.UserActivityMonitor</a> library in a project and I've faced an error I can not overcome on my own.</p>
  
  <p>In the <code>HookManager.Callbacks.cs</code> file there's a static method called <code>EnsureSubscribedToGlobalMouseEvents</code> with the following code (more or less):</p>
  
  <pre><code>var asm = Assembly.GetExecutingAssembly().GetModules()[0];
  var mar = Marshal.GetHINSTANCE(asm);
  s_MouseHookHandle = SetWindowsHookEx(
      WH_MOUSE_LL,
      s_MouseDelegate,
      mar,
      0);
  //If SetWindowsHookEx fails.
  if (s_MouseHookHandle == 0)
  {
      //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
      int errorCode = Marshal.GetLastWin32Error();
      //do cleanup
  
      //Initializes and throws a new instance of the Win32Exception class with the specified error. 
      throw new Win32Exception(errorCode);
  }
  </code></pre>
  
  <p>The <code>SetWindowsHookEx</code> always returns <code>0</code> and the above code keeps throwing an exception with message <code>The specified module could not be found</code> and the call to <code>Marshal.GetLastWin32Error</code> returns code <code>126</code>. I can successfully run the demo provided with Gma.UserActivityMonitor's original project but since my project is a little too complicated to explain here I can not go into detail explaining its difference with mine. I'm just hoping someone can blind guess the problem.</p>
  
  <p>BTW, in the project's FAQ it's said that others have a problem close to mine (with <code>SetWindowsHookEx</code> returning error) when the <code>Enable Visual Studio hosting process</code> is checked only when the project is debugged. So I unchecked that box in mine and still I'm having the same problem, and not just in debugging mode but also when I double click the release file in Windows Explorer (no Visual Studio involved).</p>
  
  <p>To give more information, in demo project (which works fine) the <code>asm</code> variable points to <code>{Gma.UserActivityMonitor.dll}</code> and the same in my project which the exception is thrown!</p>