Reputation: 4221
I send a Message to an external program :
SendMessage(Handle, WM_CHAR, Ord('A'), 0);
But i don't know how can i receive this message in another program ? (with WndProc or something like that) may somebody explain that for me ?
Thank you
Upvotes: 1
Views: 5887
Reputation: 6477
I use the following code in a series of programs; one program launches programs, and when each program finishes, they send a message back to the launcher that they have finished. The key to the program is using the HWND_BROADCAST parameter: the message is sent to all running programs on the computer, but of course it is handled only by those who have the correct message handler. In the program which has to send the message, write
SendMessage (HWND_BROADCAST, RegisterWindowMessage ('message'), 0, 0);
Obviously, you would replace 'message' with some string which is a constant in both programs (the one that sends and the one that receives).
I don't recommend the use of the lparam parameter of SendMessage in order to pass data to the receiving program; this is supposed to be a pointer, and of course a pointer to data in program 1 would point to garbage in program 2. One could cast a longint to a pointer and send that, which would then have to be dereferenced on the receiving side
In the receiving program,
type
TWMMYMessage = record
Msg: Cardinal; // ( first is the message ID )
Handle: HWND; // ( this is the wParam, Handle of sender)
Info: Longint; // ( this is lParam, pointer to our data)
Result: Longint;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
wm_Launcher:= RegisterWindowMessage ('message')
// wm_launcher is a private member of TForm
.
.
.
end;
procedure TForm1.DefaultHandler(var Message);
var
ee: TWMMYMessage;
begin
with TMessage (Message) do
begin
if (Msg = wm_Launcher) then
begin
ee.Msg:= Msg;
ee.Handle:= wParam;
ee.Info:= lParam;
// Checking if this message is not from us
if ee.Handle <> Handle then WmMyMessage(ee);
end
else inherited DefaultHandler(Message);
end;
end;
procedure TForm1.WmMyMessage(var Msg: TWMMYMessage);
begin
startbtnclick (nil) // here we actually do something when the message is received
end;
Upvotes: 2
Reputation: 37566
Read How to send and handle a custom Windows message
Untested Code:
const
WM_REFRESHSOMETHING = WM_USER + 6;
On the Form which should catch the Message you need something like:
...
procedure WMRefreshsomething(var ppbMsg : TMessage); message WM_REFRESHSOMETHING;
...
procedure YourForm.WMRefreshsomething(var ppbMsg : TMessage);
begin
//Actions...
end;
And then you can send a message like:
SendMessageToAll(Handle, WM_REFRESHSOMETHING, 0, 0);
To all Forms which have the message WM_REFRESHSOMETHING;
See this compilable Example
Upvotes: 2