Lazarus

Programming => General => Topic started by: Ericktux on November 30, 2020, 04:42:55 am

Title: PostMessage example
Post by: Ericktux on November 30, 2020, 04:42:55 am
Hello friends, please where can I find an example of how to use "PostMessage" to communicate processes.

Thanks for your time.
Title: Re: PostMessage example
Post by: dbannon on November 30, 2020, 08:36:59 am
PostMessage as in httpclient ?

about line 950
https://github.com/tomboy-notes/tomboy-ng/blob/nextcloud/source/transnextcloud.pas

or maybe using SimpleIPCClient

about line 127
https://github.com/tomboy-notes/tomboy-ng/blob/master/source/cli.pas

Lots of other ways to PostMessage of course, you might need to be a bit more specific ...

Davo
Title: Re: PostMessage example
Post by: Ericktux on November 30, 2020, 03:55:43 pm
hello friend, basically what I need to know is how to communicate two processes (parent, child) with the postmessage command
Title: Re: PostMessage example
Post by: Remy Lebeau on November 30, 2020, 06:41:35 pm
hello friend, basically what I need to know is how to communicate two processes (parent, child) with the postmessage command

There are a bazillion online examples of using PostMessage() and SendMessage() for inter-process communications using window messages.  What EXACTLY do you need help with in doing this with FPC?  Can you be more specific?
Title: Re: PostMessage example
Post by: jamie on November 30, 2020, 11:01:07 pm
of course this will only work on windows. I don't think the window managers on other targets have such control ?

In any case, you need to acquire the window handle of the other process you wish to send messages to.

 I suppose if you use some sort of unique window search like EnumWindows or FindWindow etc, you could comeup with something..


 Simple example

 PostMessage(theRemoteWindowHandle, TheMessageTOSend, Param1, Param2);

Title: Re: PostMessage example
Post by: zoltanleo on December 01, 2020, 04:49:41 pm
of course this will only work on windows.
I think this is not entirely true  ;)

Hello friends, please where can I find an example of how to use "PostMessage" to communicate processes.
PostMessage function is used in very specific cases like threads. Here is the topic where it was discussed
https://forum.lazarus.freepascal.org/index.php/topic,40163.msg277135.html#msg277135

it can also be used on the main thread, for example to destroy a control or close a form
Code: Pascal  [Select][+][-]
  1. const
  2.   WM_KILLBTN_MSG = LM_USER + $101;  
  3. ...
  4.   public
  5.     procedure WmKillBtn(var Msg: TLMessage) message WM_KILLBTN_MSG;
  6.   end;
  7. ...
  8. procedure TfrmMain.ActKillBtnExecute(Sender: TObject);
  9. begin
  10.   PostMessage(Self.Handle,WM_KILLBTN_MSG,PtrInt(TButton(Sender)),0);
  11. end;
  12. ...
  13. procedure TfrmMain.WmKillBtn(var Msg: TLMessage);
  14. var
  15.   btn: TButton;
  16. begin
  17.   btn:= TButton(Msg.wParam);
  18.   if Assigned(btn) then FreeAndNil(btn);
  19. end;

Title: Re: PostMessage example
Post by: PascalDragon on December 02, 2020, 09:17:09 am
of course this will only work on windows.
I think this is not entirely true  ;)

Yes, it is true. While PostMessage itself is implemented by the LCL for communication inside the same process, it is not implemented for IPC. On Windows it directly uses the WinAPI function thus it's no problem there.
Title: Re: PostMessage example
Post by: zoltanleo on December 02, 2020, 10:08:55 am
Hi PascalDragon.

Perhaps we are talking about different things.
I wanted to say that PostMessage can be used not only for Windows. And it works great for Linux. Lazarus is a cool IDE!  ;)
Title: Re: PostMessage example
Post by: PascalDragon on December 02, 2020, 02:44:30 pm
I wanted to say that PostMessage can be used not only for Windows. And it works great for Linux. Lazarus is a cool IDE!  ;)

As I said: PostMessage itself can be used on non-Windows with the LCL, but only within the same process. On Windows you can use PostMessage to send a message to a completely different process, but only there. And since this topic is about Inter Process Communication as Ericktux already said this is an important distinction.
Title: Re: PostMessage example
Post by: Okoba on December 02, 2020, 03:39:34 pm
zoltanleo, that is a nice demo. Thanks for that.
Title: Re: PostMessage example
Post by: zoltanleo on December 02, 2020, 04:04:10 pm
And since this topic is about Inter Process Communication as Ericktux already said this is an important distinction.
OK. Now I understand what you and Ericktux meant. I apologize because my English is not very good. Therefore, I do not always immediately understand all the language subtleties.  :-[
TinyPortal © 2005-2018