作者在 2009-12-20 17:36:32 发布以下内容
C#的系统钩子要点
1.载入钩子函数
[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)]
private static extern int CallNextHookEx(
int idHook,
int nCode,
int wParam,
IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hInstance,
int threadId);
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int SetWindowsHookEx(
int idHook,
HookProc lpfn,
IntPtr hInstance,
int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
private static extern int UnhookWindowsHookEx(int idHook);
//f上面三个分别是SetWindowsHookEx()建立钩子
//UnhookWindowsHookEx()撤销钩子
//CallNextHookEx()回叫函数
呵呵
2。全局实现
SetWindowsHookEx(
WH_MOUSE_LL,
s_MouseDelegate,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),//重点
0);
WH_MOUSE_LL,
s_MouseDelegate,
Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),//重点
0);
注意包含System.Reflection;
3.CallNextHookEx()就是自己的喜好了。嗯就这样