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

PeekMessage (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern bool PeekMessage(out MSG lpMsg, HandleRef hWnd, uint wMsgFilterMin,
   uint wMsgFilterMax, uint wRemoveMsg);

User-Defined Types:

public struct POINT

{

    public Int32    x;
    public Int32    y;

}

public struct MSG

{

    public IntPtr    hwnd;
    public UInt32    message;
    public IntPtr    wParam;
    public IntPtr    lParam;
    public UInt32    time;
    public POINT    pt;
    public UInt32    lPrivate;

}

Notes:

Specifing HandleRef instead of IntPtr as the hWnd type prevents the handle from going out of scope. All internal Microsoft PInvokes use HandleRef and not IntPtr.

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

Sample Code:

MSG message = new MSG();
PeekMessage(
    out message,
    new HandleRef(myWindow,myWindow.hWnd),
    0,
    0,
    PM_REMOVE);

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
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 PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions