This code used to work with Delphi 5, but with delphi XE2 does not work as expected. The string passed using wm_copydata will be cut.
procedure SendAppParameters(aMsgStr: string);
var
hwnd: THandle;
cds: CopyDataStruct;
begin
hwnd := FindWindow('TMyAppFormMain', nil); //<-- Substitute window classname with your own
if hwnd <> 0 then
begin
// showmessage(aMsgStr);
// prepare the data to copy
cds.dwData := 0;
cds.cbData := length(AMsgStr);
cds.lpData := PChar(AMsgStr);
// activate the destination window
SetForegroundWindow(hwnd);
// send the data to the first instance using a wm_CopyData message
SendMessage(hwnd, wm_CopyData, Application.Handle, integer(@cds));
end
end;
And in the Main Form I have:
procedure TMyAppFormMain.GotMessage_CopyData(var Msg: TWmCopyData);
var
MsgString: string;
I: Integer;
begin
MsgString := PChar(Msg.CopyDataStruct.lpData);
showmessage(MsgString);
end;