Send another program data after work, send a string. I would like to understand why postmessage
does not work while SendMessage
sends data. I do not need to return the result after sending messages, so I want to refuse SendMessage
.
Unit2;
Interface.
Uses.
WinAPI.Windows, WinAPI.Messages, System.Sysutils, System.Variants,
System.Classes, vcl.graphics, vcl.controls, vcl.forms, vcl.dialogs,
Vcl.stdctrls;
Type
TForm2 = Class (TForm)
Button1: Tbutton;
Memo1: TMemo;
Procedure SendData (Const CopyDataStruct: TCOPYDATASTRUC);
Procedure Button1Click (Sender: Togject);
Procedure Wmcopydata (VAR MSG: twmcopydata); Message WM_COPYDATA;
end;
var.
Form2: TForm2;
Implementation
{$ R * .dfm}
Procedure TForm2.Button1Click (Sender: Togject);
var.
StringTosend: String;
CopyDataStruct: TCOPYDATASTRUC;
Begin.
StringTosend: = 'Avtorize Sucefull';
CopyDataStruct.dwdata: = 0; // identify the data
CopyDataStruct.cbdata: = 1 + length (stringtosend);
CopyDataStruct.lpdata: = pchar (stringtosend);
SendData (CopyDataStruct);
end;
Procedure TForm2.sendData (Const CopyDataStruct: TCOPYDATASTRUC);
var.
ReceiverHandle: Thandle;
Res: Integer;
Begin.
ReceiverHandle: = FindWindow (PChar ('TForm2'), PChar ('Form2'));
If ReceiverHandle = 0 THEN
Begin.
form2.memo1.lines.add (INTTOSTR (ReceiverHandle) + 'No');
Exit;
end;
// Res: = SendMessage (ReceiverHandle, WM_COPYDATA, INTEGER (Handle), Integer (@CopyDataStruct));
PostMessage (ReceiverHandle, WM_COPYDATA, INTEGER (Handle), Integer (@CopyDataStruct));
form2.memo1.lines.add (INTTOSTR (ReceiverHandle));
end;
Procedure TForm2.WmcopyData (VAR MSG: twmcopydata);
var.
S: String;
Begin.
form2.memo1.lines.add ('SD');
S: = pchar (msg.copydatastruct.lpdata);
form2.memo1.lines.add (s);
Msg.Result: = 2006; // send something back
end;
end.
Answer 1, Authority 100%
postmessage
function can not send WM_COPYDATA
.
Here is a long post, with an explanation:
Why can’t i postmessage The WM_CopyData Message, But i CAN SENDMESSATIMEOUT IT WITH A TINY TIMEOUT?
If in a nutshell, then this is due to the fact that the system needs to know at what point the message is delivered and you can already release the internal buffers. Obviously, this can only be done using a synchronous call SendMessage
.