SendMessageTimeout function (Windows)

Switch View :
ScriptFree
SendMessageTimeout function

Applies to: desktop apps only

Sends the specified message to one or more windows.

Syntax

LRESULT WINAPI SendMessageTimeout(
  __in       HWND hWnd,
  __in       UINT Msg,
  __in       WPARAM wParam,
  __in       LPARAM lParam,
  __in       UINT fuFlags,
  __in       UINT uTimeout,
  __out_opt  PDWORD_PTR lpdwResult
);

Parameters

hWnd [in]

Type: 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, including disabled or invisible unowned windows. The function does not return until each window has timed out. Therefore, the total wait time can be up to the value of uTimeout multiplied by the number of top-level windows.

Msg [in]

Type: UINT

The message to be sent.

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

wParam [in]

Type: WPARAM

Any additional message-specific information.

lParam [in]

Type: LPARAM

Any additional message-specific information.

fuFlags [in]

Type: UINT

The behavior of this function. This parameter can be one or more of the following values.

ValueMeaning
SMTO_ABORTIFHUNG
0x0002

The function returns without waiting for the time-out period to elapse if the receiving thread appears to not respond or "hangs."

SMTO_BLOCK
0x0001

Prevents the calling thread from processing any other requests until the function returns.

SMTO_NORMAL
0x0000

The calling thread is not prevented from processing other requests while waiting for the function to return.

SMTO_NOTIMEOUTIFNOTHUNG
0x0008

The function does not enforce the time-out period as long as the receiving thread is processing messages.

SMTO_ERRORONEXIT
0x0020

The function should return 0 if the receiving window is destroyed or its owning thread dies while the message is being processed.

 

uTimeout [in]

Type: UINT

The duration of the time-out period, in milliseconds. If the message is a broadcast message, each window can use the full time-out period. For example, if you specify a five second time-out period and there are three top-level windows that fail to process the message, you could have up to a 15 second delay.

lpdwResult [out, optional]

Type: PDWORD_PTR

The result of the message processing. The value of this parameter depends on the message that is specified.

Return value

Type:

Type: LRESULT

If the function succeeds, the return value is nonzero. SendMessageTimeout does not provide information about individual windows timing out if HWND_BROADCAST is used.

If the function fails or times out, the return value is 0. To get extended error information, call GetLastError. If GetLastError returns ERROR_TIMEOUT, then the function timed out.

Windows 2000:  If GetLastError returns 0, then the function timed out.

Remarks

The function calls the window procedure for the specified window and, if the specified window belongs to a different thread, does not return until the window procedure has processed the message or the specified time-out period has elapsed. If the window receiving the message belongs to the same queue as the current thread, the window procedure is called directly—the time-out value is ignored.

This function considers that a thread is not responding if it has not called GetMessage or a similar function within five seconds.

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.

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

SendMessageTimeoutW (Unicode) and SendMessageTimeoutA (ANSI)

See also

Reference
GetMessage
InSendMessage
PostMessage
SendDlgItemMessage
SendMessage
SendMessageCallback
SendNotifyMessage
Conceptual
Messages and Message Queues

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Community Content

zedubal
Unicode or ANSI strings for LPARAM
From my testing it seems that if you are calling SendMessageTimeout and passing the string "Environment" according to this description you should actually pass L"Environment" if you are calling SendMessageTimeoutW and "Environment" if you are calling SendMessageTimoutA. Is that correct? Can someone please clarify this in the description?

JOUE
HELLO
HI I AMA A XBOX 36O LIVE PLAYER I HAVE A BIG PARBLIM'S
IN
(CALL OF DUTY WAW) THE PEAPOL THAT THEAY PLAY THIS GAME ALL OF THEAM HACKERS ANA I HOPE THAT YOU CAN MACKE AN UPDAIT PLEAS
THANK YOU .....

ElmueSoft
!Important note missing!
Can you please add this important note :

SMTO_ERRORONEXIT:
ATTENTION: This falg is available only on Windows Vista and higher.
If you use SMTO_ERRORONEXIT on Windows XP SP3 or older, SendMessageTimeout will return ERROR_INVALID_PARAMETER


KJK_Hyperion
Alternate purpose for SendMessageTimeout
Want to use SendMessage but don't want to give the calling thread a message queue? use SendMessageTimeout with SMTO_BLOCK and MAXINT milliseconds of timeout

KJK_Hyperion
Maximum timeout is not INFINITE

The uTimeout argument is declared as UINT, but it seems to actually be an INT internally, because timeouts larger than MAXINT (0x7fffffff, or a timeout of over 24 years and 10 months) are treated like 0. Bottom line, you can't use INFINITE for uTimeout (tested on Windows Vista). 24 years and 10 months (MAXINT) should be infinite enough for most purposes, tough


The MAZZTer
Purpose
Use SendMessageTimeout when sending messages to windows you do not control, when you need a response. If you use SendMessage, your program will hang if the window you sent to is hung.

Use PostMessage if you don't care about the return value.

Simon Said
SMTO_ERRORONEXIT Flag - Description missing
No description has been provided for the SMTO_ERRORONEXIT flag.

Ciaran Gultnieks
Bad description & typo
On top of the fact that the main description is vague and doesn't describe the purpose of SendMessageTimeout at all, it says "Sends the specified message to one of more windows" when surely it means "Sends the specified message to one OR more windows"