title: How to port the C++ SetWindowsHookEx to C# or is this not possible?
body: <p>I was looking at the example in <a href="http://support.microsoft.com/kb/318804" rel="nofollow">Microsoft KB318804</a> but they use the threadId of the "current" application!!!  I have some C++ code which works but we have to rewrite it, and I would prefer to re-write in C# while I am in there.  The one thing it does is get the threadId of the target application like so:</p>
  
  <pre><code>uint lastId = GetWindowThreadProcessId(targetHandle, IntPtr.Zero);
  </code></pre>
  
  <p><strong>no, GetCurrentThread is not the correct call as I am getting the thread id of a remote application which is what we do today AND what we want to do.  targetHandle is a handle to that remote application.</strong></p>
  
  <p>I cast this lastId to an int and tried to wire up C# code but the SetWindowsHookEx returns 0 and fails. Only AppDomain.GetCurrentThreadId() seems to work (even though it is deprecated, the replacement though doesn't work either).</p>
  
  <p>Do I have to go with the C++ code then? or is there a way to get it to work in C#?</p>
  
  <p>Currently we register the hookhandler in C++ with the other application and get events back.</p>