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

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);

// Overloads use on toolbar/rebar
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern void SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, ref RECT lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, ref POINT lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern void SendMessage(IntPtr hWnd, uint msg, IntPtrwParam, ref TBBUTTON lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern void SendMessage(IntPtr hWnd, uint msg, IntPtrwParam, ref TBBUTTONINFO lParam);

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern void SendMessage(IntPtr hWnd, uint msg, IntPtrwParam, ref REBARBANDINFO lParam);

VB Signature:

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

' Overloads use on toolbar/rebar
<DllImport("user32.dll", CharSet=CharSet.Auto)> _
static extern void SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, RECT lParam)

<DllImport("user32.dll", CharSet=CharSet.Auto)> _
static extern IntPtr SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, POINT lParam)

<DllImport("user32.dll", CharSet=CharSet.Auto)> _
static extern void SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, TBBUTTON lParam)

<DllImport("user32.dll", CharSet=CharSet.Auto)> _
static extern void SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, TBBUTTONINFO lParam)

<DllImport("user32.dll", CharSet=CharSet.Auto)> _
static extern void SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, REBARBANDINFO lParam)

Private Declare Unicode Function SendMessageW Lib "user32" (ByVal hWnd As HandleRef, ByVal msg As Int32, ByVal wParam As IntPtr, ByRef lparam As IntPtr) As IntPtr

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

3) **IMPORTANT** The correct designation of a SendMessage is as follows:

      * Return value is the same size as a pointer (32-bit or 64-bit - use IntPtr ONLY)
      * "msg" is always a 4-byte integer (can be unsigned or signed - internally microsoft designates it as a "uint32"
      * "wParam" and "lParam" are ALWAYS the size of a pointer (use IntPtr ONLY - DO NOT USE "Int" or "Integer"!!)
      * Do not overload the return value as a boolean, since pInvoke cannot marshal an IntPtr to a boolean.

Tips & Tricks:

Please add some more!

Sample Code:

//C#
//This sample 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); // use the imutable singelton IntPtr.Zero instead

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

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

     //Get the text of the window/control t
     retValue =  SendMessage(hWnd,WM_GETTEXT,txtLength,sb); // must be txtLength + 1

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

Sample Code:

// C#
// This sample handles the WM_GETTEXTLENGTH and WM_GETTEXT
// messages in a standard WinForms Windows application.
// Use this code in any class that directly or indirectly
// derives from the Component class.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
     switch (m.Msg)
     {
     case WM_GETTEXTLENGTH:
         if (null != GetText())
         {
         m.Result = (IntPtr)GetText().Length;
         }
         else
         {
         m.Result = (IntPtr)0;
         }
         break;

     case WM_GETTEXT:
         String s = GetText();
         if (null != s)
         {
         int len = s.Length;
         Marshal.Copy(s.ToCharArray(), 0, m.LParam, Math.Min((int)m.WParam, len));
         m.Result = (IntPtr)len;
         }
         else
         {
         m.Result = (IntPtr)0;
         }
         return;

     // Pass any other messages to our base class
     default:
         base.WndProc(ref m);
         break;
     }
}

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