i use to send a data on two separate process but it fails. it works only under same process… this is concept.
//———————————————————————————–
MainApps
//———————————————————————————–
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnButtonSend
var aData : PMyrec;
begin
new(aData);
aData.Name := 'MyName';
aData.Add := 'My Address';
aData.Age : 18;
SendMessage(FindWindow('SubApps'),WM_MyMessage,0,Integer(@aData));
end;
//———————————————————————————–
SubApps
//———————————————————————————–
Type
PMyrec = ^TMyrec;
TMyrec = Record
name : string;
add : string;
age : integer;
end;
:OnCaptureMessage
var
aData : PMyrec;
begin
aData := PMyrec(Msg.LParam);
showmessage(aData^.Name);
end;
You’re right. Addresses only have meaning within a single process. The PMyRec value you create in the first process is just a garbage address in the target process.
To send an arbitrary block of memory to another process via a window message, you should use the
wm_CopyDatamessage. You give that message the address of the data and the size, and the OS takes care of copying it into the target process’s address space.Since your data includes a string, which is represented internally as a another pointer, it won’t be enough to just copy the 12 bytes of your record. You’ll need to allocate additional memory to hold the record and the string data in a single block of memory so
wm_CopyDatacan copy it and the target process can read it.Here’s one way to do it, using a stream to collect the data into a single block of memory.
We write the lengths of the strings in addition to the strings’ characters so that the recipient knows how many characters belong to each one. The recipient’s code will look like this:
I’ve used the non-standard
TReadOnlyMemoryStreamsince it makes everything easier. Here’s a simple implementation for it: