我们来看一下主要代码段。
首先倒入所需要的windows函数,主要有三个,SetWindowsHookEX用来安装钩子,UnhookWindowsHookEX用来卸载钩子以及CallNextHookEX用来将hook信息传递到链表中下一个hook处理过程。
下面是有关这两个low-level hook在Winuser.h中的定义:
在安装全局钩子的时候,我们就要做替换了,将WH_MOUSE和
首先倒入所需要的windows函数,主要有三个,SetWindowsHookEX用来安装钩子,UnhookWindowsHookEX用来卸载钩子以及CallNextHookEX用来将hook信息传递到链表中下一个hook处理过程。
[DllImport(
"
user32.dll
"
, CharSet
=
CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true )]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId);
[DllImport( " user32.dll " , CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true )]
private static extern int UnhookWindowsHookEx( int idHook);
[DllImport( " user32.dll " , CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam);
CallingConvention = CallingConvention.StdCall, SetLastError = true )]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hMod,
int dwThreadId);
[DllImport( " user32.dll " , CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true )]
private static extern int UnhookWindowsHookEx( int idHook);
[DllImport( " user32.dll " , CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam);
下面是有关这两个low-level hook在Winuser.h中的定义:
/**/
/// <summary>
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
/// </summary>
private const int WH_MOUSE_LL = 14 ;
/**/ /// <summary>
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events.
/// </summary>
private const int WH_KEYBOARD_LL = 13 ;
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level mouse input events.
/// </summary>
private const int WH_MOUSE_LL = 14 ;
/**/ /// <summary>
/// Windows NT/2000/XP: Installs a hook procedure that monitors low-level keyboard input events.
/// </summary>
private const int WH_KEYBOARD_LL = 13 ;
在安装全局钩子的时候,我们就要做替换了,将WH_MOUSE和