[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, String lParam);
//Also can add 'ref' or 'out' ahead 'String lParam'
// -- Do not use 'out String', use '[Out] StringBuilder' instead and initialize the string builder
// with proper length first. Dunno why but that is the only thing that worked for me.
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Function SendMessage( _
ByVal hWnd As HandleRef, _
ByVal Msg As UInteger, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As IntPtr
End Function
VB Signature:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Common VB Overloads:
Declare Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
ByVal wParam As IntPtr, ByRef lParam As StringBuilder) As IntPtr
Declare Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
ByVal wParam As IntPtr, ByRef RECT As IntPtr) As IntPtr
Declare Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer,
ByVal wParam As IntPtr, ByRef POINT As IntPtr) As IntPtr
2) NEVER use "int" or "integer" as lParam. Your code WILL crash on 64-bit windows. ONLY use IntPtr, a "ref" structure, or an "out" structure.
3) NEVER use "bool", "int", or "integer" as the return value. Your core WILL crash on 64-bit windows. ONLY use IntPtr. It's not safe to use bool - pInvoke cannot marshal an IntPtr to a boolean.
4) Use "IntPtr" as the return value, EVEN if the message says it doesn't return any useful information.
5) "msg" is always a 4-byte integer (do not use IntPtr). It is safe to use both "int" and "uint" (32-bit only), but Microsoft technically defines "msg" as a "double word" - which is the same as "Uint32". A listing of common Msg codes can be found here: http://www.vbcode.com/Asp/showsn.asp?theID=11797
6) You can replace "hWnd" with "IntPtr" instead of "HandleRef". However, you are taking a risk in doing this - it may cause your code to crash with race conditions - .NET can and will dispose your window handles out from under your message causing all sorts of nasty problems!
::: Question, for someone more knowledgable than me: Can't this last issue be addressed with marshaling, specifically pinning? Answer: You can use GC.KeepAlive() right after the SendMessage() with the Form object as the parameter to KeepAlive().
7) When passing integer values to lParam and wParam, use IntPtr as they get <MarshalAs(UnmanagedType.SysInt)> attributes by default. You should avoid mixing IntPtr and Integer as parameter types. Use IntPtr.
Tips & Tricks:
1) As the number of messages are varied, just keep overloading the function as you need.
2) C# - SendMessage() to Turn the Screensaver On or Off :
[DllImport("User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
public const Int32 WM_SYSCOMMAND = 0x112;
public const Int32 SC_SCREENSAVE = 0xF140;
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
TODO - a short description
16/03/2007 14:32:35 - -80.202.213.210
Click to read this page
23/03/2008 20:11:34 - Forex course-58.22.101.123
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
An IntPtr is a pointer to a memory location (unmanaged) that adapts to the platform it is running on (64-bit, etc.) UNLIKE a standard int/Integer. You should always use this type for unmanaged calls that require it, even though an int will appear to work on your development machine.
13/01/2008 11:00:13 - tsahi-62.219.227.88
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" in the upper right corner and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).
Profile your .NET code
Boost the performance of your application with ANTS Profiler
This download also contains the FREE PInvoke.net Visual Studio Add-in