title: C# WndProc method not receiving expected Msg type
body: <p>I've been trying to communicate with another software using its documented COPYDATA API. User Xanotos has been incredibly helpful <a href="https://stackoverflow.com/q/65497215/4700011">in this question I asked</a> which has the sending method sorted and working fine. I am having issues that the receiving <code>WndProc</code> method does not seem to catch the expected response. Here is the link to <a href="https://docs.google.com/document/d/1vmSvYMJz_FmD4JPTrEZjQp-mKOgI0Z9izFWIHUyG05o/edit?usp=sharing" rel="nofollow noreferrer">COPYDATA API</a> documentation for reference.</p>
  <p>The current method is below. Testing has shown the <code>WndProc</code> does receive messages, but not the ones I expect, namely a <code>struct</code> depending on which message was sent.</p>
  <p>Declarations:</p>
  <pre><code> [DllImport(&quot;User32.dll&quot;, SetLastError = true, EntryPoint = &quot;FindWindow&quot;)]
      public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
  
      [DllImport(&quot;User32.dll&quot;, SetLastError = true, EntryPoint = &quot;SendMessage&quot;)]
      public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
  
      [StructLayout(LayoutKind.Sequential)]
      public struct COPYDATASTRUCT
      {
          public IntPtr dwData;    // Any value the sender chooses.  Perhaps its main window handle?
          public int cbData;       // The count of bytes in the message.
          public IntPtr lpData;    // The address of the message.
      }
      public struct ExternalGetPositionType
      {
          public double X;
          public double Y;
          public double Z;
          public double W;
      }
      const int WM_COPYDATA = 0x004A;
      const int EXTERNAL_CD_COMMAND_RUN_ASYNC = 0x8001;
      const int EXTERNAL_CD_GET_POSITION_PCS = 0x8011;
      const int EXTERNAL_CD_GET_POSITION_MCS = 0x8012;
  </code></pre>
  <p>Sending Methods (these all work fine and return true):</p>
  <pre><code>public static IntPtr RunAsync(IntPtr hwnd, string str)
      {
          // We have to add a \0 terminator, so len + 1 / len + 2 for Unicode
          int len = Encoding.Default.GetByteCount(str);
          var buff = new byte[len + 1]; // len + 2 for Unicode
          Encoding.Default.GetBytes(str, 0, str.Length, buff, 0);
  
          IntPtr ret;
  
          GCHandle h = default(GCHandle);
  
          try
          {
              h = GCHandle.Alloc(buff, GCHandleType.Pinned);
  
              var cds = new COPYDATASTRUCT();
              cds.dwData = (IntPtr)EXTERNAL_CD_COMMAND_RUN_ASYNC;
              cds.lpData = h.AddrOfPinnedObject();
              cds.cbData = buff.Length;
  
              ret = SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
          }
          finally
          {
              if (h.IsAllocated)
              {
                  h.Free();
              }
          }
  
          return ret;
      }
  
      public static IntPtr GetPosition(IntPtr hwnd, bool pcs, ExternalGetPositionType position)
      {
          // We cheat here... It is much easier to pin an array than to copy around a struct
          var positions = new[]
          {
      position
  };
  
          IntPtr ret;
  
          GCHandle h = default(GCHandle);
  
          try
          {
              h = GCHandle.Alloc(positions, GCHandleType.Pinned);
  
              var cds = new COPYDATASTRUCT();
              cds.dwData = pcs ? (IntPtr)EXTERNAL_CD_GET_POSITION_PCS : (IntPtr)EXTERNAL_CD_GET_POSITION_MCS;
              cds.lpData = h.AddrOfPinnedObject();
              cds.cbData = Marshal.SizeOf&lt;ExternalGetPositionType&gt;();
  
              ret = SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
          }
          finally
          {
              if (h.IsAllocated)
              {
                  h.Free();
              }
          }
  
          return ret;
      }
  </code></pre>
  <p><code>WndProc</code> Method - This is where things don't work as expected. The <code>if(m.Msg == WM_COPYDATA)</code> never returns true when <code>GetPosition</code> is called.</p>
  <pre><code>protected override void WndProc(ref Message m)
      {
          if (m.Msg == WM_COPYDATA) //this does not execute
          {
              COPYDATASTRUCT cds = Marshal.PtrToStructure&lt;COPYDATASTRUCT&gt;(m.LParam);
              label5.Text = &quot;message received&quot;;
              if (cds.dwData == (IntPtr)EXTERNAL_CD_COMMAND_RUN_ASYNC)
              {
                  label5.Text = &quot;NORMAL&quot;;
                  string str = Marshal.PtrToStringAnsi(cds.lpData);
  
                  Debug.WriteLine($&quot;EXTERNAL_CD_COMMAND_RUN_ASYNC: {str}&quot;);
                  toolStripStatusLabel1.Text = $&quot;COMMAND&quot;;
                  m.Result = (IntPtr)100; // If you want to return a value
              }
              else if (cds.dwData == (IntPtr)EXTERNAL_CD_GET_POSITION_PCS) //this does not execute
              {
                  label5.Text = &quot;MSC&quot;;
                  if (cds.cbData &gt;= Marshal.SizeOf&lt;ExternalGetPositionType&gt;())
                  {
                      var position = Marshal.PtrToStructure&lt;ExternalGetPositionType&gt;(cds.lpData);
  
                      Debug.WriteLine($&quot;EXTERNAL_CD_GET_POSITION_PCS: X = {position.X}, Y = {position.Y}, Z = {position.Z}, W = {position.W}&quot;);
                      toolStripStatusLabel1.Text = $&quot;External MCS = {position.X}&quot;;
                      label4.Text = position.X.ToString();
  
                      m.Result = (IntPtr)200;
                  }
                  else
                  {
                      m.Result = (IntPtr)0;
                  }
              }
              else if (cds.dwData == (IntPtr)EXTERNAL_CD_GET_POSITION_MCS) //this does not execute
              {
                  label5.Text = &quot;MSC&quot;; //this does not execute
                  if (cds.cbData &gt;= Marshal.SizeOf&lt;ExternalGetPositionType&gt;())
                  {
                      var position = Marshal.PtrToStructure&lt;ExternalGetPositionType&gt;(cds.lpData);
  
                      Debug.WriteLine($&quot;EXTERNAL_CD_GET_POSITION_MCS: X = {position.X}, Y = {position.Y}, Z = {position.Z}, W = {position.W}&quot;);
                      toolStripStatusLabel1.Text = $&quot;External MCS = {position.X}&quot;;
                      label4.Text = position.X.ToString(); 
  
                      m.Result = (IntPtr)300;
                  }
                  else
                  {
                      m.Result = (IntPtr)0;
                  }
              }
  
              return;
          }
          MessageBox.Show(m.Msg.ToString()); //this DOES execute
          
          base.WndProc(ref m);
      }
  </code></pre>
  <p>Both programs are 32 bit.</p>
  <p>Any ideas why I am not receiving the expected <code>Msg</code>?</p>