Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than user32, prefix the name with the module name and a period.
PeekMessage (user32)
.
User-Defined Types C#:
[StructLayout(LayoutKind.Sequential)]
public struct NativeMessage
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
Public Structure NativeMessage
Public handle As IntPtr
Public msg As UInteger
Public wParam As IntPtr
Public lParam As IntPtr
Public time As UInteger
Public p As System.Drawing.Point
End Structure
<DllImport("User32.dll", SetLastError:=True)> _
Public Shared Function PeekMessage(ByRef message As NativeMessage, ByVal handle As IntPtr, _
ByVal filterMin As UInteger, ByVal filterMax As UInteger, ByVal flags As UInteger) _
As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
User-Defined Types:
None.
Notes:
Specifying HandleRef instead of IntPtr as the hWnd type prevents the object the handle referees to from being garbage collected. All internal Microsoft PInvokes use HandleRef and not IntPtr.
Do not use System.Windows.Forms.Message for the first argument - this is a different data structure.
Tips & Tricks:
I have been seeing with this interface that occasionally PeekMessage will thrown a couple different types of exceptions.
(1) "Object reference not set to an instance of an object."
(2) "External component has thrown an exception."
As seen below, "message" is the culprit. After exception (1) occurs, all the data members of NativeMessage are zero. Even the point is initialized. After exception (2) occurs, the external dll crashes and it brings down your app. I have tried using GC.KeepAlive(message); without luck.
Tips & Tricks 2:
In an attempt to avoid the above, I've modified the declaration of the function to pass the first parameter as 'ref' instead of 'out' and allocated a handle to my NativeMessage variable using GCHandle.Alloc(), like this:
NativeMessage message = new NativeMessage();
PeekMessage(
out message,
new HandleRef(myWindow,myWindow.hWnd),
0,
0,
PM_REMOVE);
Alternative Managed API:
Based off of Tom Miller's blog. Modified to comply with VS.Net 2005 static code analysis (i.e. proper MarshalAs attributes) and eliminate the refrence to WindowMessage.
A HandleRef is essentially an IntPtr to a handle and a reference to the object the handle belongs to. Using HandleRef prevents the GC from collecting the object until the native method is done with it.
7/22/2009 9:41:44 AM - -212.251.139.186
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.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
The mechanism provided by the CLR that enables managed code to call static DLL exports.k
10/27/2022 9:24:28 PM - 114.37.143.20
A HandleRef is essentially an IntPtr to a handle and a reference to the object the handle belongs to. Using HandleRef prevents the GC from collecting the object until the native method is done with it.
7/22/2009 9:41:44 AM - -212.251.139.186
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.
1/13/2008 4:00:13 AM - Damon Carr-72.43.165.29
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).