Reputation: 64730
I have a thread that calls ::PostMessage(hWnd, [...]);
to send a message to the main thread alerting it to the results of an async operation.
However, I'm concerned that if the thread takes a particularly long time to finish its operation, the hWnd
may not exist when the PostMessage
is called (the user may have closed the window).
The MSDN Documentation doesn't say anything about the results if hWnd
is invalid.
Do you know from experience, or other documentation, about what I can expect if hWnd
is invalid?
Upvotes: 1
Views: 1926
Reputation: 598309
As others have pointed out, Ramond Chen explained what happens if the HWND
gets re-used by a new window. PostMessage()
will succeed, it will just go to the wrong window. However, in cases where the HWND
does not get re-used, PostMessage()
will fail with an ERROR_INVALID_WINDOW_HANDLE
(1400) error code.
Upvotes: 1
Reputation: 283921
Raymond Chen wrote about this:
Some choice excerpts:
It so happens that boatloads of programs (and "boatloads" is a technical term) contain bugs where they use window handles after the window has been destroyed. When a window handle is re-used, that program sends a message to the window it thinks is still there, but instead it sends the message to a completely unrelated window. This doesn't bode well for the program, and it usually doesn't bode well for the new window that received the message by mistake either.
We left off our story last time by raising the problem of programs that send messages to windows that have already been destroyed and how window handle re-use exacerbates the problem. Although this is clearly a bug in the programs that use window handles after destroying the window, the problem is so widespread that the window manager folks in Windows NT decided to take a more proactive approach.
Upvotes: 3