Windows API Reference

PostMessage Function

BOOL PostMessage(
  HWND hWnd,
  UINT Msg,
  WPARAM wParam,
  LPARAM lParam
);
                

The PostMessage function places (posts) a message in the message queue of the specified thread and returns without waiting for the thread to process the message.

Parameters

Parameter Type Description
hWnd HWND A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system (excluding disabled top-level windows).
Msg UINT The message to be posted. This parameter can be an application-defined message. Message values from 0 through WM_USER are available for use by applications and other programs.
wParam WPARAM Additional message-specific information.
lParam LPARAM Additional message-specific information.

Return Value

If the function successfully places the message in the message queue, the return value is nonzero.

If the function fails to place the message in the message queue, the return value is zero. To get extended error information, call GetLastError.

Remarks

The PostMessage function is used to send messages to a window or thread. Unlike SendMessage, which processes the message synchronously, PostMessage places the message in the target thread's message queue and returns immediately. The message is then processed when the thread's message loop retrieves it.

When posting a message to another process, you should be careful not to pass dangling pointers in wParam or lParam. If the target process is running on a different system, it is best to marshal any necessary data into a structure and pass a pointer to that structure, or to send data by value.

The system posts messages to the message queues of processes. For the system to deliver a message to a thread's message queue, the thread must have created a message queue. Threads create message queues by calling CreateThread or by calling GetMessage, PeekMessage, or TranslateMessage.

Requirements

Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll

See Also