Send double array with SendMessage WM_COPYDATA

1.9k views Asked by At

I had problems to send a double array from one application to another (both c#).

I try format the CopyData Struct like this:

    [StructLayout(LayoutKind.Sequential)]
    public struct CopyDataStruct
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.R8)]
        public double[] lpData;
    }

In Sender Application I fill the struct like this:

            double[] a = new double[2] { 1.0, 2.0 };
            int size = Marshal.SizeOf(typeof(double)) * a.Count();

            CopyDataStruct copyDataStruct;
            copyDataStruct.dwData = IntPtr.Zero;
            copyDataStruct.lpData = a;
            copyDataStruct.cbData = size;

            int result = SendMessage(hWnd, WM_COPYDATA, 0, ref copyDataStruct);

In Receiver Application, I try this:

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch (msg)
        {
            case (int)WINMESSAGES.WM_COPYDATA:
                CopyDataStruct cp = (CopyDataStruct)Marshal.PtrToStructure(lParam, typeof(CopyDataStruct));

                break;
        }

        return IntPtr.Zero;
    }

but in cp.lpData value comes null. I don't know if I sent wrong or if I received wrong. Please help, thanks.

1

There are 1 answers

1
Erti-Chris Eelmaa On

You need to use native memory. Passing C# double[] like that wont work. It will be garbage collected(heck, it's not even allocated in C# - it's just in stack). You can either pin the object with unsafe/fixed keywords or use AllocHGlobal.

This is some code I ripped from Internets. Demonstrates sending part:

    // Marshal the managed struct to a native block of memory.
    var myStruct = new double[2] { 1.0, 2.0 };
    int myStructSize = Marshal.SizeOf(myStruct); 
    IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize);
    try
    {
        Marshal.StructureToPtr(myStruct, pMyStruct, true);

        COPYDATASTRUCT cds = new COPYDATASTRUCT();
        cds.cbData = myStructSize;
        cds.lpData = pMyStruct;

        // Send the COPYDATASTRUCT struct through the WM_COPYDATA message to 
        // the receiving window. (The application must use SendMessage, 
        // instead of PostMessage to send WM_COPYDATA because the receiving 
        // application must accept while it is guaranteed to be valid.)
        NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, this.Handle, ref cds);

        int result = Marshal.GetLastWin32Error();
        if (result != 0)
        {
            MessageBox.Show(String.Format(
                "SendMessage(WM_COPYDATA) failed w/err 0x{0:X}", result));
        }
    }
    finally
    {
        Marshal.FreeHGlobal(pMyStruct);
    }

Taken: http://code.msdn.microsoft.com/windowsdesktop/CSReceiveWMCOPYDATA-dbbc7ed7