static extern IntPtr SetWindowsHookEx(HookType hook, HookProc callback,
IntPtr hMod, uint dwThreadId);
Public Shared Function SetWindowsHookEx( _
ByVal idHook As Integer, _
ByVal lpfn As HookProc, _
ByVal hMod As IntPtr, _
ByVal dwThreadId As Integer) As IntPtr
A HookType constant specifying the type of hook to install.
A HookProc delegate representing the hook procedure method.
This will enable you to install application hooks. However, you cannot implement global hooks in Microsoft .NET Framework. 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.
Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.
Before terminating, an application must call the UnhookWindowsHookEx function to free system resources associated with the hook.
Remember to keep the HookProc delegate alive manually, otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException.
// this sample installs a keyboard hook
public class MyClass
private HookProc myCallbackDelegate = null;
// initialize our delegate
this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);
// setup a keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
protected static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
private void MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
// we can convert the 2nd parameter (the key code) to a System.Windows.Forms.Keys enum constant
Keys keyPressed = (Keys)wParam.ToInt32();
Do you know one? Please contribute it!