title: SetWindowsHookEx another process made by C# always return 0 IntPtr
body: <p>I am trying to hook mouse click event to a windows application which is made of C# as well.</p>
  
  <p>This is my code;</p>
  
  <pre><code>private const int WH_MOUSE = 7;
  
  [DllImport("user32.dll", SetLastError = true)]
  static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  
  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
  
  private static IntPtr SetHook(LowLevelMouseProc proc)
  {
      IntPtr hWnd = IntPtr.Zero;
      uint processId = 0;
      uint threadID = 0;
      string moduleName   = string.Empty;
      Process[] localAll = Process.GetProcesses();
      string toFindModule  ="windowsformsapplication1.exe";
      foreach (Process p in localAll)
      {
          if (p.MainWindowHandle != IntPtr.Zero)
          {
              ProcessModule pm = GetModule(p);
              if (pm != null &amp;&amp; p.MainModule.FileName.ToLower().IndexOf(toFindModule) &gt; -1)
              {
                  hWnd = p.MainWindowHandle;
                  threadID = GetWindowThreadProcessId(hWnd, out processId);
                  break;
              }
          }
      }
  
      IntPtr ret = SetWindowsHookEx(WH_MOUSE, proc, IntPtr.Zero, threadID);
  
      return ret;
  }
  </code></pre>
  
  <p>I have assured the thread ID of WindowsFormsApplication1.exe.</p>
  
  <p>But, <code>IntPtr ret = SetWindowsHookEx(WH_MOUSE, proc, IntPtr.Zero, threadID);</code> always returns 0;</p>
  
  <p>So, I am enable hook the mouse click event.</p>
  
  <p>Is there something wrong with my code?</p>
  
  <p>I even tried <code>calc.exe</code>, but no luck either.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>And I am pretty sure that I captured correct thread id of my target winform.</p>
  
  <p><img src="https://i.stack.imgur.com/NT56x.png" alt="enter image description here"></p>