SendMessage function (Windows)

Switch View :
ScriptFree
SendMessage function

Applies to: desktop apps only

Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.

Syntax

LRESULT WINAPI SendMessage(
  __in  HWND hWnd,
  __in  UINT Msg,
  __in  WPARAM wParam,
  __in  LPARAM lParam
);

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, overlapped windows, and pop-up windows; but the message is not sent to child windows.

Message sending is subject to UIPI. The thread of a process can send messages only to message queues of threads in processes of lesser or equal integrity level.

Msg [in]

Type: UINT

The message to be sent.

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: LRESULT

The return value specifies the result of the message processing; it depends on the message sent.

Remarks

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

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 the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use SendMessageTimeout with SMTO_BLOCK set. For more information on nonqueued messages, see Nonqueued Messages.

Examples

For an example, see Displaying Keyboard Input.

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

SendMessageW (Unicode) and SendMessageA (ANSI)

See also

Reference
InSendMessage
PostMessage
PostThreadMessage
RegisterWindowMessage
SendDlgItemMessage
SendMessageCallback
SendMessageTimeout
SendNotifyMessage
Conceptual
Messages and Message Queues

 

 

Send comments about this topic to Microsoft

Build date: 2/3/2012

Community Content

Valera_
Remarks
"Remarks When a message is blocked by UIPI the last error, retrieved with GetLastError, is set to 5 (access denied)." When I use ChangeWindowMessageFilterEx SendMessage successfully send to elevated process, but GetLastError not changed from previous state (

sqykly
SendMessageW in Win7
I have also had a problem with calling SendMessageW() in win7 from a winxp application.  Goes like this:

CHAR szOtherThing[512];
WCHAR wszDomain[256];
HWND hDomainEdit;
HWND hOtherEdit;
HWND hButton;

#define DOMAIN_EDIT_ID 1019
#define OTHER_EDIT_ID 1020
#define OK_BUTTON_ID 1021

BOOL DialogProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_INIT) {
InitComCtl32();
hDomainEdit = GetDlgItem(hDlg, DOMAIN_EDIT_ID);
hOtherEdit = GetDlgItem(hDlg, OTHER_EDIT_ID);
hButton = GetDlgItem(hDlg, OK_BUTTON_ID);
/*...*/
return FALSE;
} else if (msg == WM_COMMAND) {
WORD wNotifyCode = HIWORD(wParam);
WORD wId = LOWORD(wParam);
if (wId == OK_BUTTON_ID && wNotifyCode == BN_CLICKED) {
int length = (int) SendMessageA(hOtherEdit, WM_GETTEXT, (WPARAM) 512, (LPARAM) szOtherThing);
/* length is non-zero in WinXP and Win7 */
length = (int) SendMessageW(hDomainEdit, WM_GETTEXT, (WPARAM) 256, (LPARAM) wszDomain);
/* length is non-zero in WinXP, zero in Win7!  Same input!  Error find?  Medal get? */
/* ... think about doing other stuff, but can't without a domain name ... */
}
} // don't process other messages, just out of spite.  just kidding, it's for brevity
}
So what do?  Is unicode finally out of fashion? 

amouna123
0793250288
slt

amouna123
0793250288
slt

mandela admasu
+251913439608
Hey men$0

--Fragman--
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.


e1337
RE: Kind of Messages
Message Required Long. A hexidecimal number that corresponds to the message you want to send. If you have the Microsoft Platform Software Development Kit, you can look up the name of the message in the header files (Winuser.h, for example) to find the associated hexadecimal number (precede the hexidecimal value with &h).

Taken from:
http://msdn.microsoft.com/en-us/library/aa220811%28office.11%29.aspx

Bart Burkhardt
On Vista, Windows 7, how to send a string?
I made a service that sends a string to a systray winform app. This works fine on a Windows XP system and where the service is allowed to interact with the deskop.

I tested it on a Windows 7 system and it fails now. I tried to use HWND_BROADCAST with RegisterWindowMessage but get Exception in the revieve part.

Here the receive part in the Winform;

if (m.Msg == this.msgId)
{
MessageUtil.COPYDATASTRUCT mystr = new MessageUtil.COPYDATASTRUCT();
Type mytype = mystr.GetType();
mystr = (MessageUtil.COPYDATASTRUCT)m.GetLParam(mytype);
handleMessageCommand(mystr.lpData);
}


The exception I get is as follows;

Exception of type 'System.ExecutionEngineException' was thrown.

This happens at following line;
mystr = (MessageUtil.COPYDATASTRUCT)m.GetLParam(mytype);


Any ideas?

TENware
VB .NET (2005) Syntax

Imports System.Runtime.InteropServices


<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
'Msg is declared as Integer because the Message Structure's Msg property is Integer type.


Philipp Schwesig
C# Syntax
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

JustGreat
Where to find constants ?
Thanks for the link showing where to find messages, but can anyone tell us where to find constant's meaning ?
ok, in this link http://www.vbcode.com/Asp/showsn.asp?theID=11797 we can find the values of each constant but what does each constant mean ?

Thomas Lee
Emulate WM_TOUCH messages

Hi,
I would like to know if it is possible to emulate WM_TOUCH messages by using sendmessage.
Thanks

[tfl - 03 08 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at

http://www.microsoft.com/communities/newsgroups/en-us/ . You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C &
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C &
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C &


keych
Where to find messages