
Marshaling problem using WM_COPYDATA and byte[]
What I have come up with that seems to work is:
[StructLayout(LayoutKind.Sequential)] struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
public IntPtr lpData;
}
IntPtr p = Marshal.AllocHGlobal(Msg.Length);
Marshal.Copy(Msg, 0, p, Msg.Length);
COPYDATASTRUCT cds;
cds.dwData = (IntPtr) 100;
cds.lpData = p;
cds.cbData = Msg.Length;
SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
Is this the correct way of handling these types of problems?
It would see that moving byte[] arrays would be a basic operation and
everything else would be built on top. But it looks like most
operations revolve around strings instead.
Bewildered,
John Dyer
Quote:
> I can send strings around with WM_COPYDATA but am unable to get it
> working with a byte array.
> This works:
> [StructLayout(LayoutKind.Sequential)] struct COPYDATASTRUCT
> {
> public IntPtr dwData;
> public int cbData;
> [MarshalAs(UnmanagedType.LPStr)] public string lpData;
> }
> Then in a method:
> COPYDATASTRUCT cds;
> cds.dwData = (IntPtr) 100;
> cds.lpData = strMsg;
> cds.cbData = cds.lpData.Length + 1;
> SendMessage(hwnd, WM_COPYDATA, 0, ref cds);
> If I change lpData to:
> [MarshalAs(UnmanagedType.LP.LPArray)] public byte[] lpData;
> To send a byte array instead of a string I get a marshalling error.
> What am I missing here?
> Thanks,
> John Dyer