1
votes

I am trying to send a message using myresult=SendMessage(hWnd,...). As I understand it, SendMessage sends a Windows message to the specified target hWnd; the target's WndProc returns a long value; that value is returned by SendMessage (myresult). Am I right so far?

Ok, now if the SendMessage hWnd is HWND_BROADCAST, it is sent to all top-level windows. So which window's WndProc determines the return value?

UPDATE: 02-10-20
Just to clear up what I hoped to do...
My app consists of a small constellation of windows apps that need to communicate via Windows Messages. My plan was to broadcast a registered message, e.g., WM_IDENTIFYMYAPP, and identify all siblings by responses. That won't for the reason explained in the answer by Adrian Mole.

1
There's really only a handful or two of messages that are designed to be broadcast. Among them are WM_SYSCOLORCHANGE and WM_DEVMODECHANGE, i.e. messages that are intended to be notifications. I don't know what ultimately decides the return value of SendMessage, but have not ever found it to be useful for anything either. - IInspectable
The return value will be that from whatever is the last of the receiving windows to process the message and return. - Adrian Mole
Are you trying to inject code into every single process in the system that has a window? That's the only reason I can think of for using HWND_BROADCAST except for the notification-style messages mentioned by IInspectable (and DDE, which no one should be using these days). - Jonathan Potter
See my updated question for my intentions - DontPanic

1 Answers

2
votes

The return value will be that from whatever is the last of the receiving windows to process the message and return; which window this is is difficult (if not impossible) for the sending application to determine.

Generally, when you want to use HWND_BROADCAST to notify multiple windows (or to communicate with a window to which you don't have a handle), you should use the (asynchronous) PostMessage function (which returns immediately) rather than SendMessage (which waits for all receiving windows to process the message).

Using the (synchronous) SendMessage with HWND_BROADCAST as the target can cause many undesirable effects, as discussed here on Stack Overflow. But note, there are some message that cannot be sent asynchronously (via PostMessage) - WM_COPYDATA is one that I know of.