Пример #1
0
        public void UnHook()
        {
            if (WindowsHookExHandle != null &&
                !WindowsHookExHandle.IsClosed &&
                !WindowsHookExHandle.IsInvalid)
            {
                WindowsHookExHandle.Dispose();
            }

            WindowsHookExHandle = null;
        }
Пример #2
0
 public void Hook()
 {
     if (hookId == User32.SafeHookHandle.Null)
     {
         proc = HookProcedure;
         using (var curProcess = Process.GetCurrentProcess()) {
             using (ProcessModule curModule = curProcess.MainModule) {
                 hookId = User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, proc, Kernel32.GetModuleHandle(curModule.ModuleName), 0);
             }
         }
     }
 }
Пример #3
0
 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();
             }
         }
 }
Пример #4
0
        /// <summary>
        ///     The main entry point to the application that listens to the keystrokes across the entire OS.
        /// </summary>
        /// <param name="args">We don't use arguments.</param>
        private static void Main(string[] args)
        {
            var pid = -1;

            if (args.Length > 0)
            {
                int.TryParse(args[0], out pid);
            }

            var thread = new Thread(
                () => {
                while (true)
                {
                    if (-1 == pid)
                    {
                        return;
                    }

                    try {
                        var process = Process.GetProcessById(pid);
                        if (process.HasExited)
                        {
                            Environment.Exit(0);
                            return;
                        }
                    } catch (Exception) {
                        Environment.Exit(0);
                        return;
                    }

                    Thread.Sleep(500);
                }
            });

            thread.IsBackground = true;
            thread.Start();

            // Set a hook.
            Program.hook = User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, Program.KeystrokeCallback, IntPtr.Zero, 0);

            unsafe {
                // Buffer the messages.
                User32.MSG message;
                while (0 != User32.GetMessage(&message, IntPtr.Zero, 0, 0))
                {
                }
            }
        }
Пример #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("安装钩子失败");
            }
        }
Пример #6
0
        // ReSharper restore PrivateFieldCanBeConvertedToLocalVariable

        /// <summary>
        ///     Set up hooks
        /// </summary>
        static WinHook()
        {
            AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;

            // Set up hooks
            EventProcDelegate    = WinEventProc;
            CbForegroundPtr      = Marshal.GetFunctionPointerForDelegate(EventProcDelegate);
            ForegroundWindowHook = User32.SetWinEventHook(User32.WindowsEventHookType.EVENT_SYSTEM_FOREGROUND,
                                                          User32.WindowsEventHookType.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, CbForegroundPtr, 0, 0,
                                                          User32.WindowsEventHookFlags.WINEVENT_OUTOFCONTEXT);

            MouseHookDelegate = MouseHookProc;
            CbMouseLlPtr      = Marshal.GetFunctionPointerForDelegate(MouseHookDelegate);
            MouseHook         = User32.SetWindowsHookEx(User32.WindowsHookType.WH_MOUSE_LL, CbMouseLlPtr,
                                                        Kernel32.GetModuleHandle(null).DangerousGetHandle(), 0);

            // Run standard event loop
            //EventLoop.Run();
        }
Пример #7
0
 static void StopListening()
 {
     s_oldHook.Dispose();
     s_oldHook = null;
 }
Пример #8
0
 public void UnHook()
 {
     hookId.Close();
     hookId = User32.SafeHookHandle.Null;
 }
 public void Run()
 {
     _hookHandle = User32.SetWindowsHookEx(User32.WindowsHookType.WH_KEYBOARD_LL, Callback, IntPtr.Zero, 0);
 }