1

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

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I have . but it's not clear for me . i need a practical example.
That won't help for input. Input is special.
@O.D. That's very nice and all, but it doesn't address the question which was about faking input.
Also it should be in the WM_APP range and you've not tackled marshalling data across the process boundary.
@David, the question that was asked was not about faking input. The question asked how to handle a message.
|
2

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.