In fact, no matter what method, the root cause is to pass the object pointer to achieve the effect.
Method 1:
Procedure sendstring (strmsg: string );
VaR
Data: tagcopydatastruct;
Pbuf: pchar;
Begin
Getmem (pbuf, length (strmsg) + 1 );
Try
Zeromemory (pbuf, length (strmsg) + 1 );
Strpcopy (pbuf, strmsg );
Data. cbdata: = length (strmsg );
Data. dwdata: = length (strmsg );
Data. lpdata: = pbuf;
Sendmessage (htargetwin, wm_copydata, INTEGER (self. Handle), INTEGER (@ data ));
Finally
Freemem (pbuf );
End;
End;
Procedure wmcopydata (var msg: tmessage); message wm_copydata;
Procedure tform1.wmcopydata (var msg: tmessage );
VaR
Data: ^ tagcopydatastruct;
Strmsg: string;
Begin
Data: = pointer (msg. lparam );
Strmsg: = strpas (data. lpdata );
Showmessage (strmsg );
End;
Method 2:
Tmyrecord = record S: string; end;
TT: tmyrecord;
VaR TT: tmyrecord; begin TT. S: = 's2343243'; postmessage (handle, wm_my, INTEGER (TT), 5); end;
To send a message, because the parameter can only be an integer, you can send only four bytes in this way, so you need to change it to a sending pointer, for example:
VaR TT: tmyrecord; begin TT. S: = 's2343243'; postmessage (handle, wm_my, INTEGER (@ TT), 5); end;
Of course, the original method cannot be used for receiving: My: = tmyrecord (msg. wparam); in this way, the tmyrecord should be declared as a pointer:
Type pmyrecord = ^ tmyrecord; var my: pmyrecord; my: = pmyrecord (msg. wparam );