I have a window, and I am looking at the messages that are being sent to it using Spy++. Is it possible to determine which process sent my window a given message, if it indeed was sent from another process?
No, Win32 window messages do not contain information about where they were sent from (unless that information is specifically included as part of the message WPARAM and/or LPARAM components).
Related
It seems to me, WM_PAINT messages are send to a window while a thread with this window is waiting for a synchronous response of the COM server. The message appears after a while, or when other actions of user appear. I would like to change background color of the window in this case, but how can I find out, if WM_PAINT is sent as a result of blocking COM server, and not just send in other cases?
I tried to expect a flag while painting, maybe in PAINTSTRUCT, but there I found anything. I need another API call to check this condition.
I want to konw if there is a message pump in DestroyWindow,and if the message WM_DESTROY will be processed before DestroyWindow return.
This is my test code:
And the message posted before DestroyWindow still remains in the message queue when the WM_DESTROY is processed:
enter image description here
but when DestroyWindow return,the message posted before DestroyWindow will be removed:
enter image description here
There is no message pump in DestroyWindow. I submit that it is pretty obvious that any messages destined to a window that's being destroyed are dropped from the queue when the window is destroyed. Even in Windows 3.1, the alternative would be unthinkable because you can't know if another program's window is about to be destroyed. How much more so now when we have pre-emptive threading now.
My app1 PostMessage WM_LBUTTONDOWN and WM_LBUTTONUP to app2 (third-party) which is in different process.
How to make sure those message been handled by app2 in app1, the logic in app1 depends on the result of those messages after PostMessage.
Here's the pseudo code for app1
PostMessage(app2Handle, WM_LBUTTON_DOWN, 0, lParam);
PostMessage(app2Handle, WM_LBUTTON_UP, 0, lParam);
// How to ensure above messages has been handled by app2 here?
PostMessage() works asynchronously. It simply puts the message into the target window's message queue and then exits immediately. There is no notification when the message is processed. If you need to know that, you could try using a message hook from SetWindowsHookEx() to monitor the activity of the target window's message queue and/or window procedure. Or, maybe you could use SetWinEventHook() to receive events like EVENT_OBJECT_INVOKED, EVENT_OBJECT_SELECTION..., etc if the mouse messages are meant to cause such click/selection actions in the window.
I am trying to send data from one app to another using WM_COPYDATA. Both apps are console and have no window. I can send user messages just fine. When I try to send WM_COPYDATA, and setup the data structure or not, I get error 1159, which basically says I have to send using a synchronous message call... yet there is no SendThreadMessage.
It seems this is a oversight in the api or docs? There seems to be no way to use WM_COPYDATA using only threads without windows?
WM_COPYDATA can only be sent and cannot be posted. Because the payload is marshaled between processes, temporary data structures are created to support that marshaling. They need to be destroyed when the message processing is complete. That implies that the message must be delivered synchronously.
All of this means that you cannot use PostThreadMessage. Instead you will need to create a window to act as the recipient of such messages. Note that this window can be a message-only window and does not need to be visible.
I'm aware that some messages types are sent directly to window procedures, while others are posted to a thread's message queue, but I haven't found any way to determine if a message will be sent or posted.
MSDN is half-helpful; it explained what's going on but the examples it gives are presumably not exhaustive.
Is there a definitive list of sent vs. posted messages, or a way to decide which type a message is?
Use InSendMessage or InSendMessageEx to determine if you are processing a message that was sent by a call to the SendMessage function.
And some messages are neither posted nor sent. Such is the case of WM_PAINT, WM_TIMER and a few others. They are simply returned by GetMessage when the queue of posted messages are empty.
I'm not sure what applications you are trying to hook, but if you have to ask such questions, then I/m a bit scared. Nothing is more frustrating for a developer to spend time over user-reported crashes only to find out that the cause is from some other application that is injecting misbehaving code. Tread carefully!
Also, Spy++ (tool that ships with Visual Studio) will show you which messages are posted/sent/recevied for any given live windows app.
The MSDN pages documenting each message should be considered the authoritative source for this:
The WM_LBUTTONDOWN message is posted when ...
The WM_SETFOCUS message is sent to a window after ...
etc.