212 out of 1595 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: 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.

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

Minimum supported server

Windows 2000 Server

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

 

 

Send comments about this topic to Microsoft

Build date: 9/11/2011

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
SIGN IN PROBLEM
Dear sir, $0My Name is Shahbaz Guul. My msn live in box has been blocked.$0 $0My previous email address: shahbaz_guul@hotmail.com and its password was harkonens contains allot of my private stuff including my M.Sc thesis please help me because I don't have any back up copy in my PC. $0 $0And I also forgot the answer of my private security question. Please sir its about my thesis work.$0 $0according to my friends we are getting allot of spam email from your ID.$0 $0I will be very thankful for this kindness from you.$0 $0Sincerely,$0 $0shahbaz gul.$0
UIPI Workaround

There actually is an official workaround to UIPI if you are developing the elevated application:

Calling ChangeWindowMessageFilter() in the elevated process allows to define messages that can be sent to the elevated process.

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.