A customer was writing diagnostic code to monitor another application. They found that under certain conditions (which they can detect by other means), the program would stop responding to incoming posted messages, though it would respond to incoming sent messages.¹ Is there version of PostÂMessage
that puts the message in the message queue and either waits for the message to be retrieved (like SendÂMessage
) or calls you back when the retrieval occurs (like SendÂMessageÂCallback
)?
No, there is no such version. When a message is posted, it is placed in destination queue, and that’s the end of it. There is no further tracking of the message.
If you can change the target program, you can post it a custom message like WM_
and implement the handler for that message by posting an acknowledgement back.
// Monitoring program posts a heartbeat PostMessage(hwndAppBeingMonitored, WM_HEARTBEAT, (WPARAM)hwndMonitor, MAKELPARAM(replyMessage, replyMessageData)); // Program being monitored replies with a heartbeat case WM_HEARTBEAT: PostMessage((HWND)wParam, LOWORD(lParam), HIWORD(lParam), 0);
If you cannot make changes to the app being monitored, you could install a WH_
hook onto the UI thread of the app you are monitoring, post the window a harmless message like WM_
, and have your hook respond when it sees that message.
Another option is to post a harmless message that has some observable effect, like WM_
and then use accessibility or UI Automation to detect when the menu appears. (You could also use accessibility or UI Automation to dismiss the menu.)
¹ This can happen if the thread is inside its own SendÂMessage
call to another thread. While waiting for SendÂMessage
to complete, the thread can process inbound sent messages, but inbound posted messages go into the queue and are not processed until the thread calls GetÂMessage
or PeekÂMessage
.
0 comments