PostMessage function
Posts a message to the message queue of the specified thread and returns without waiting for the thread to process the message.
Syntax
BOOL PostMessageA(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters
Parameter | Description |
---|---|
hWnd | Handle to the window whose thread will receive the message. If this parameter is NULL , PostMessage posts the message to the message queue of the calling thread. |
Msg | Message identifier. For example, WM_KEYDOWN or a custom message defined with RegisterWindowMessage . |
wParam | Additional message-specific information. The content of this parameter depends on the value of Msg . |
lParam | Additional message-specific information. The content of this parameter depends on the value of Msg . |
Return value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError
.
Remarks
- The message is placed in the message queue associated with the thread that created the specified window and then the thread is awakened.
- Unlike
SendMessage
,PostMessage
does not wait for the message to be processed. - Do not use
PostMessage
to send messages that require a response. - For cross-process communication, ensure the receiving thread has a message pump.
Example
The following example posts a WM_CLOSE
message to a window identified by its handle.
#include <windows.h>
int main()
{
HWND hWnd = FindWindowA(NULL, "Untitled - Notepad");
if (hWnd)
{
PostMessageA(hWnd, WM_CLOSE, 0, 0);
}
return 0;
}