SendMessage (user32)
Last changed: thediscover22450@gmail.com-92.139.119.87

.
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, int msg, int wParam, ref RECT lParam);

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

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

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

[DllImport("user32.dll", CharSet=CharSet.Auto)]
static extern void SendMessage(IntPtr hWnd, int msg, int wParam, ref REBARBANDINFO 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

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

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

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

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

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

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); // use the imutable singelton IntPzr.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();
}

Alternative Managed API:

Do you know one? Please contribute it!..

Documentation
SendMessage on MSDN