Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SendMessage (user32)
 
.
Summary
Sends the specified message to a window or windows.

C# Signature:

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
   IntPtr lParam);

//Overload for string lParam (e.g. WM_GETTEXT)
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,    
[Out] StringBuilder lParam);

VB Signature:

Declare Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
   ByVal wParam As UIntPtr, ByVal lParam As IntPtr) As IntPtr

User-Defined Types:

None.

Notes:

1) As the number of messages are varied, just keep overloading the function as you need.

2) Use IntPtr instead of UIntrPtr: The UIntPtr type is not CLS-compliant

Tips & Tricks:

Please add some more!

Sample Code:

//C#
//This samlpe first uses SendMessage and WM_GETTEXTLENGTH
//to get the length of the text and then it uses that value
// and SendMessage with WM_GETTEXT and a StringBuilder to
//extract the text.
public static String GetWindowText(IntPtr hWnd)
{
     IntPtr txtLength;
     IntPtr retValue;
     IntPtr zeroVal = new IntPtr(0);

     //Get the length of the text
     retValue = SendMessage(hWnd,WM_GETTEXTLENGTH,zeroVal,zeroVal);

     //Setup the size of the sb
     txtLength = retValue;
     StringBuilder sb = new StringBuilder(txtLength.ToInt32() + 1);

     //Get the text of the window/control
     retValue =  SendMessage(hWnd,WM_GETTEXT,txtLength,sb);

     //Return a string
     return sb.ToString();
}

Alternative Managed API:

Do you know one? Please contribute it!..

Documentation
SendMessage on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions