PostMessage

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.

Syntax


BOOL PostMessageA(
  [in, optional] HWND hWnd,
  [in]           UINT Msg,
  [in]           WPARAM wParam,
  [in]           LPARAM lParam
);

BOOL PostMessageW(
  [in, optional] HWND hWnd,
  [in]           UINT Msg,
  [in]           WPARAM wParam,
  [in]           LPARAM lParam
);
                

Parameters

Parameter Description
hWnd A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)-65535), the message is sent to all top-level windows in the system (excluding disabled top-level windows). The system uses the calling thread's current Unicode character set to translate the Unicode characters in the character set if there is a difference between the Unicode character sets of the sending and receiving threads.

If this parameter is NULL, the message is posted to the calling thread's message queue.
Msg The message to be posted. For a list of the system-provided messages, see System Defined Messages.
wParam Additional message information. The contents of this parameter depend on the value of the Msg parameter.
lParam Additional message information. The contents of this parameter depend on the value of the Msg parameter.

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 PostMessage function posts the message to the thread's message queue and returns immediately. The message is processed when a thread retrieves the message from its message queue by calling the GetMessage or PeekMessage function.

If the hWnd parameter specifies a window created by another thread, the function posts the message to the message queue of the thread that created the window. The system uses the calling thread's current Unicode character set to translate the Unicode characters in the character set if there is a difference between the Unicode character sets of the sending and receiving threads.

If the hWnd parameter is NULL, the message is posted to the calling thread's message queue.

If hWnd is a handle to a nonexistent window, or if the thread or process that owns the window cannot be found, the function returns zero.

Note Do not use PostMessage to send messages intended for the current thread. Use SendMessage instead. PostMessage is designed for inter-thread communication where the sender does not need to wait for the receiver to process the message.
Important If you are posting a message that includes a pointer, you must ensure that the receiving thread's message queue is processed before the memory pointed to by the pointer is deallocated. Otherwise, the receiving thread may access invalid memory.

Examples

The following example posts a custom user-defined message to a window.


// Define a custom message (ensure it doesn't conflict with system messages)
#define WM_MY_CUSTOM_MESSAGE (WM_USER + 1)

// ... somewhere in your code ...

HWND hWndTarget = FindWindow(NULL, L"My Target Window"); // Example: find a window by title
if (hWndTarget != NULL) {
    // wParam and lParam can hold various data depending on your message definition
    WPARAM wparam = 100;
    LPARAM lparam = 200;

    if (PostMessage(hWndTarget, WM_MY_CUSTOM_MESSAGE, wparam, lparam)) {
        // Message posted successfully
        OutputDebugString(L"WM_MY_CUSTOM_MESSAGE posted successfully.\n");
    } else {
        // Failed to post message
        DWORD error = GetLastError();
        OutputDebugStringW(wprintf(L"Failed to post message. Error code: %lu\n", error));
    }
} else {
    OutputDebugString(L"Target window not found.\n");
}
                

Requirements

SDK Header
Windows 2000 Professional winuser.h
Windows XP winuser.h
Windows Server 2003 winuser.h
Windows Vista winuser.h
Windows 7 winuser.h
Windows Server 2008 winuser.h
Windows Server 2008 R2 winuser.h
Windows 8 winuser.h
Windows Server 2012 winuser.h
Windows 8.1 winuser.h
Windows Server 2012 R2 winuser.h
Windows 10 winuser.h
Windows Server 2016 winuser.h
Windows 11 winuser.h
Windows Server 2019 winuser.h
Windows Server 2022 winuser.h