Search code examples
cwinapibroadcastsendmessagepostmessage

PostMessage and HWND_BROADCAST


I'm trying to send message to all windows, by:

PostMessage( HWND_BROADCAST, 0x0401, 0, 0 );
or
SendMessage( HWND_BROADCAST, 0x0401, 0, 0 );

The problem is, that no window receives this message. Windows has own threads. The question is... how to send message to all windows in my application, or why this function doesn't work...

Thanks in advance.


Solution

  • The question is, how to send message to all windows in my application, or why this function doesn't work.

    Messages in the WM_USER range (and incidentally, you really should stop using magic constants like 0x0401) are reserved for private window classes. On the other hand HWND_BROADCAST results in the message being delivered to all top-level windows in the system.

    These two facts conflict and PostMessage knows that. If PostMessage were to do your bidding then it would, for sure, deliver bogus messages to windows that would lead to undesirable behaviour. Hence PostMessage does not deliver your message to all the top-level windows.

    Now, as mentioned, HWND_BROADCAST results in the message being delivered to all top-level windows in the system. You don't want that. You only want it to go to windows in your application. Which means that HWND_BROADCAST is not for you. If you want to deliver messages to specific windows in your application, you will have to maintain or obtain the list of windows, and deliver the messages one by one.