我在将双数组从一个应用程序发送到另一个应用程序(均为 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
。不知道是发错了还是收错了。请帮忙,谢谢。