Export (0) Print
Expand All
230 out of 676 rated this helpful - Rate this topic

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 WINAPI PostMessage(
  _In_opt_  HWND hWnd,
  _In_      UINT Msg,
  _In_      WPARAM wParam,
  _In_      LPARAM lParam
);

Parameters

hWnd [in, optional]

Type: HWND

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

ValueMeaning
HWND_BROADCAST
((HWND)0xffff)

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.

 

Starting with Windows Vista, message posting is subject to UIPI. The thread of a process can post messages only to message queues of threads in processes of lesser or equal integrity level.

Msg [in]

Type: UINT

The message to be posted.

For lists of the system-provided messages, see System-Defined Messages.

wParam [in]

Type: WPARAM

Additional message-specific information.

lParam [in]

Type: LPARAM

Additional message-specific information.

Return value

Type:

Type: BOOL

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. GetLastError returns ERROR_NOT_ENOUGH_QUOTA when the limit is hit.

Remarks

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.

An accessibility application can use PostMessage to post WM_APPCOMMAND messages to the shell to launch applications. This functionality is not guaranteed to work for other types of applications.

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.

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.

Requirements

Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

Unicode and ANSI names

PostMessageW (Unicode) and PostMessageA (ANSI)

See also

Reference
GetMessage
PeekMessage
PostQuitMessage
PostThreadMessage
RegisterWindowMessage
SendMessageCallback
SendNotifyMessage
Conceptual
Messages and Message Queues

 

 

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.