🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #116

WM_COPYDATA

#116
From: 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);
 }
Technical Details
Message-ID: <45b250bd@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,5bed5c3cf1c76c51,start
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Ray" <noreply@daemon.com>
Newsgroups: borland.public.csharpbuilder.language.csharp
Subject: WM_COPYDATA
Date: Sat, 20 Jan 2007 17:28:43 -0000
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3028
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 81.76.249.27
Message-ID: <45b250bd@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1169313981 81.76.249.27 (20 Jan 2007 09:26:21 -0800)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail