Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
PeekMessage (user32)

User-Defined Types C#:

  public struct NativeMessage
      public IntPtr handle;
      public uint msg;
      public IntPtr wParam;
      public IntPtr lParam;
      public uint time;
      public System.Drawing.Point p;

User-Defined Types VB.NET:

<StructLayout(LayoutKind.Sequential)> _

    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

C# Signature:

[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PeekMessage(out NativeMessage lpMsg, HandleRef hWnd, uint wMsgFilterMin,
   uint wMsgFilterMax, uint wRemoveMsg);

VB.NET Signature:

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



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 msg = new NativeMessage();
GCHandle handle = GCHandle.Alloc(msg);
bool foundMessage = PeekMessage(ref msg, hWnd, 0, 0, 0);

Sample Code:

NativeMessage message = new NativeMessage();
    out message,
    new HandleRef(myWindow,myWindow.hWnd),

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.

( See: )

C# Signature:

  [return: MarshalAs(UnmanagedType.Bool)]
  [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern bool PeekMessage(out NativeMessage message, IntPtr handle, uint filterMin, uint filterMax, uint flags);

Sample Code:

  NativeMessage msg;
  bool foundMessage = PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);

PeekMessage 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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version