Marshaling problem using WM_COPYDATA and byte[] 
Author Message
 Marshaling problem using WM_COPYDATA and byte[]

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



Fri, 04 Feb 2005 03:14:08 GMT  
 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



Sun, 06 Feb 2005 03:01:36 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Problem using Marshal.PtrToStructure

2. Problems using marshaled interface

3. Converting byte[] to structure without Marshal?

4. Marshal.StructureToPtr, IntPtr and byte[]

5. Using WM_COPYDATA in MFC

6. Marshal..marshaling..

7. Problem transferring BYTE buffer using CArchive

8. WM_COPYDATA Problems (urgent) ...

9. Problem in sending WM_COPYDATA message

10. Problem with WM_COPYDATA

11. Still got problems with WM_COPYDATA message !

12. Send events from ATL object by using Free Treaded Marshaling

 

 
Powered by phpBB® Forum Software