1

我在将双数组从一个应用程序发送到另一个应用程序(均为 c#)时遇到问题。

我尝试像这样格式化CopyData Struct

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

Sender Application我这样填充结构:

            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);

Receiver Application,我试试这个:

    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;
    }

但在cp.lpDat一个值来null。不知道是发错了还是收错了。请帮忙,谢谢。

4

1 回答 1

0

您需要使用本机内存。像这样通过 C#double[]是行不通的。它将被垃圾收集(哎呀,它甚至没有在 C# 中分配 - 它只是在堆栈中)。您可以使用不安全/固定关键字固定对象,也可以使用AllocHGlobal.

这是我从互联网上抓取的一些代码。演示发送部分:

    // 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);
    }

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

于 2013-12-13T18:29:29.333 回答