sl@0: //=============================================================================
sl@0: // COPYRIGHT: Prosoft-Lanz
sl@0: //=============================================================================
sl@0: //
sl@0: // $Workfile: WindowsHook.cs $
sl@0: //
sl@0: // PROJECT : CodeProject Components
sl@0: // VERSION : 1.00
sl@0: // CREATION : 19.02.2003
sl@0: // AUTHOR : JCL
sl@0: //
sl@0: // DETAILS : This class implement the Windows hook mechanism.
sl@0: // From MSDN, Dino Esposito.
sl@0: //
sl@0: //-----------------------------------------------------------------------------
sl@0: using System;
sl@0: using System.Runtime.InteropServices;
sl@0:
sl@0: namespace CodeProject.Win32API.Hook
sl@0: {
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Class HookEventArgs
sl@0:
sl@0: /// Class used for hook event arguments.
sl@0: public class HookEventArgs : EventArgs
sl@0: {
sl@0: /// Event code parameter.
sl@0: public int code;
sl@0: /// wParam parameter.
sl@0: public IntPtr wParam;
sl@0: /// lParam parameter.
sl@0: public IntPtr lParam;
sl@0:
sl@0: internal HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
sl@0: {
sl@0: this.code = code;
sl@0: this.wParam = wParam;
sl@0: this.lParam = lParam;
sl@0: }
sl@0: }
sl@0:
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Enum HookType
sl@0:
sl@0: /// Hook Types.
sl@0: public enum HookType : int
sl@0: {
sl@0: /// 0
sl@0: WH_JOURNALRECORD = 0,
sl@0: /// 1
sl@0: WH_JOURNALPLAYBACK = 1,
sl@0: /// 2
sl@0: WH_KEYBOARD = 2,
sl@0: /// 3
sl@0: WH_GETMESSAGE = 3,
sl@0: /// 4
sl@0: WH_CALLWNDPROC = 4,
sl@0: /// 5
sl@0: WH_CBT = 5,
sl@0: /// 6
sl@0: WH_SYSMSGFILTER = 6,
sl@0: /// 7
sl@0: WH_MOUSE = 7,
sl@0: /// 8
sl@0: WH_HARDWARE = 8,
sl@0: /// 9
sl@0: WH_DEBUG = 9,
sl@0: /// 10
sl@0: WH_SHELL = 10,
sl@0: /// 11
sl@0: WH_FOREGROUNDIDLE = 11,
sl@0: /// 12
sl@0: WH_CALLWNDPROCRET = 12,
sl@0: /// 13
sl@0: WH_KEYBOARD_LL = 13,
sl@0: /// 14
sl@0: WH_MOUSE_LL = 14
sl@0: }
sl@0: #endregion
sl@0:
sl@0: ///////////////////////////////////////////////////////////////////////
sl@0: #region Class WindowsHook
sl@0:
sl@0: ///
sl@0: /// Class to expose the windows hook mechanism.
sl@0: ///
sl@0: public class WindowsHook
sl@0: {
sl@0: ///
sl@0: /// Hook delegate method.
sl@0: ///
sl@0: public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
sl@0:
sl@0: // internal properties
sl@0: internal IntPtr hHook = IntPtr.Zero;
sl@0: internal HookProc filterFunc = null;
sl@0: internal HookType hookType;
sl@0:
sl@0: ///
sl@0: /// Hook delegate method.
sl@0: ///
sl@0: public delegate void HookEventHandler(object sender, HookEventArgs e);
sl@0:
sl@0: ///
sl@0: /// Hook invoke event.
sl@0: ///
sl@0: public event HookEventHandler HookInvoke;
sl@0:
sl@0: internal void OnHookInvoke(HookEventArgs e)
sl@0: {
sl@0: if (HookInvoke != null)
sl@0: HookInvoke(this, e);
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Construct a HookType hook.
sl@0: ///
sl@0: /// Hook type.
sl@0: public WindowsHook(HookType hook)
sl@0: {
sl@0: hookType = hook;
sl@0: filterFunc = new HookProc(this.CoreHookProc);
sl@0: }
sl@0: ///
sl@0: /// Construct a HookType hook giving a hook filter delegate method.
sl@0: ///
sl@0: /// Hook type
sl@0: /// Hook filter event.
sl@0: public WindowsHook(HookType hook, HookProc func)
sl@0: {
sl@0: hookType = hook;
sl@0: filterFunc = func;
sl@0: }
sl@0:
sl@0: // default hook filter function
sl@0: internal int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
sl@0: {
sl@0: if (code < 0)
sl@0: return CallNextHookEx(hHook, code, wParam, lParam);
sl@0:
sl@0: // let clients determine what to do
sl@0: HookEventArgs e = new HookEventArgs(code, wParam, lParam);
sl@0: OnHookInvoke(e);
sl@0:
sl@0: // yield to the next hook in the chain
sl@0: return CallNextHookEx(hHook, code, wParam, lParam);
sl@0: }
sl@0:
sl@0: ///
sl@0: /// Install the hook.
sl@0: ///
sl@0: public void Install()
sl@0: {
sl@0: hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
sl@0: }
sl@0:
sl@0:
sl@0: ///
sl@0: /// Uninstall the hook.
sl@0: ///
sl@0: public void Uninstall()
sl@0: {
sl@0: if (hHook != IntPtr.Zero)
sl@0: {
sl@0: UnhookWindowsHookEx(hHook);
sl@0: hHook = IntPtr.Zero;
sl@0: }
sl@0: }
sl@0:
sl@0: #region Win32 Imports
sl@0:
sl@0: [DllImport("user32.dll")]
sl@0: internal static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
sl@0:
sl@0: [DllImport("user32.dll")]
sl@0: internal static extern int UnhookWindowsHookEx(IntPtr hhook);
sl@0:
sl@0: [DllImport("user32.dll")]
sl@0: internal static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
sl@0:
sl@0: #endregion
sl@0: }
sl@0: #endregion
sl@0: }