Messaging interop between c# and VB6 mdi applications
<p>I have two mdi applications, both of which retrieve their data from the same database.
The two applications need to be able to send messages to each other to keep in synch.
The messages being passed back and forth only contain a string telling the recieving application which piece of data in the database it should be looking at (a job number, and some additional related info).
Both applications have a message handler, instantiated when each program starts up.</p>

<p>When a message is sent from the VB6 app to the c# app it sees the message, and acts appropriately, but when I send the same type of message from the c# app to the VB6 app, it seems to see the message event, but when unpacking it, only sees part of the data, and then ignores the message.</p>

<p>I'm thinking I may be formatting something wrong on the c# end.
Here is the method that sends the message:</p>

<pre><code>namespace InterProcessMessaging
{
[StructLayout(LayoutKind.Sequential)]
    public struct COPYDATASTRUCT
    {
        public IntPtr dwData;//a pointer to a number use this to identify your message
        public IntPtr lpData;//a pointer to the address of the data
        public IntPtr cbData;//a pointer to the number of bytes to be transferred 

    }
 public class clsMessaging : System.Windows.Forms.NativeWindow, IDisposable
 {
 //API function to send async. message to target application
        [DllImport("user32.dll", CharSet = CharSet.Ansi)]
        public static extern IntPtr SendMessageA(IntPtr hwnd, Int32 wMsg, Int32 wParam, COPYDATASTRUCT lParam);

  public void SendMessageToVB6(string sendMsg, string WindowsAppTitle)
  {
            try
            {

                IntPtr hwndTarget = FindWindow(null, WindowsAppTitle);

                IntPtr pDWData = Marshal.AllocHGlobal(sizeof(Int32));//a pointer to a number used this to identify your message
                Marshal.StructureToPtr(3, pDWData, true);//place the value 3 at this location

                IntPtr pLPData = Marshal.StringToHGlobalAnsi(sendMsg.Trim());//a pointer to the address of the data

                IntPtr pCBData = Marshal.AllocHGlobal(sizeof(Int32));//a pointer to the number of bytes to be transferred 
                Marshal.StructureToPtr(sendMsg.Trim().Length+1, pCBData, true);//place the size of the string at this location

                COPYDATASTRUCT cds;//a structure containing the three pointers above
                cds.dwData = pDWData;//a pointer to a number used this to identify your message (3)
                cds.lpData = pLPData;//a pointer to the address of the data
                cds.cbData = pCBData;//a pointer to the number of bytes to be transferred   

                if (!System.IntPtr.Zero.Equals(hwndTarget))
                {
                    SendMessageA(hwndTarget, 74, 0, cds);
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.InnerException.ToString());
            }
  }
 }
}
</code></pre>
