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

SetWinEventHook (user32)
 
.
Summary

C# Signature:

[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
   hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
   uint idThread, uint dwFlags);

VB Signature:

    <System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint:="SetWinEventHook")> _
    Private Shared Function SetWinEventHook( _
        ByVal eventMin As UInteger, _
        ByVal eventMax As UInteger, _
        ByVal hmodWinEventProc As IntPtr, _
        ByVal lpfnWinEventProc As WinEventDelegate, _
        ByVal idProcess As UInteger, _
        ByVal idThread As UInteger, _
        ByVal dwFlags As UInteger _
    ) As IntPtr
    End Function

User-Defined Types:

WinEventDelegate

C# Constants:

uint WINEVENT_OUTOFCONTEXT = 0x0000; // Events are ASYNC

uint WINEVENT_SKIPOWNTHREAD = 0x0001; // Don't call back for events on installer's thread

uint WINEVENT_SKIPOWNPROCESS = 0x0002; // Don't call back for events on installer's process

uint WINEVENT_INCONTEXT = 0x0004; // Events are SYNC, this causes your dll to be injected into every process

uint EVENT_MIN = 0x00000001;

uint EVENT_MAX = 0x7FFFFFFF;

uint EVENT_SYSTEM_SOUND = 0x0001;

uint EVENT_SYSTEM_ALERT = 0x0002;

uint EVENT_SYSTEM_FOREGROUND = 0x0003;

uint EVENT_SYSTEM_MENUSTART = 0x0004;

uint EVENT_SYSTEM_MENUEND = 0x0005;

uint EVENT_SYSTEM_MENUPOPUPSTART = 0x0006;

uint EVENT_SYSTEM_MENUPOPUPEND = 0x0007;

uint EVENT_SYSTEM_CAPTURESTART = 0x0008;

uint EVENT_SYSTEM_CAPTUREEND = 0x0009;

uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;

uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;

uint EVENT_SYSTEM_CONTEXTHELPSTART = 0x000C;

uint EVENT_SYSTEM_CONTEXTHELPEND = 0x000D;

uint EVENT_SYSTEM_DRAGDROPSTART = 0x000E;

uint EVENT_SYSTEM_DRAGDROPEND = 0x000F;

uint EVENT_SYSTEM_DIALOGSTART = 0x0010;

uint EVENT_SYSTEM_DIALOGEND = 0x0011;

uint EVENT_SYSTEM_SCROLLINGSTART = 0x0012;

uint EVENT_SYSTEM_SCROLLINGEND = 0x0013;

uint EVENT_SYSTEM_SWITCHSTART = 0x0014;

uint EVENT_SYSTEM_SWITCHEND = 0x0015;

uint EVENT_SYSTEM_MINIMIZESTART = 0x0016;

uint EVENT_SYSTEM_MINIMIZEEND = 0x0017;

uint EVENT_SYSTEM_DESKTOPSWITCH = 0x0020;

uint EVENT_SYSTEM_END = 0x00FF;

uint EVENT_OEM_DEFINED_START = 0x0101;

uint EVENT_OEM_DEFINED_END = 0x01FF;

uint EVENT_UIA_EVENTID_START = 0x4E00;

uint EVENT_UIA_EVENTID_END = 0x4EFF;

uint EVENT_UIA_PROPID_START = 0x7500;

uint EVENT_UIA_PROPID_END = 0x75FF;

uint EVENT_CONSOLE_CARET = 0x4001;

uint EVENT_CONSOLE_UPDATE_REGION = 0x4002;

uint EVENT_CONSOLE_UPDATE_SIMPLE = 0x4003;

uint EVENT_CONSOLE_UPDATE_SCROLL = 0x4004;

uint EVENT_CONSOLE_LAYOUT = 0x4005;

uint EVENT_CONSOLE_START_APPLICATION = 0x4006;

uint EVENT_CONSOLE_END_APPLICATION = 0x4007;

uint EVENT_CONSOLE_END = 0x40FF;

uint EVENT_OBJECT_CREATE = 0x8000; // hwnd ID idChild is created item

uint EVENT_OBJECT_DESTROY = 0x8001; // hwnd ID idChild is destroyed item

uint EVENT_OBJECT_SHOW = 0x8002; // hwnd ID idChild is shown item

uint EVENT_OBJECT_HIDE = 0x8003; // hwnd ID idChild is hidden item

uint EVENT_OBJECT_REORDER = 0x8004; // hwnd ID idChild is parent of zordering children

uint EVENT_OBJECT_FOCUS = 0x8005; // hwnd ID idChild is focused item

uint EVENT_OBJECT_SELECTION = 0x8006; // hwnd ID idChild is selected item (if only one), or idChild is OBJID_WINDOW if complex

uint EVENT_OBJECT_SELECTIONADD = 0x8007; // hwnd ID idChild is item added

uint EVENT_OBJECT_SELECTIONREMOVE = 0x8008; // hwnd ID idChild is item removed

uint EVENT_OBJECT_SELECTIONWITHIN = 0x8009; // hwnd ID idChild is parent of changed selected items

uint EVENT_OBJECT_STATECHANGE = 0x800A; // hwnd ID idChild is item w/ state change

uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B; // hwnd ID idChild is moved/sized item

uint EVENT_OBJECT_NAMECHANGE = 0x800C; // hwnd ID idChild is item w/ name change

uint EVENT_OBJECT_DESCRIPTIONCHANGE = 0x800D; // hwnd ID idChild is item w/ desc change

uint EVENT_OBJECT_VALUECHANGE = 0x800E; // hwnd ID idChild is item w/ value change

uint EVENT_OBJECT_PARENTCHANGE = 0x800F; // hwnd ID idChild is item w/ new parent

uint EVENT_OBJECT_HELPCHANGE = 0x8010; // hwnd ID idChild is item w/ help change

uint EVENT_OBJECT_DEFACTIONCHANGE = 0x8011; // hwnd ID idChild is item w/ def action change

uint EVENT_OBJECT_ACCELERATORCHANGE = 0x8012; // hwnd ID idChild is item w/ keybd accel change

uint EVENT_OBJECT_INVOKED = 0x8013; // hwnd ID idChild is item invoked

uint EVENT_OBJECT_TEXTSELECTIONCHANGED = 0x8014; // hwnd ID idChild is item w? test selection change

uint EVENT_OBJECT_CONTENTSCROLLED = 0x8015;

uint EVENT_SYSTEM_ARRANGMENTPREVIEW = 0x8016;

uint EVENT_OBJECT_END = 0x80FF;

uint EVENT_AIA_START = 0xA000;

uint EVENT_AIA_END = 0xAFFF;

Notes:

The MSDN documentation states:

When you use a Win32 API that has a callback in managed code, such as SetWinEventHook, you should use GCHandle class structure to avoid exceptions.

This is apparently not accurate as the P/Invoke layer automatically generates a marshaling stub at a fixed address:

http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

Alternative Managed API:

The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a SystemAccessibleObject class to access accessible objects and handle accessible events.

Documentation

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