WindowsHook.cs
changeset 18 7acec5059fa6
parent 17 19c1aaf900dc
child 19 1a85ec255882
     1.1 --- a/WindowsHook.cs	Tue Aug 12 20:37:57 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,186 +0,0 @@
     1.4 -//=============================================================================
     1.5 -// COPYRIGHT: Prosoft-Lanz
     1.6 -//=============================================================================
     1.7 -//
     1.8 -// $Workfile: WindowsHook.cs $
     1.9 -//
    1.10 -// PROJECT : CodeProject Components
    1.11 -// VERSION : 1.00
    1.12 -// CREATION : 19.02.2003
    1.13 -// AUTHOR : JCL
    1.14 -//
    1.15 -// DETAILS : This class implement the Windows hook mechanism.
    1.16 -//           From MSDN, Dino Esposito.
    1.17 -//
    1.18 -//-----------------------------------------------------------------------------
    1.19 -using System;
    1.20 -using System.Runtime.InteropServices;
    1.21 -
    1.22 -namespace CodeProject.Win32API.Hook
    1.23 -{
    1.24 -	///////////////////////////////////////////////////////////////////////
    1.25 -	#region Class HookEventArgs
    1.26 -
    1.27 -	/// Class used for hook event arguments.
    1.28 -	public class HookEventArgs : EventArgs
    1.29 -	{
    1.30 -		/// Event code parameter.
    1.31 -		public int code;
    1.32 -		/// wParam parameter.
    1.33 -		public IntPtr wParam;
    1.34 -		/// lParam parameter.
    1.35 -		public IntPtr lParam;
    1.36 -
    1.37 -		internal HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
    1.38 -		{
    1.39 -			this.code = code;
    1.40 -			this.wParam = wParam;
    1.41 -			this.lParam = lParam;
    1.42 -		}
    1.43 -	}
    1.44 -	
    1.45 -	#endregion
    1.46 -
    1.47 -	///////////////////////////////////////////////////////////////////////
    1.48 -	#region Enum HookType
    1.49 -
    1.50 -	/// Hook Types.
    1.51 -	public enum HookType : int
    1.52 -	{
    1.53 -		/// <value>0</value>
    1.54 -		WH_JOURNALRECORD = 0,
    1.55 -		/// <value>1</value>
    1.56 -		WH_JOURNALPLAYBACK = 1,
    1.57 -		/// <value>2</value>
    1.58 -		WH_KEYBOARD = 2,
    1.59 -		/// <value>3</value>
    1.60 -		WH_GETMESSAGE = 3,
    1.61 -		/// <value>4</value>
    1.62 -		WH_CALLWNDPROC = 4,
    1.63 -		/// <value>5</value>
    1.64 -		WH_CBT = 5,
    1.65 -		/// <value>6</value>
    1.66 -		WH_SYSMSGFILTER = 6,
    1.67 -		/// <value>7</value>
    1.68 -		WH_MOUSE = 7,
    1.69 -		/// <value>8</value>
    1.70 -		WH_HARDWARE = 8,
    1.71 -		/// <value>9</value>
    1.72 -		WH_DEBUG = 9,
    1.73 -		/// <value>10</value>
    1.74 -		WH_SHELL = 10,
    1.75 -		/// <value>11</value>
    1.76 -		WH_FOREGROUNDIDLE = 11,
    1.77 -		/// <value>12</value>
    1.78 -		WH_CALLWNDPROCRET = 12,		
    1.79 -		/// <value>13</value>
    1.80 -		WH_KEYBOARD_LL = 13,
    1.81 -		/// <value>14</value>
    1.82 -		WH_MOUSE_LL = 14
    1.83 -	}
    1.84 -	#endregion
    1.85 -
    1.86 -	///////////////////////////////////////////////////////////////////////
    1.87 -	#region Class WindowsHook
    1.88 -
    1.89 -	/// <summary>
    1.90 -	/// Class to expose the windows hook mechanism.
    1.91 -	/// </summary>
    1.92 -	public class WindowsHook
    1.93 -	{
    1.94 -		/// <summary>
    1.95 -		/// Hook delegate method.
    1.96 -		/// </summary>
    1.97 -		public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
    1.98 -
    1.99 -		// internal properties
   1.100 -		internal IntPtr hHook = IntPtr.Zero;
   1.101 -		internal HookProc filterFunc = null;
   1.102 -		internal HookType hookType;
   1.103 -
   1.104 -		/// <summary>
   1.105 -		/// Hook delegate method.
   1.106 -		/// </summary>
   1.107 -		public delegate void HookEventHandler(object sender, HookEventArgs e);
   1.108 -
   1.109 -		/// <summary>
   1.110 -		/// Hook invoke event.
   1.111 -		/// </summary>
   1.112 -		public event HookEventHandler HookInvoke;
   1.113 -
   1.114 -		internal void OnHookInvoke(HookEventArgs e)
   1.115 -		{
   1.116 -			if (HookInvoke != null)
   1.117 -				HookInvoke(this, e);
   1.118 -		}
   1.119 -
   1.120 -		/// <summary>
   1.121 -		/// Construct a HookType hook.
   1.122 -		/// </summary>
   1.123 -		/// <param name="hook">Hook type.</param>
   1.124 -		public WindowsHook(HookType hook)
   1.125 -		{
   1.126 -			hookType = hook;
   1.127 -			filterFunc = new HookProc(this.CoreHookProc);
   1.128 -		}
   1.129 -		/// <summary>
   1.130 -		/// Construct a HookType hook giving a hook filter delegate method.
   1.131 -		/// </summary>
   1.132 -		/// <param name="hook">Hook type</param>
   1.133 -		/// <param name="func">Hook filter event.</param>
   1.134 -		public WindowsHook(HookType hook, HookProc func)
   1.135 -		{
   1.136 -			hookType = hook;
   1.137 -			filterFunc = func; 
   1.138 -		}
   1.139 -
   1.140 -		// default hook filter function
   1.141 -		internal int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
   1.142 -		{
   1.143 -			if (code < 0)
   1.144 -				return CallNextHookEx(hHook, code, wParam, lParam);
   1.145 -
   1.146 -			// let clients determine what to do
   1.147 -			HookEventArgs e = new HookEventArgs(code, wParam, lParam);
   1.148 -			OnHookInvoke(e);
   1.149 -
   1.150 -			// yield to the next hook in the chain
   1.151 -			return CallNextHookEx(hHook, code, wParam, lParam);
   1.152 -		}
   1.153 -
   1.154 -		/// <summary>
   1.155 -		/// Install the hook. 
   1.156 -		/// </summary>
   1.157 -		public void Install()
   1.158 -		{
   1.159 -			hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
   1.160 -		}
   1.161 -
   1.162 -		
   1.163 -		/// <summary>
   1.164 -		/// Uninstall the hook.
   1.165 -		/// </summary>
   1.166 - 		public void Uninstall()
   1.167 -		{
   1.168 -			if (hHook != IntPtr.Zero)
   1.169 -			{
   1.170 -				UnhookWindowsHookEx(hHook);
   1.171 -				hHook = IntPtr.Zero;
   1.172 -			}
   1.173 -		}
   1.174 -
   1.175 -		#region Win32 Imports
   1.176 -
   1.177 -		[DllImport("user32.dll")]
   1.178 -		internal static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
   1.179 -
   1.180 -		[DllImport("user32.dll")]
   1.181 -		internal static extern int UnhookWindowsHookEx(IntPtr hhook); 
   1.182 -
   1.183 -		[DllImport("user32.dll")]
   1.184 -		internal static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
   1.185 -
   1.186 -		#endregion
   1.187 -	}
   1.188 -	#endregion
   1.189 -}