Server/WindowsHook.cs
author StephaneLenclud
Tue, 29 Sep 2015 18:38:36 +0200
changeset 170 f59d913ae7a9
parent 0 f6eca6facd07
permissions -rw-r--r--
Adding Visual Studio Setup project.
sl@0
     1
//=============================================================================
sl@0
     2
// COPYRIGHT: Prosoft-Lanz
sl@0
     3
//=============================================================================
sl@0
     4
//
sl@0
     5
// $Workfile: WindowsHook.cs $
sl@0
     6
//
sl@0
     7
// PROJECT : CodeProject Components
sl@0
     8
// VERSION : 1.00
sl@0
     9
// CREATION : 19.02.2003
sl@0
    10
// AUTHOR : JCL
sl@0
    11
//
sl@0
    12
// DETAILS : This class implement the Windows hook mechanism.
sl@0
    13
//           From MSDN, Dino Esposito.
sl@0
    14
//
sl@0
    15
//-----------------------------------------------------------------------------
sl@0
    16
using System;
sl@0
    17
using System.Runtime.InteropServices;
sl@0
    18
sl@0
    19
namespace CodeProject.Win32API.Hook
sl@0
    20
{
sl@0
    21
	///////////////////////////////////////////////////////////////////////
sl@0
    22
	#region Class HookEventArgs
sl@0
    23
sl@0
    24
	/// Class used for hook event arguments.
sl@0
    25
	public class HookEventArgs : EventArgs
sl@0
    26
	{
sl@0
    27
		/// Event code parameter.
sl@0
    28
		public int code;
sl@0
    29
		/// wParam parameter.
sl@0
    30
		public IntPtr wParam;
sl@0
    31
		/// lParam parameter.
sl@0
    32
		public IntPtr lParam;
sl@0
    33
sl@0
    34
		internal HookEventArgs(int code, IntPtr wParam, IntPtr lParam)
sl@0
    35
		{
sl@0
    36
			this.code = code;
sl@0
    37
			this.wParam = wParam;
sl@0
    38
			this.lParam = lParam;
sl@0
    39
		}
sl@0
    40
	}
sl@0
    41
	
sl@0
    42
	#endregion
sl@0
    43
sl@0
    44
	///////////////////////////////////////////////////////////////////////
sl@0
    45
	#region Enum HookType
sl@0
    46
sl@0
    47
	/// Hook Types.
sl@0
    48
	public enum HookType : int
sl@0
    49
	{
sl@0
    50
		/// <value>0</value>
sl@0
    51
		WH_JOURNALRECORD = 0,
sl@0
    52
		/// <value>1</value>
sl@0
    53
		WH_JOURNALPLAYBACK = 1,
sl@0
    54
		/// <value>2</value>
sl@0
    55
		WH_KEYBOARD = 2,
sl@0
    56
		/// <value>3</value>
sl@0
    57
		WH_GETMESSAGE = 3,
sl@0
    58
		/// <value>4</value>
sl@0
    59
		WH_CALLWNDPROC = 4,
sl@0
    60
		/// <value>5</value>
sl@0
    61
		WH_CBT = 5,
sl@0
    62
		/// <value>6</value>
sl@0
    63
		WH_SYSMSGFILTER = 6,
sl@0
    64
		/// <value>7</value>
sl@0
    65
		WH_MOUSE = 7,
sl@0
    66
		/// <value>8</value>
sl@0
    67
		WH_HARDWARE = 8,
sl@0
    68
		/// <value>9</value>
sl@0
    69
		WH_DEBUG = 9,
sl@0
    70
		/// <value>10</value>
sl@0
    71
		WH_SHELL = 10,
sl@0
    72
		/// <value>11</value>
sl@0
    73
		WH_FOREGROUNDIDLE = 11,
sl@0
    74
		/// <value>12</value>
sl@0
    75
		WH_CALLWNDPROCRET = 12,		
sl@0
    76
		/// <value>13</value>
sl@0
    77
		WH_KEYBOARD_LL = 13,
sl@0
    78
		/// <value>14</value>
sl@0
    79
		WH_MOUSE_LL = 14
sl@0
    80
	}
sl@0
    81
	#endregion
sl@0
    82
sl@0
    83
	///////////////////////////////////////////////////////////////////////
sl@0
    84
	#region Class WindowsHook
sl@0
    85
sl@0
    86
	/// <summary>
sl@0
    87
	/// Class to expose the windows hook mechanism.
sl@0
    88
	/// </summary>
sl@0
    89
	public class WindowsHook
sl@0
    90
	{
sl@0
    91
		/// <summary>
sl@0
    92
		/// Hook delegate method.
sl@0
    93
		/// </summary>
sl@0
    94
		public delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
sl@0
    95
sl@0
    96
		// internal properties
sl@0
    97
		internal IntPtr hHook = IntPtr.Zero;
sl@0
    98
		internal HookProc filterFunc = null;
sl@0
    99
		internal HookType hookType;
sl@0
   100
sl@0
   101
		/// <summary>
sl@0
   102
		/// Hook delegate method.
sl@0
   103
		/// </summary>
sl@0
   104
		public delegate void HookEventHandler(object sender, HookEventArgs e);
sl@0
   105
sl@0
   106
		/// <summary>
sl@0
   107
		/// Hook invoke event.
sl@0
   108
		/// </summary>
sl@0
   109
		public event HookEventHandler HookInvoke;
sl@0
   110
sl@0
   111
		internal void OnHookInvoke(HookEventArgs e)
sl@0
   112
		{
sl@0
   113
			if (HookInvoke != null)
sl@0
   114
				HookInvoke(this, e);
sl@0
   115
		}
sl@0
   116
sl@0
   117
		/// <summary>
sl@0
   118
		/// Construct a HookType hook.
sl@0
   119
		/// </summary>
sl@0
   120
		/// <param name="hook">Hook type.</param>
sl@0
   121
		public WindowsHook(HookType hook)
sl@0
   122
		{
sl@0
   123
			hookType = hook;
sl@0
   124
			filterFunc = new HookProc(this.CoreHookProc);
sl@0
   125
		}
sl@0
   126
		/// <summary>
sl@0
   127
		/// Construct a HookType hook giving a hook filter delegate method.
sl@0
   128
		/// </summary>
sl@0
   129
		/// <param name="hook">Hook type</param>
sl@0
   130
		/// <param name="func">Hook filter event.</param>
sl@0
   131
		public WindowsHook(HookType hook, HookProc func)
sl@0
   132
		{
sl@0
   133
			hookType = hook;
sl@0
   134
			filterFunc = func; 
sl@0
   135
		}
sl@0
   136
sl@0
   137
		// default hook filter function
sl@0
   138
		internal int CoreHookProc(int code, IntPtr wParam, IntPtr lParam)
sl@0
   139
		{
sl@0
   140
			if (code < 0)
sl@0
   141
				return CallNextHookEx(hHook, code, wParam, lParam);
sl@0
   142
sl@0
   143
			// let clients determine what to do
sl@0
   144
			HookEventArgs e = new HookEventArgs(code, wParam, lParam);
sl@0
   145
			OnHookInvoke(e);
sl@0
   146
sl@0
   147
			// yield to the next hook in the chain
sl@0
   148
			return CallNextHookEx(hHook, code, wParam, lParam);
sl@0
   149
		}
sl@0
   150
sl@0
   151
		/// <summary>
sl@0
   152
		/// Install the hook. 
sl@0
   153
		/// </summary>
sl@0
   154
		public void Install()
sl@0
   155
		{
sl@0
   156
			hHook = SetWindowsHookEx(hookType, filterFunc, IntPtr.Zero, (int)AppDomain.GetCurrentThreadId());
sl@0
   157
		}
sl@0
   158
sl@0
   159
		
sl@0
   160
		/// <summary>
sl@0
   161
		/// Uninstall the hook.
sl@0
   162
		/// </summary>
sl@0
   163
 		public void Uninstall()
sl@0
   164
		{
sl@0
   165
			if (hHook != IntPtr.Zero)
sl@0
   166
			{
sl@0
   167
				UnhookWindowsHookEx(hHook);
sl@0
   168
				hHook = IntPtr.Zero;
sl@0
   169
			}
sl@0
   170
		}
sl@0
   171
sl@0
   172
		#region Win32 Imports
sl@0
   173
sl@0
   174
		[DllImport("user32.dll")]
sl@0
   175
		internal static extern IntPtr SetWindowsHookEx(HookType code, HookProc func, IntPtr hInstance, int threadID);
sl@0
   176
sl@0
   177
		[DllImport("user32.dll")]
sl@0
   178
		internal static extern int UnhookWindowsHookEx(IntPtr hhook); 
sl@0
   179
sl@0
   180
		[DllImport("user32.dll")]
sl@0
   181
		internal static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);
sl@0
   182
sl@0
   183
		#endregion
sl@0
   184
	}
sl@0
   185
	#endregion
sl@0
   186
}