コード例 #1
0
        public int HookProcedure(int nCode, IntPtr wParam, IntPtr lParam)
        {
            bool cancel = false;

            if (nCode >= 0 && (wParam == (IntPtr)User32.WindowMessage.WM_KEYDOWN || wParam == (IntPtr)User32.WindowMessage.WM_SYSKEYDOWN))
            {
                Win32Api.KBDLLHOOKSTRUCT kb = (Win32Api.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32Api.KBDLLHOOKSTRUCT));
                if ((kb.dwFlags & Win32Api.KBDLLHOOKSTRUCTF.LLKHF_INJECTED) == 0)
                {
                    cancel = OnKeyDownEvent(kb);
                }
            }
            else if (nCode >= 0 && (wParam == (IntPtr)User32.WindowMessage.WM_KEYUP || wParam == (IntPtr)User32.WindowMessage.WM_SYSKEYUP))
            {
                Win32Api.KBDLLHOOKSTRUCT kb = (Win32Api.KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32Api.KBDLLHOOKSTRUCT));
                if ((kb.dwFlags & Win32Api.KBDLLHOOKSTRUCTF.LLKHF_INJECTED) == 0)
                {
                    cancel = OnKeyUpEvent(kb);
                }
            }
            if (cancel)
            {
                return((int)new IntPtr(1));
            }
            else
            {
                return(User32.CallNextHookEx(hookId.DangerousGetHandle(), nCode, wParam, lParam));
            }
        }
コード例 #2
0
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposedValue)
     {
         NativeMethods.UnhookWindowsHookEx(_hookHandle.DangerousGetHandle());
         _hookHandle.Close();
         _disposedValue = true;
     }
 }
コード例 #3
0
        /// <summary>
        /// <inheritdoc cref="User32.WindowsHookDelegate"/>
        /// </summary>
        private static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                var msg = (User32.WindowMessage)wParam;
                if (msg == User32.WindowMessage.WM_LBUTTONDOWN || msg == User32.WindowMessage.WM_RBUTTONDOWN)
                {
                    var input = Marshal.PtrToStructure <User32.MOUSEINPUT>(lParam);
                    MouseChanged?.Invoke(null, new MouseMessageArg
                    {
                        Msg = (int)msg,
                        X   = input.dx,
                        Y   = input.dy
                    });
                }
            }

            return(User32.CallNextHookEx(MouseHook.DangerousGetHandle(), nCode, wParam, lParam));
        }
コード例 #4
0
ファイル: HookKeys.cs プロジェクト: tqk2811/TqkLibrary.WinApi
 public void SetupHook()
 {
     using (Process curProcess = Process.GetCurrentProcess())
         using (ProcessModule curModule = curProcess.MainModule)
         {
             IntPtr ModuleHandle = Kernel32.GetModuleHandle(curModule.ModuleName);
             if (ModuleHandle != IntPtr.Zero)
             {
                 WindowsHookExHandle = User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, windowsHook, ModuleHandle, 0);
                 handle = WindowsHookExHandle.DangerousGetHandle();
             }
         }
 }
コード例 #5
0
        public void Start()
        {
            WindowsHookProceDure = new HookProc(WindowsHookProc);
            IntPtr lpfnHooker = Marshal.GetFunctionPointerForDelegate(WindowsHookProceDure);
            IntPtr hMod       = Kernel32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName).DangerousGetHandle();

            //最后一个参数为要注入的线程ID,为0时表示注册全局(系统)钩子
            User32.SafeHookHandle result = User32.SetWindowsHookEx(User32.WindowsHookType.WH_CBT, lpfnHooker, Kernel32.GetModuleHandle(new FileInfo("./Injector.dll").FullName).DangerousGetHandle(), 0);
            int error = Marshal.GetLastWin32Error();

            //wHookId = SetWindowsHookEx((int)User32.WindowsHookType.WH_CBT, WindowsHookProceDure, hMod, 0);
            if ((wHookId | result.DangerousGetHandle().ToInt32()) == 0)
            {
                throw new Exception("安装钩子失败");
            }
        }