I'm trying to use WM_COPYDATA to copy data between two Delphi Prism applications on the same computer.
In the sender application I have the code:
type
[StructLayout(LayoutKind.Sequential)]
CopyDataStruct = public record
var dwData: IntPtr;
var cbData: System.Int32;
var [MarshalAs(UnmanagedType.ByValArray, SizeConst := 11)]
lpData: array[0..10] of Int32;
end;
ConsoleApp = class
public
const WM_COPYDATA: System.Int32 = $4a;//WM_COPYDATA 0x004A
class method Main;
[DllImport('user32.dll', EntryPoint := 'FindWindow')]
class method FindWindow(lpClasName: System.String; lpWindowName: System.String): System.IntPtr; external;
[DllImport('user32.dll', EntryPoint := 'SendMessage')]
class method SendMessage(hWnd: System.IntPtr; Msg: System.Int32; wParam: System.Int32; var lParam: CopyDataStruct): System.Int32; external;
end;
implementation
class method ConsoleApp.Main;
var
cd : CopyDataStruct;
WHnd : IntPtr;
i : word;
MyArray : Array[0..10] of Int32;
begin
cd.dwData := IntPtr(0);
for i := 0 to 10 do
MyArray[i] := i;
cd.lpData := MyArray;
cd.cbData := MyArray.Length;
WHnd := FindWindow(nil, 'PrismForm');
if (WHnd<>System.IntPtr.Zero) then
begin
SendMessage(WHnd, WM_COPYDATA, 0, var cd);
Console.WriteLine('Message sent');
end
else
Console.WriteLine('Window not found');
Console.ReadKey();
end;
In the receiver app I have the same CopyDataStruct definition and the code:
method PrismForm.WndProc(var m: Message);
var
cd : CopyDataStruct;
s : System.String;
i : Int32;
begin
case m.Msg of
WM_COPYDATA : begin
TextBox1.Text := "Copy message received. ";
cd := m.GetLParam(typeof(CopyDataStruct)) as CopyDataStruct;
TextBox1.AppendText(cd.cbData.ToString);
for i := 0 to 10 do
TextBox1.AppendText(', ' + cd.lpData[i].ToString);
end
end;
inherited WndProc(var m);
end;
When using this code, the array I get at the receiving end is garbage. Anyone know what I'm doing wrong?