🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.csharpbuilder.language.csharp
3 messages
3 total messages Started by Ray Sat, 20 Jan 2007 17:28
WM_COPYDATA
#116
Author: Ray
Date: Sat, 20 Jan 2007 17:28
67 lines
2776 bytes
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);
 }
Re: WM_COPYDATA
#117
Author: Marc Rohloff TeamB
Date: Sat, 20 Jan 2007 19:31
40 lines
2339 bytes
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
Re: WM_COPYDATA
#118
Author: Ray
Date: Sun, 21 Jan 2007 13:16
46 lines
2251 bytes
Many thanks all working fine now

Ray
"Marc Rohloff [TeamB]" <"on request"> wrote in message 
news:80n5rkq4zz6p.dlg@dlg.marcrohloff.com...
> 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 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads