Recent

Author Topic: Broadcast message  (Read 2265 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Broadcast message
« on: January 31, 2021, 09:31:04 pm »
Hi All,

how can I send a message to all running applications?

I've tried this and it doesn't work

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Windows, StdCtrls;
  9.  
  10.  
  11. const
  12.   WM_MYMSG = WM_USER + 101;
  13.  
  14. type
  15.  
  16.   { TForm1 }
  17.  
  18.   TForm1 = class(TForm)
  19.     Button1: TButton;
  20.     procedure Button1Click(Sender: TObject);
  21.   private
  22.  
  23.   public
  24.     procedure WMMymsg(var Msg: TMessage); message WM_MYMSG;
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.   PostMessage(HWND_BROADCAST, WM_MYMSG, 1, 0);
  37. end;
  38.  
  39. procedure TForm1.WMMymsg(var Msg: TMessage);
  40. begin
  41.   ShowMessage('here');
  42. end;
  43.  
  44. end.
  45.  
  46.  
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6836
Re: Broadcast message
« Reply #1 on: January 31, 2021, 10:11:10 pm »
PostMessage(HWND_BROADCAST,AMessageValue, option1, option2);

The HWND_BROADCAST handle is a system wide window  , it will send the message to all top level windows
 
The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Broadcast message
« Reply #2 on: January 31, 2021, 10:27:16 pm »
Did you look at the posted code. See line 36.

It doesn't work. If I change HWND_BROADCAST to self.handle it works, but I need to broadcast.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6836
Re: Broadcast message
« Reply #3 on: January 31, 2021, 11:26:15 pm »
that message only goes to the top level window which in this case is the Application.Handle

I see that Laz does not provide a OnMessage event to make this easier.

You need to hook the Application.Handle window..

You can't use the MsgHook unit for that you need to do it the hard way..
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 5080
Re: Broadcast message
« Reply #4 on: February 01, 2021, 12:15:08 am »
It doesn't work. If I change HWND_BROADCAST to self.handle it works, but I need to broadcast.
You need to read the documentation for the APIs PostMessage, SendMessage and RegisterWindowMessage.   There are restrictions placed on broadcasting WM_USER messages. 

In a few words, don't expect system wide broadcasting of a WM_USER message to work.  Allowing that to work would enable applications to maliciously cause problems in other applications that use user defined messages to carry out some of their functions.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 6836
Re: Broadcast message
« Reply #5 on: February 01, 2021, 12:48:07 am »
you take the fun out of it  :o

anyways I think you can send user messages because it only goes to the top level window which In most cases is not a user form but just a message handler capture window.

 But in this case its hard to get to because its in the application class and that window basically only handles known messages and ignores all others. Only apps that are special in this case for example would be interacting there.

 But who knows.. I guess you could just send a WM_POWERBROADCAST message yourself !

 There is also the BroadCastSystemMessage API call too
« Last Edit: February 01, 2021, 12:55:36 am by jamie »
The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Broadcast message
« Reply #6 on: February 01, 2021, 03:50:47 am »
ok then. How can I enumerate all top level forms and get their handles?
« Last Edit: February 01, 2021, 04:42:39 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6836
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018