title: How to hook an application?
body: <p>I'm trying to hook the creation of a windows in my C# app.</p>
  
  <pre><code>static IntPtr hhook = IntPtr.Zero;
  static NativeMethods.HookProc hhookProc;
  
  static void Main(string[] args)
  {
      // Dummy.exe is a form with a button that opens a MessageBox when clicking on it.
      Process dummy = Process.Start(@"Dummy.exe");
  
      try
      {
          hhookProc = new NativeMethods.HookProc(Hook);
          IntPtr hwndMod = NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);
          hhook = NativeMethods.SetWindowsHookEx(HookType.WH_CBT, hhookProc, hwndMod, (uint)AppDomain.GetCurrentThreadId());
  
          Console.WriteLine("hhook valid? {0}", hhook != IntPtr.Zero);
  
          while (!dummy.HasExited)
              dummy.WaitForExit(500);                
      }
      finally
      {
          if(hhook != IntPtr.Zero)
              NativeMethods.UnhookWindowsHookEx(hhook);
      }
  }
  
  static int Hook(int nCode, IntPtr wParam, IntPtr lParam)
  {
      Console.WriteLine("Hook()");
      return NativeMethods.CallNextHookEx(hhook, nCode, wParam, lParam);
  }
  </code></pre>
  
  <p>Problem is, when clicking on my button (in Dummy.exe), I never enter in my Hook, what am I doing wrong?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>Program.cs</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  
  namespace Hooker
  {
      class Program
      {
          static IntPtr hhook = IntPtr.Zero;
          static NativeMethods.HookProc hhookProc;
  
          static void Main(string[] args)
          {
              Process dummy = Process.Start(@"Dummy.exe");
  
              try
              {
                  hhookProc = new NativeMethods.HookProc(Hook);
                  hhook = NativeMethods.SetWindowsHookEx(HookType.WH_CBT, hhookProc, IntPtr.Zero, 0);
  
                  Console.WriteLine("hhook valid? {0}", hhook != IntPtr.Zero);
  
                  while (!dummy.HasExited)
                      dummy.WaitForExit(500);                
              }
              finally
              {
                  if(hhook != IntPtr.Zero)
                      NativeMethods.UnhookWindowsHookEx(hhook);
              }
          }
  
          static int Hook(int nCode, IntPtr wParam, IntPtr lParam)
          {
              Console.WriteLine("Hook()");
              return NativeMethods.CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
          }
      }
  }
  </code></pre>
  
  <p>NativeMethods.cs</p>
  
  <pre><code>namespace Hooker
  {
      using System;
      using System.Runtime.InteropServices;
  
      internal static class NativeMethods
      {
          public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
  
          [DllImport("user32.dll", SetLastError = true)]
          public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, int dwThreadId);
          [DllImport("user32.dll")]
          public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
          [DllImport("user32.dll", SetLastError = true)]
          [return: MarshalAs(UnmanagedType.Bool)]
          public static extern bool UnhookWindowsHookEx(IntPtr hhk);
  
          [DllImport("user32.dll", SetLastError = true)]
          public static extern int GetWindowThreadProcessId(IntPtr hwnd, ref int pid);
  
          [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
          public static extern IntPtr GetModuleHandle(string lpModuleName);
      }
  }
  </code></pre>
  
  <p>For dummy, do a new Form with :</p>
  
  <pre><code>    public Form1()
      {
          InitializeComponent();
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          MessageBox.Show("CONTENT", "TITLE");
      }
  </code></pre>