Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
Delphi 10.2 Tokyo Professional
|
AW: Probleme mit CopyMemory
17. Mär 2011, 15:29
Hallo,
Habe das mal probiert:
Delphi-Quellcode:
type
TMyRecord=packed record
Var1:Cardinal;
Var2:Cardinal;
Test:Array[0..30] of Char;
end;
PMyRecord = ^TMyRecord;
Programm1 (Sender):
Delphi-Quellcode:
procedure TForm1.SendRec<T>(AHandle: THandle; Rec: T);
var
CopyData: TCopyDataStruct;
SendRecord: ^T;
Size: Cardinal;
begin
New(SendRecord);
try
// CopyMemory(SendRecord, @rec, SizeOf(T));
// Oder, wie Himitsu schon vorgeschlagen hat (schöner):
SendRecord^ := rec;
CopyData.cbData := SizeOf(T);
CopyData.lpData := SendRecord;
CopyData.dwData := Cardinal(SendRecord);
SendMessage(AHandle,WM_COPYDATA, Handle,LParam(@CopyData));
finally
Dispose(SendRecord);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var test: TMyRecord;
begin
test.Var1 := 1234;
test.Var2 := 9876;
test.Test := ' Hallo Welt!';
SendRec<TMyRecord>(FindWindow( nil,' Form2'),test);
end;
Programm 2 (Empfänger):
Delphi-Quellcode:
// ...
public
procedure Data(var Msg: TWMCopyData); message WM_COPYDATA;
end;
// ...
procedure TForm2.Data(var Msg: TWMCopyData);
var tmp: TMyRecord;
begin
tmp := PMyRecord(Msg.CopyDataStruct^.lpData)^;
Showmessage(Format('Var1: %d, Var2: %d, Test: %s',[tmp.Var1,tmp.Var2,tmp.Test]));
end;
Bei mir funktioniert das ohne Probleme!
Michael "Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
Geändert von Neutral General (17. Mär 2011 um 15:50 Uhr)
|
|
Zitat
|