title: C# - Capturing Windows Messages from a specific application
body: <p>I'm writing a C# application which needs to intercept <strong>Window Messages</strong> that another applications is sending out. The company who wrote the application I'm monitoring sent me some example code, however it's in C++ which I don't really know.</p>
  
  <p>In the C++ example code I've got they use the following code:</p>
  
  <pre><code> UINT uMsg = RegisterWindowMessage(SHOCK_MESSAGE_BROADCAST);
   ON_REGISTERED_MESSAGE(WM_SHOCK_BROADCAST_MESSAGE, OnShockStatusMessage)
   LRESULT OnShockStatusMessage(WPARAM wParam, LPARAM lParam);
  </code></pre>
  
  <p>As I understand it this retrieves an Id from Windows for the specific message we want to listen for. Then we're asking C++ to call <strong>OnShockStatusMessage</strong> whenever an message matching the Id is intercepted.</p>
  
  <p>After a bit of research I've put together the following in C#</p>
  
  <pre><code>[DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  
  [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  static extern uint RegisterWindowMessage(string lpString);
  
  private IntPtr _hWnd; // APS-50 class reference
  private List&lt;IntPtr&gt; _windowsMessages = new List&lt;IntPtr&gt;(); // APS-50 messages
  
  private const string _className = "www.AuPix.com/SHOCK/MessageWindowClass";
  
  // Windows Messages events
  private const string _messageBroadcast = "www.AuPix.com/SHOCK/BROADCAST";
  private const string _messageCallEvents = "www.AuPix.com/SHOCK/CallEvents";
  private const string _messageRegistrationEvents = "www.AuPix.com/SHOCK/RegistrationEvents";
  private const string _messageActions = "www.AuPix.com/SHOCK/Actions";
  
  private void DemoProblem()
  {
      // Find hidden window handle
      _hWnd = FindWindow(_className, null);
  
      // Register for events
      _windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageActions ) ) );
      _windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageBroadcast ) ) );
      _windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageCallEvents ) ) );
      _windowsMessages.Add( new IntPtr( RegisterWindowMessage( _messageRegistrationEvents ) ) );
  }
  
  protected override void WndProc(ref Message m)
  {
      base.WndProc(ref m);
  
      // Are they registered Windows Messages for the APS-50 application?
      foreach (IntPtr message in _windowsMessages)
      {
          if ((IntPtr)m.Msg == message)
          {
              Debug.WriteLine("Message from specified application found!");
          }
      }
  
      // Are they coming from the APS-50 application?
      if ( m.HWnd == shock.WindowsHandle)
      {
          Debug.WriteLine("Message from specified application found!");
      }
  
  }
  </code></pre>
  
  <p>As I understand this should do the same basic thing, in that it:</p>
  
  <ol>
  <li>Finds the application I wish to monitor</li>
  <li>Registers the Window Messages I wish to intercept</li>
  <li>Watches for all Window Messages - then strips out the ones I need</li>
  </ol>
  
  <p>However in my override of the <strong>WndProc()</strong> method neither of my checks intercept any of the specific messages or any message from the application I'm monitoring.</p>
  
  <p>If I <strong>Debug.WriteLine</strong> for all messages that come through it, I can see that it's monitoring them. However it never filters out the messages that I want.</p>
  
  <p>By running the example monitoring application written in C++ I can see that Window Messages are being sent and picked up - it's just my C# implemention doesn't do the same.</p>