I'm trying to make to Delphi applications communicate with each other via WM_COPYDATA. The problem I'm having though is is that the sender app is written in Delphi 7 and the receiver is written in Delphi 10.2 . I copied my Delphi 7 program's code into Delphi 10 and the communication worked perfectly. Using the exact same code in Delphi 7 however caused my string being passed to the receiver app to get corrupted. The codes I use are as follows:
One the sending side I have:
procedure TSenderApp.SendString(ToSend: string);
var
copyDataStruct : TCopyDataStruct;
receiverHandle : THandle;
res : integer;
begin
copyDataStruct.dwData := 140500; //use it to identify the message contents
copyDataStruct.cbData := (1+ Length(ToSend))* SizeOf(Char) ;
copyDataStruct.lpData := pchar(ToSend) ;
receiverHandle := FindWindow(PChar('TRecieverApp'),PChar('RecieverApp')) ;
if receiverHandle = 0 then
begin
ShowMessage('CopyData Receiver NOT found!') ;
Exit;
end;
res := SendMessage(receiverHandle, WM_COPYDATA, Integer(Handle),
LPARAM(@copyDataStruct)) ;
end;
And on the receiving side I have:
procedure TRecieverApp.WMCopyData(var Message: TMessage);
var
p : PCopyDataStruct;
l : Integer;
s : string;
begin
p := PCopyDataStruct( Message.lParam );
if (p <> nil) then
begin
ShowMessage('New Message Recieved!');
l := p^.cbData;
SetLength( s, (l+1) );
StrLCopy( PChar(s), PChar(p^.lpData), l );
Edit1.Text := s;
end
else
Edit1.Text := 'ERROR';
end;
What am I doing wrong? Or why is the message string being corrupted when sent from the Delphi 7 written SenderApp and not from the Delphi 10 written SenderApp?