private void Dispose(bool disposing) { if (disposed || disposing) { return; } if (HookId != IntPtr.Zero) { User32Dll.UnhookWindowsHookEx( HookId); } disposed = true; }
/*-------------------------- Private Methods -------------------------*/ /// <summary> /// 设置钩子函数 /// </summary> /// <param name="proc"> /// 钩子的地址? /// </param> /// <returns> /// </returns> /// <remarks> /// https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process.mainmodule?view=netframework-4.8 /// </remarks> protected IntPtr SetHook(LowLevelHookProc proc) { // [Process] 提供对本地和远程进程的访问权限并使你能够启动和停止本地系统进程。 using (Process curProcess = Process.GetCurrentProcess()) // 获取新的 Process 组件并将其与当前活动的进程关联。 { using (ProcessModule curModule = curProcess.MainModule) // 获取关联进程的主模块。 { var ret = User32Dll.SetWindowsHookEx( (int)IdHook, proc, User32Dll.GetModuleHandle(curModule.ModuleName), 0 ); return(ret); } } }
/// <summary> /// 卸载键盘/鼠标钩子函数 /// </summary> public void UnHook() { User32Dll.UnhookWindowsHookEx(HookId); }
/// <summary> /// 调用下一个钩子 /// </summary> /// <param name="nCode"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> protected IntPtr CallNextHookEx(int nCode, IntPtr wParam, IntPtr lParam) => User32Dll.CallNextHookEx(HookId, nCode, wParam, lParam);