SetWindowsHookEx (user32)
Last changed: -93.41.3.12

.
Summary
Installs a hook to monitor certain types of events.

C# Signature:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback,
IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(HookType hook, LowLevelKeyboardProc callback,
IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowsHookEx(HookType code, LowLevelMouseProc func,
IntPtr hInstance, int threadID);

VB.NET Signature:

<DllImport("user32.dll", SetLastError := True)> _
Private Shared Function SetWindowsHookEx(ByVal hook As HookType, ByVal callback As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function

<DllImport("user32.dll", SetLastError := True)> _
Private Shared Function SetWindowsHookEx(ByVal hook As HookType, ByVal callback As LowLevelKeyboardProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function

<DllImport("user32.dll", SetLastError := True)> _
Private Shared Function SetWindowsHookEx(ByVal code As HookType, ByVal func As LowLevelMouseProc, ByVal hInstance As IntPtr, ByVal threadID As Integer) As IntPtr
End Function

User-Defined Types:

A HookType constant specifying the type of hook to install.

A HookProc, LowLevelKeyboardProc or LowLevelMouseProc delegate representing the hook procedure method.

Notes:

This will enable you to install application hooks. However, you cannot implement global hooks in Microsoft .NET Framework except low level hooks. To install a global hook, a hook must have a native dynamic-link library (DLL) export to inject itself in another process that requires a valid, consistent function to call into. This requires a DLL export, which .NET Framework does not support. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically.

AppDomain.GetCurrentThreadId is marked as deprecated in favour of Thread.ManagedThreadId but this does not seem to work!

// start edit

Quite true System.Threading.Thread.CurrentThread.ManagedThreadId does not appear to return the correct threadid for this purpose.

I found WIN32 API GetCurrentThreadId() does the trick.

VB.NET:

Private Declare Function GetCurrentThreadId Lib "kernel32" () As Integer

SetWindowsHookEx(HookType.WH_KEYBOARD, Me.myCallbackDelegate, IntPtr.Zero, GetCurrentThreadId)

// end edit T.A, Jones 2008

To hook low level events use:

        IntPtr hHook;

        using (Process process = Process.GetCurrentProcess())
        using (ProcessModule module = process.MainModule)
        {
        IntPtr hModule = GetModuleHandle(module.ModuleName);

        hHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, hook, hModule, 0);
        }

Tips & Tricks:

Remember to keep the HookProc delegate alive manually, such as using a class member as shown in the example below, otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException.

Remember to call CallNextHookEx in your callback delegate.

Remember to call UnhookWindowsHookEx using the handle returned by SetWindowsHookEx

Note how you can import the same function several times with different overloaded signatures to handle the common case where one parameter is an opaque pointer pointing to a struct that depends on another parameter.

Sample Code:

// this sample installs a keyboard hook

using System.Windows.Forms;
public class MyClass
{
     private HookProc myCallbackDelegate = null;

     public MyClass()
     {
     // initialize our delegate
     this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);

     // setup a keyboard hook
     SetWindowsHookEx(HookType.WH_KEYBOARD, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
     }

     [DllImport("user32.dll")]
     protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);

     [DllImport("user32.dll")]
     static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

     private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
     {
    if (code < 0) {
        //you need to call CallNextHookEx without further processing
        //and return the value returned by CallNextHookEx
        return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
    }
     // we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
     Keys keyPressed = (Keys)wParam.ToInt32();
     Console.WriteLine(keyPressed);
    //return the value returned by CallNextHookEx
    return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
     }
}

VB.NET:

' My sample installs a keyboard hook

Imports System.Windows.Forms

Imports System.Runtime.InteropServices

Public Class MyClass1

    Delegate Function HookProc(ByVal code As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) _
    As Integer

    Private myCallbackDelegate As HookProc = Nothing

    Public Sub New()

    ' initialize our delegate
    Me.myCallbackDelegate = New HookProc(AddressOf Me.MyCallbackFunction)

    ' setup a keyboard hook
    SetWindowsHookEx(HookType.WH_KEYBOARD, Me.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId())
    End Sub

    <DllImport("user32.dll")> _
    Friend Shared Function SetWindowsHookEx(ByVal idHook As Integer, _
      ByVal lpfn As HookProc, _
      ByVal hInstance As IntPtr, _
      ByVal threadId As Integer) As Integer
    End Function

    <DllImport("user32.dll")> _
    Friend Shared Function CallNextHookEx(ByVal hhk As intptr, _
    ByVal nCode As Integer, ByVal wParam As intptr, _
    ByVal lParam As intptr) As Integer
    End Function
    'ok
    Private Function MyCallbackFunction(ByVal code As Integer, ByVal wParam As intptr, ByVal lParam As intptr) As Integer
    If (code < 0) Then
        'you need to call CallNextHookEx without further processing
        'and return the value returned by CallNextHookEx
        Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)    'unt
    End If
    ' we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
    Dim keyPressed As Keys = CType(wParam.ToInt32, Keys)
    Console.WriteLine(keyPressed)
    'return the value returned by CallNextHookEx
    Return CallNextHookEx(IntPtr.Zero, code, wParam, lParam)
    End Function

End Class

Alternative Managed API:

The ManagedWindowsApi project (http://mwinapi.sourceforge.net) provides a Hook class and subclasses for Journal hooks, Message hooks and Low-Level hooks.

Documentation

SetWindowsHookEx on MSDN

MSDN example of a mouse hook in C#: http://support.microsoft.com/default.aspx?scid=kb;en-us;318804#3

MSDN article on hooks in .NET: http://msdn.microsoft.com/msdnmag/issues/02/10/CuttingEdge/

MSDN article on KeyboardProc: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/keyboardproc.asp

Stephen Toub entry on low-level keyboard hook in C#: http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx

Stephen Toub entry on low-level mouse hook on C#: http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx

.