title: Hook to receive window created and destroyed notification
body: <p>I use RegisterWindowMessage("SHELLHOOK") to register for window create and destroy events. My code is written in C#. The code works perfectly when I'm debugging the code. But when I run the program without debugging, I'm not getting WndProc messages like I used to get while debugging. Does Windows block the hook?</p>
  
  <pre><code>class ShellHook:NativeWindow
  {
      public ShellHook(IntPtr hWnd)
      {
          if (Win32.RegisterShellHookWindow(this.Handle))
          {
              WM_ShellHook    = Win32.RegisterWindowMessage("SHELLHOOK");
          }
      }
  
      protected override void WndProc(ref Message m)
      {
          if (m.Msg == WM_ShellHook)
          {
              switch((ShellEvents)m.WParam)
              {
                      //m.lparam
                  case ShellEvents.HSHELL_WINDOWCREATED:
                      if (windowCreatedEvent != null)
                      {
                          windowCreatedEvent(m.LParam);
                      }
                      break;
  
                  case ShellEvents.HSHELL_WINDOWDESTROYED:
                      if (windowDestroyedEvent != null)
                      {
                          windowDestroyedEvent(m.LParam);
                      }
                      break;
              }
          }
          base.WndProc(ref m);
      }
  }
  </code></pre>
  
  <p>This is how I start my wpf app.</p>
  
  <pre><code> public partial class App : Application
   {
      MainWindow mainWindowView;
  
      public App()
      {
          Startup += new StartupEventHandler(App_Startup);
      }
  
      void App_Startup(object sender, StartupEventArgs e)
      {
          mainWindowView = new MainWindow();
          MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
          mainWindowView.DataContext = mainWindowViewModel;
          mainWindowView.ShowDialog();
      }
   }
  </code></pre>
  
  <p>My MainWindowViewModel Constructor is as follows:</p>
  
  <pre><code> public MainWindowViewModel()
      {
          EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
          System.Windows.Forms.Form f = new System.Windows.Forms.Form();
          ShellHook shellHookObject = new ShellHook(f.Handle);
          shellHookObject.windowCreatedEvent += shellHookObject_windowCreatedEvent;
          shellHookObject.windowDestroyedEvent += shellHookObject_windowDestroyedEvent;
      }
  </code></pre>