求详细的写事件方法和HOOK API的方法 !!!!C#语言
现在在做一个注册表监控的程序,里面用到一个时间的句柄。而那个事件需要自己定义,目前完全不接触过事件定义!目的在于,当注册表发生改变时,用那个事件去拦截,用户同意后在继续。...
现在在做一个注册表监控的程序,里面用到一个时间的句柄。而那个事件需要自己定义,目前完全不接触过事件定义!目的在于,当注册表发生改变时,用那个事件去拦截,用户同意后在继续。如果不同意则取消!
知道里面要用到API所以请求详细的帮助,谢谢
写错了 里面用到一个事件的句柄!!不好意思 展开
知道里面要用到API所以请求详细的帮助,谢谢
写错了 里面用到一个事件的句柄!!不好意思 展开
展开全部
全局HOOK API (C#) 的实现
主要实现代码
代码:
[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);
[DllImport("user32")]
private static extern int ToAscii(
int uVirtKey,
int uScanCode,
byte[] lpbKeyState,
byte[] lpwTransKey,
int fuState);
[DllImport("user32")]
private static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern short GetKeyState(int vKey);
使用SetWindowsHookEx函数(API函数),指定一个Hook类型、自己的Hook过程是全局还是局部Hook,同时给出Hook过程的进入点,就可以轻松的安装自己的Hook。
代码:
// 创建鼠标实例.
MouseHookProcedure = new HookProc(MouseHookProc);
//安装 鼠标hook
hMouseHook = SetWindowsHookEx(
WH_MOUSE_LL,
MouseHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
// 创建键盘实例.
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//安装 键盘hook
hKeyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
KeyboardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
卸载Hook UnhookWindowsHookEx()函数(API函数)
主要实现代码
代码:
[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);
[DllImport("user32")]
private static extern int ToAscii(
int uVirtKey,
int uScanCode,
byte[] lpbKeyState,
byte[] lpwTransKey,
int fuState);
[DllImport("user32")]
private static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern short GetKeyState(int vKey);
使用SetWindowsHookEx函数(API函数),指定一个Hook类型、自己的Hook过程是全局还是局部Hook,同时给出Hook过程的进入点,就可以轻松的安装自己的Hook。
代码:
// 创建鼠标实例.
MouseHookProcedure = new HookProc(MouseHookProc);
//安装 鼠标hook
hMouseHook = SetWindowsHookEx(
WH_MOUSE_LL,
MouseHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
// 创建键盘实例.
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//安装 键盘hook
hKeyboardHook = SetWindowsHookEx(
WH_KEYBOARD_LL,
KeyboardHookProcedure,
Marshal.GetHINSTANCE(
Assembly.GetExecutingAssembly().GetModules()[0]),
0);
卸载Hook UnhookWindowsHookEx()函数(API函数)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询