什么是钩子(Hook)
钩子(Hook)是Windows提供的一种消息处理机制平台,是指在程序正常运行中接受信息之前预先 启动的函数,用来检查和修改传给该程序的信息,(钩子)实际上是一个处理消息的程序段,通 过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获 该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不作处理而继续传递该消息,还可以强制结束消息的传递。注意:安装钩子函数将会影响系统的性能。监测“系统范围事件”的系统钩子特别明显。因为系统在处理所有的相关事件时都将调用您的钩子函数,这样您的系统将会明显的减慢。所以应谨慎使用,用完后立即卸载。还有,由于您可以预先截获其它进程的消息,所以一旦您的钩子函数出了问题的话必将影响其它的进程。
钩子的作用范围
一共有两种范围(类型)的钩子,局部的和远程的。局部钩子仅钩挂自己进程的事件。远程的钩子还可以将钩挂其它进程发生的事件。远程的钩子又有两种: 基于线程的钩子将捕获其它进程中某一特定线程的事件。简言之,就是可以用来观察其它进程中的某一特定线程将发生的事件。 系统范围的钩子将捕捉系统中所有进程将发生的事件消息。
Hook 类型
Windows共有14种Hooks,每一种类型的Hook可以使应用程序能够监视不同类型的系统消息处理机制。下面描述所有可以利用的Hook类型的发生时机。详细内容可以查阅MSDN,这里只介绍我们将要用到的两种类型的钩子。
(1)WH_KEYBOARD_LL Hook
WH_KEYBOARD_LL Hook监视输入到线程消息队列中的键盘消息。
(2)WH_MOUSE_LL Hook
WH_MOUSE_LL Hook监视输入到线程消息队列中的鼠标消息。
下面的 class 把 API 调用封装起来以便调用。
1// NativeMethods.cs
2using System;
3using System.Runtime.InteropServices;
4using System.Drawing;
5
6namespace CnBlogs.Youzai.ScreenKeyboard {
7 [StructLayout(LayoutKind.Sequential)]
8 internal struct MOUSEINPUT {
9 public int dx;
10 public int dy;
11 public int mouseData;
12 public int dwFlags;
13 public int time;
14 public IntPtr dwExtraInfo;
15 }
16
17 [StructLayout(LayoutKind.Sequential)]
18 internal struct KEYBDINPUT {
19 public short wVk;
20 public short wScan;
21 public int dwFlags;
22 public int time;
23 public IntPtr dwExtraInfo;
24 }
25
26 [StructLayout(LayoutKind.Explicit)]
27 internal struct Input {
28 [FieldOffset(0)]
29 public int type;
30 [FieldOffset(4)]
31 public MOUSEINPUT mi;
32 [FieldOffset(4)]
33 public KEYBDINPUT ki;
34 [FieldOffset(4)]
35 public HARDWAREINPUT hi;
36 }
37
38 [StructLayout(LayoutKind.Sequential)]
39 internal struct HARDWAREINPUT {
40 public int uMsg;
41 public short wParamL;
42 public short wParamH;
43 }
44
45 internal class INPUT {
46 public const int MOUSE = 0;
47 public const int KEYBOARD = 1;
48 public const int HARDWARE = 2;
49 }
50
51 internal static class NativeMethods {
52 [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
53 internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
54
55 [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
56 internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
57
58 [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
59 internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);
60
61 [DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
62 internal static extern int GetTickCount();
63
64 [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
65 internal static extern short GetKeyState(int nVirtKey);
66
67 [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
68 internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
69 }
70}
安装钩子
使用SetWindowsHookEx函数(API函数),指定一个Hook类型、自己的Hook过程是全局还是局部Hook,同时给出Hook过程的进入点,就可以轻松的安装自己的Hook过程。SetWindowsHookEx总是将你的Hook函数放置在Hook链的顶端。你可以使用CallNextHookEx函数将系统消息传递给Hook链中的下一个函数。
对于某些类型的Hook,系统将向该类的所有Hook函数发送消息,这时,Hook函数中的CallNextHookEx语句将被忽略。全局(远程钩子)Hook函数可以拦截系统中所有线程的某个特定的消息,为了安装一个全局Hook过程,必须在应用程序外建立一个DLL并将该Hook函数封装到其中,应用程序在安装全局Hook过程时必须先得到该DLL模块的句柄。将Dll名传递给LoadLibrary 函数,就会得到该DLL模块的句柄;得到该句柄 后,使用GetProcAddress函数可以得到Hook过程的地址。最后,使用SetWindowsHookEx将 Hook过程的首址嵌入相应的Hook链中,SetWindowsHookEx传递一个模块句柄,它为Hook过程的进入点,线程标识符置为0,该Hook过程同系统中的所有线程关联。如果是安装局部Hook此时该Hook函数可以放置在DLL中,也可以放置在应用程序的模块段。在C#中通过平台调用(前文已经介绍过)来调用API函数。
1 public void Start(bool installMouseHook, bool installKeyboardHook) {
2 if (hMouseHook == IntPtr.Zero && installMouseHook) {
3 MouseHookProcedure = new HookProc(MouseHookProc);
4 hMouseHook = SetWindowsHookEx(
5 WH_MOUSE_LL,
6 MouseHookProcedure,
7 Marshal.GetHINSTANCE(
8 Assembly.GetExecutingAssembly().GetModules()[0]),
9 0
10 );
11
12 if (hMouseHook == IntPtr.Zero) {
13 int errorCode = Marshal.GetLastWin32Error();
14 Stop(true, false, false);
15
16 throw new Win32Exception(errorCode);
17 }
18 }
19
20 if (hKeyboardHook == IntPtr.Zero && installKeyboardHook) {
21 KeyboardHookProcedure = new HookProc(KeyboardHookProc);
22 //install hook
23 hKeyboardHook = SetWindowsHookEx(
24 WH_KEYBOARD_LL,
25 KeyboardHookProcedure,
26 Marshal.GetHINSTANCE(
27 Assembly.GetExecutingAssembly().GetModules()[0]),
28 0);
29 // If SetWindowsHookEx fails.
30 if (hKeyboardHook == IntPtr.Zero) {
31 // Returns the error code returned by the last
32 // unmanaged function called using platform invoke
33 // that has the DllImportAttribute.SetLastError flag set.
34 int errorCode = Marshal.GetLastWin32Error();
35 //do cleanup
36 Stop(false, true, false);
37 //Initializes and throws a new instance of the
38 // Win32Exception class with the specified error.
39 throw new Win32Exception(errorCode);
40 }
41 }
42 }
使用完钩子后,要进行卸载,这个可以写在析构函数中。
1
2 public void Stop() {
3 this.Stop(true, true, true);
4 }
5
6 public void Stop(bool uninstallMouseHook, bool uninstallKeyboardHook,
7 bool throwExceptions) {
8 // if mouse hook set and must be uninstalled
9 if (hMouseHook != IntPtr.Zero && uninstallMouseHook) {
10 // uninstall hook
11 bool retMouse = UnhookWindowsHookEx(hMouseHook);
12 // reset invalid handle
13 hMouseHook = IntPtr.Zero;
14 // if failed and exception must be thrown
15 if (retMouse == false && throwExceptions) {
16 // Returns the error code returned by the last unmanaged function
17 // called using platform invoke that has the DllImportAttribute.
18 // SetLastError flag set.
19 int errorCode = Marshal.GetLastWin32Error();
20 // Initializes and throws a new instance of the Win32Exception class
21 // with the specified error.
22 throw new Win32Exception(errorCode);
23 }
24 }
25
26 // if keyboard hook set and must be uninstalled
27 if (hKeyboardHook != IntPtr.Zero && uninstallKeyboardHook) {
28 // uninstall hook
29 bool retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
30 // reset invalid handle
31 hKeyboardHook = IntPtr.Zero;
32 // if failed and exception must be thrown
33 if (retKeyboard == false && throwExceptions) {
34 // Returns the error code returned by the last unmanaged function
35 // called using platform invoke that has the DllImportAttribute.
36 // SetLastError flag set.
37 int errorCode = Marshal.GetLastWin32Error();
38 // Initializes and throws a new instance of the Win32Exception class
39 // with the specified error.
40 throw new Win32Exception(errorCode);
41 }
42 }
43 }
44