Ejemplo n.º 1
0
        /// <summary>
        ///     清理占用的资源
        /// </summary>
        /// <param name="disposing">
        ///     垃圾回收器正在释放资源?
        /// </param>
        protected void Dispose(bool disposing)
        {
            if (disposed || disposing)
            {
                return;
            }

            if (_HookId != IntPtr.Zero)
            {
                User32Dll.UnhookWindowsHookEx(
                    _HookId);
            }

            disposed = true;
        }
Ejemplo n.º 2
0
 /*--------------------------  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);
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 ///     卸载键盘/鼠标钩子函数
 /// </summary>
 public void UnHook()
 {
     User32Dll.UnhookWindowsHookEx(_HookId);
 }
Ejemplo n.º 4
0
 /// <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);