title: WM_COPYDATA from c++ to C#
body: <p>I'm developing 4 applications (using founded examples in internet) with Visual Studio 2010: 2 in C++ MFC (named: SenderCpp and ReceiverCpp) and 2 in C# (named: SenderCsh and ReceiverCsh) to test the WM_DATACOPY interprocess communication. </p>
  
  <p>My goal is to have the C++ application "SenderCpp" that send some data to the C# application "ReceiverCsh" and it doesn't work, no message is received by the "ReceiverCsh".</p>
  
  <p>So:</p>
  
  <ul>
  <li>SenderCpp->ReceiverCpp: works</li>
  <li>SenderCsh->ReceiverCsh: works</li>
  <li>SenderCpp->ReceiverCsh: not works</li>
  </ul>
  
  <p>The SenderCpp code is:</p>
  
  <pre><code>void SendCopyData(HWND hFind)
  {
      COPYDATASTRUCT cp;
      StackRecord record;
  
      record.CursorX = 1;
      record.CursorY = -1;
  
     _tcscpy(record.cmdline, L"Hello World!");
     cp.cbData = sizeof(record);
     cp.lpData = &amp;record;
     cp.dwData = 12;
     SendMessage(hFind, WM_COPYDATA, NULL, (LPARAM)&amp;cp);
  }
  
  void CCANDriverDlg::OnBnClickedButton2()
  {
     HWND hWnd = ::FindWindow(NULL, CString("ReceiverCpp"));
     SendCopyData(hWnd);
  }
  
  void CCANDriverDlg::OnBnClickedButton1()
  {
     HWND hWnd = ::FindWindow(NULL, CString("ReceiverCsh"));
     SendCopyData(hWnd);
  }
  </code></pre>
  
  <p>The ReceiverCsh is:</p>
  
  <pre><code> protected override void WndProc(ref Message m)
      {
          switch(m.Msg)
          {
              case WM_COPYDATA:
                  // Win32.CopyDataStruct st = (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(Win32.CopyDataStruct));
                  CopyDataStruct st = (CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(CopyDataStruct));
                  //txtmessagereceive.Text = strData;
  
                  CANMessage MsgIn = new CANMessage(8);
                  MsgIn = (CANMessage)Marshal.PtrToStructure(st.lpData, typeof(CANMessage));
  
                  // Decode the marshaled message
                  txtmessagereceive.Text = "dwData = " + st.dwData.ToString() +"\r\n" +
                                          "cbData = " + st.cbData + " bytes\r\n" +
                                          "CANID = " + MsgIn.CANID.ToString() + "\r\n";
                  for (int i=0; i&lt;8; i++)
                  {
                      txtmessagereceive.Text += String.Format("Data[{0}]={1}\r\n", i, MsgIn.Data[i]);
                  }
  
                  break;
              default:
                  // let the base class deal with it
                  base.WndProc(ref m);
                  break;
          }
      }
  </code></pre>
  
  <p>Using Spy++ I see that monitoring the "ReceiverCsh" no message is displayed when the WM_COPYDATA is sent by SenderCpp instead the message is displayed when the ReceiverCpp is analyzed.</p>
  
  <p>Have you some idea?
  Thanks.</p>