要实现系统钩子其实很简单,调用三个Win32的API即可。
SetWindowsHookEx 用于设置钩子。(设立一道卡子,盘查需要的信息)
SetWindowsHookEx 用于设置钩子。(设立一道卡子,盘查需要的信息)
[DllImport( "user32.dll", CallingConvention = CallingConvention.StdCall )]
public static extern IntPtr SetWindowsHookEx ( WH_Codes idHook, HookProc lpfn,
IntPtr pInstance, int threadId );
public static extern IntPtr SetWindowsHookEx ( WH_Codes idHook, HookProc lpfn,
IntPtr pInstance, int threadId );
CallNextHookEx 用于传递钩子(消息是重要的,所以从哪里来,就应该回到哪里去,除非你决定要封锁消息)
[DllImport( "user32.dll", CallingConvention = CallingConvention.StdCall )]
public static extern int CallNextHookEx ( IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam );
public static extern int CallNextHookEx ( IntPtr pHookHandle, int nCode,
Int32 wParam, IntPtr lParam );
UnhookWindowsHookEx 卸载钩子(卸载很重要,卡子设多了会造成拥堵)
[DllImport( "user32.dll", CallingConvention = CallingConvention.StdCall )]
public static extern bool UnhookWindowsHookEx ( IntPtr pHookHandle );
public static extern bool UnhookWindowsHookEx ( IntPtr pHookHandle );
更加详细的请看如下微软中国官方网站关于的HOOK的专题