Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WM_COPYDATA

2 views
Skip to first unread message

Ray

unread,
Jan 20, 2007, 12:28:43 PM1/20/07
to
Hi

I am trying to use WM_COPYDATA to send data between applications. I can send
one integer successfully but if I try to send more than one the second is
garbage.

If I try to send an integer array it crashes with a message that my array is
not blittable. I understood that integer arrays as distinct from Array types
were blittable. So I am definitely missing something here.

My code is as follows
Declarations (common to both sender and receiver)

public class Win32
{
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public static extern int SendMessage(uint hWnd, int msg, IntPtr wParam,
IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class CopyDataStruct
{
public int dwData;
public int cbData;
public IntPtr data;
}
[StructLayout(LayoutKind.Sequential)]
public class DataArray
{
public int num1;
public int num2;
}
}

Sender code

Win32.DataArray sentData = new Win32.DataArray();
sentData.num1=7101;
sentData.num2=8102;
IntPtr numMemory =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.DataArray)));
Marshal.StructureToPtr(sentData, numMemory, false);
Win32.CopyDataStruct cds=new Win32.CopyDataStruct();
cds.dwData = 101;
cds.cbData = Marshal.SizeOf(cds.dwData);
cds.data = numMemory;

IntPtr cdsMemory =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Win32.CopyDataStruct)));
Marshal.StructureToPtr(cds, cdsMemory, false);
Win32.SendMessage(recHandle,(int)msg,IntPtr.Zero, cdsMemory);
Marshal.FreeHGlobal(cdsMemory);
Marshal.FreeHGlobal(numMemory);

Receiver code

unsafe protected override void WndProc(ref Message m)
{
if(m.Msg==msg)
{
Win32.CopyDataStruct cds=new Win32.CopyDataStruct();
Win32.DataArray recData= new Win32.DataArray();
Marshal.PtrToStructure((IntPtr)m.LParam.ToPointer(), cds);
Marshal.PtrToStructure(cds.data,recData);
// read recData.num1 and recData.num2
}
base.WndProc(ref m);
}


Marc Rohloff [TeamB]

unread,
Jan 20, 2007, 2:31:16 PM1/20/07
to
On Sat, 20 Jan 2007 17:28:43 -0000, Ray wrote:

> Hi
>
> I am trying to use WM_COPYDATA to send data between applications. I can send
> one integer successfully but if I try to send more than one the second is
> garbage.

> cds.cbData = Marshal.SizeOf(cds.dwData);
This should be
cds.cbData = Marshal.SizeOf(sendData);

A simpler way to call SendMessage would be:

public class Win32
{
[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public static extern int SendMessage(

HandleRef hWnd,
int msg,
IntPtr wParam,
[In, Out,
MarshalAs(UnmanagedType.LPStruct)]
CopyDataStruct lParam);

[StructLayout(LayoutKind.Sequential, Pack=4)]
public struct CopyDataStruct


{
public int dwData;
public int cbData;
public IntPtr data;
}

This way you can just pass a CopyDataStruct into SendMessage without
having to worry about Marshalling it.

--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com

Ray

unread,
Jan 21, 2007, 8:16:37 AM1/21/07
to
Many thanks all working fine now

Ray
"Marc Rohloff [TeamB]" <"on request"> wrote in message
news:80n5rkq4...@dlg.marcrohloff.com...

0 new messages