PostMessage Function

The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

To post a message in the message queue associated with a thread, use the PostThreadMessage function.

Syntax

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

Parameters

hWnd
[in] 

Microsoft Windows Vista and later. Message posting is subject to User Interface Privilege Isolation (UIPI). The thread of a process can post messages only to message queues of threads in processes of lesser or equal integrity level.

Handle to the window whose window procedure is to receive the message. The following values have special meanings.

HWND_BROADCAST
The message is posted to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows. The message is not posted to child windows.
NULL
The function behaves like a call to PostThreadMessage with the dwThreadId parameter set to the identifier of the current thread.
Msg
[in] Specifies the message to be posted.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.

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

Microsoft Windows Vista and later. When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied).

Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage function.

Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

The system only does marshalling for system messages (those in the range 0 to (WM_USER-1)). To send other messages (those >= WM_USER) to another process, you must do custom marshalling.

If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters cannot include pointers. Otherwise, the operation will fail. The functions will return before the receiving thread has had a chance to process the message and the sender will free the memory before it is used.

Do not post the WM_QUIT message using PostMessage; use the PostQuitMessage function.

There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources. To adjust this limit, modify the following registry key.

HKEY_LOCAL_MACHINE
     SOFTWARE
          Microsoft
               Windows NT
                    CurrentVersion
                         Windows
                              USERPostMessageLimit
The minimum acceptable value is 4000.

Windows 95/98/Me: PostMessageW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Examples

The following example shows how to post a private window message using the PostMessage function. Assume you defined a private window message called WM_COMPLETE:

#define        WM_COMPLETE     (WM_USER + 0)

You can post a message to the message queue associated with the thread that created the specified window as shown below:

 WaitForSingleObject (pparams->hEvent, INFINITE) ;
 lTime = GetCurrentTime () ;
 PostMessage (pparams->hwnd, WM_COMPLETE, 0, lTime);

For more examples, see Initiating a Data Link.

Function Information

Minimum DLL Versionuser32.dll
HeaderDeclared in Winuser.h, include Windows.h
Import libraryUser32.lib
Minimum operating systems Windows 95, Windows NT 3.1
UnicodeImplemented as ANSI and Unicode versions.

See Also

Tags :


Community Content

dmex
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function PostMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As Integer End Function
Tags : vb.net syntax

dmex
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int PostMessage(HandleRef hwnd, int msg, IntPtr wparam, IntPtr lparam);
Tags :

kristo99
its message parameters can include pointers
You can include pointers as parameters. Cast them as an integer and do not dispose or free them on the thread or function that fires the PostMessage, but dispose or free them on the receiving thread or function.
Just make sure that when using a pointer, that you only use one pointer type for one message, don't mix pointer to different objects with the same message.

Page view tracker