A question about windows api DestroyWindow - windows

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.

Related

WM_PAINT while waiting for server response

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.

Determine the origin of a WM_CLOSE message

I have an app that prompts the user for confirmation on exit, which is (indirectly) done by opening a confirmation dialog when the WM_CLOSE message is received. However the message is also sent by the task manager when the user uses end task on the process.
Because I prevent the window from closing with the confirmation dialog, task manager sees it as the process hanging and forcefully terminates it preventing the app from doing a graceful exit when it's possible.
Is there any way to determine if it's the user closing the window from the message (e.g. clicking the close button, or Alt+F4), and not the task manager? As the message's parameters are unused the only way I could think of is checking for mouse events or WM_SYSCOMMAND that were received some time before the WM_CLOSE is received, but I'm unsure if this would handle all cases.
I assume the task manager calls EndTask. Perhaps in the old days it only sent WM_CLOSE but it seems to send WM_SYSCOMMAND when I tested now so there are no clues in the messages you can use, it just looks like a normal Alt+F4.
If your app is asking about unsaved changes in a document, I would say, put up the dialog regardless and just accept the hard termination if that is what the user wants.
If you are just trying to prevent an accidental close, call GetForegroundWindow in WM_CLOSE. If you are not foreground, it was not an accidental close.

Did SendMessage has sent custom message?

I'm using code below to send message to main thread dialog window:
SendMessage(AuthMsgHWND,WM_AUTHORISE_MESSAGE, (WPARAM)&params,0);
Because of unknown reason sometimes main window didn't get message and SendMessage imediatly returns. Is it possible somehow to know was SendMessage successful?

Can I determine which process sent my window a message?

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).

How can I determine whether a message type is sent or posted?

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.

Resources