Example #1
0
        private void HookMessageProcedure()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            _threadId = (uint)AppDomain.GetCurrentThreadId();
#pragma warning restore CS0618 // Type or member is obsolete

            _keyboardHookProcedure = new HookProcedure(LowLevelKeyboardProcedure);
            _keyboardHookHandle    = SetWindowsHookEx(HookType.WH_KEYBOARD_LL, _keyboardHookProcedure, UtilInterop.GetModuleHandle(null), 0);

            var message = new MSG();

            while (_keyboardHookRunning)
            {
                var result = GetMessage(out message, IntPtr.Zero, 0, 0);
                if (result == -1)
                {
                    break;
                }

                TranslateMessage(ref message);

                if (message.message == (int)UtilInterop.WindowMessage.WM_QUIT)
                {
                    break;
                }
            }

            UnhookWindowsHookEx(_keyboardHookHandle);
            _keyboardHookHandle = IntPtr.Zero;
        }
 public MouseHook(IntPtr hMod, IntPtr hThread)
 {
     _hMod     = hMod;
     _hThread  = hThread;
     _hHook    = IntPtr.Zero;
     _hookProc = new HookProcedure(MouseHookProcedure);
 }
Example #3
0
        public void Start()
        {
            _mouseHookProcedure = new HookProcedure(LowLevelMouseProcedure);

            _running       = true;
            _messageThread = new Thread(MessageThreadRoutine);
            _messageThread.Start();
        }
Example #4
0
        // Token: 0x06000317 RID: 791 RVA: 0x0000F43C File Offset: 0x0000D63C
        private static HookResult HookGlobal(int hookId, Callback callback)
        {
            HookProcedure       hookProcedure       = (int code, IntPtr param, IntPtr lParam) => HookHelper.HookProcedure(code, param, lParam, callback);
            HookProcedureHandle hookProcedureHandle = HookNativeMethods.SetWindowsHookEx(hookId, hookProcedure, Process.GetCurrentProcess().MainModule.BaseAddress, 0);

            if (hookProcedureHandle.IsInvalid)
            {
                HookHelper.ThrowLastUnmanagedErrorAsException();
            }
            return(new HookResult(hookProcedureHandle, hookProcedure));
        }
Example #5
0
        // Token: 0x06000316 RID: 790 RVA: 0x0000F3EC File Offset: 0x0000D5EC
        private static HookResult HookApp(int hookId, Callback callback)
        {
            HookProcedure       hookProcedure       = (int code, IntPtr param, IntPtr lParam) => HookHelper.HookProcedure(code, param, lParam, callback);
            HookProcedureHandle hookProcedureHandle = HookNativeMethods.SetWindowsHookEx(hookId, hookProcedure, IntPtr.Zero, ThreadNativeMethods.GetCurrentThreadId());

            if (hookProcedureHandle.IsInvalid)
            {
                HookHelper.ThrowLastUnmanagedErrorAsException();
            }
            return(new HookResult(hookProcedureHandle, hookProcedure));
        }
Example #6
0
 public InputInterceptor()
 {
     try
     {
         // Store delegate to prevent it's been garbage collected
         hookProcedure = LowLevelHookProc;
         thread.Start(MessagePumpInit);
     }
     catch
     {
         Dispose();
         throw;
     }
 }
Example #7
0
        private static HookResult HookGlobal(int hookId, Callback callback)
        {
            _globalHookProc = (code, param, lParam) => HookProcedure(code, param, lParam, callback);

            var hookHandle = HookNativeMethods.SetWindowsHookEx(
                hookId,
                _globalHookProc,
                Process.GetCurrentProcess().MainModule.BaseAddress,
                0);

            if (hookHandle.IsInvalid)
            {
                ThrowLastUnmanagedErrorAsException();
            }

            return new HookResult(hookHandle, _globalHookProc);
        }
Example #8
0
        private static HookResult HookApp(int hookId, Callback callback)
        {
            _appHookProc = (code, param, lParam) => HookProcedure(code, param, lParam, callback);

            var hookHandle = HookNativeMethods.SetWindowsHookEx(
                hookId,
                _appHookProc,
                IntPtr.Zero,
                ThreadNativeMethods.GetCurrentThreadId());

            if (hookHandle.IsInvalid)
            {
                ThrowLastUnmanagedErrorAsException();
            }

            return new HookResult(hookHandle, _appHookProc);
        }
Example #9
0
        private static HookResult HookApp(int hookId, Callback callback)
        {
            _appHookProc = (code, param, lParam) => HookProcedure(code, param, lParam, callback);

            var hookHandle = HookNativeMethods.SetWindowsHookEx(
                hookId,
                _appHookProc,
                IntPtr.Zero,
                ThreadNativeMethods.GetCurrentThreadId());

            if (hookHandle.IsInvalid)
            {
                ThrowLastUnmanagedErrorAsException();
            }

            return(new HookResult(hookHandle, _appHookProc));
        }
Example #10
0
        private static HookResult HookGlobal(int hookId, Callback callback)
        {
            _globalHookProc = (code, param, lParam) => HookProcedure(code, param, lParam, callback);

            var hookHandle = HookNativeMethods.SetWindowsHookEx(
                hookId,
                _globalHookProc,
                Process.GetCurrentProcess().MainModule.BaseAddress,
                0);

            if (hookHandle.IsInvalid)
            {
                ThrowLastUnmanagedErrorAsException();
            }

            return(new HookResult(hookHandle, _globalHookProc));
        }
Example #11
0
        public static HookHandle Create(HookType idHook, HookProcedure lpfn, IntPtr hMod, int dwThreadId)
        {
            var FunctionPointer = GCHandle.Alloc(lpfn);

            var ret = HookNativeMethods.SetWindowsHookEx(idHook, lpfn, hMod, dwThreadId);

            if (ret.IsInvalid)
            {
                FunctionPointer.Free();
                ThrowLastUnmanagedErrorAsException();
            }
            else
            {
                ret.FunctionPointer = FunctionPointer;
            }

            return(ret);
        }
Example #12
0
 protected LowLevelHook(HookType type)
 {
     _type             = type;
     _hHook            = IntPtr.Zero;
     _internalCallback = InternalCallback;
 }
Example #13
0
 /// <summary>
 /// Make sure <param name="procedure">procedure</param> is not cleaned up by the garbage collector. Otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException.
 ///
 /// </summary>
 /// <param name="procedure"></param>
 /// <returns></returns>
 public static IntPtr HookGlobal(HookProcedure procedure)
 {
     return(HookManager.GlobalHook(HookType.KeyboardGlobal, procedure));
 }
Example #14
0
 internal extern static IntPtr SetWindowsHookEx(Int32 idHook, HookProcedure hookProc, IntPtr hMod, IntPtr hThread);
Example #15
0
 private static extern IntPtr SetWindowsHookEx(
     HookType type,
     HookProcedure lpfn,
     IntPtr hMod,
     int dwThreadId);
Example #16
0
 public HookResult(HookProcedureHandle handle, HookProcedure procedure)
 {
     m_Handle = handle;
     m_Procedure = procedure;
 }
Example #17
0
 /// <summary>
 /// Make sure <param name="procedure">procedure</param> is not cleaned up by the garbage collector. Otherwise the garbage collector will clean up your hook delegate eventually, resulting in your code throwing a System.NullReferenceException.
 ///
 /// </summary>
 /// <param name="procedure"></param>
 /// <returns></returns>
 public static IntPtr GlobalHook(HookType type, HookProcedure procedure)
 {
     return(User32.SetWindowsHookEx((int)type, procedure, IntPtr.Zero, 0));
 }
Example #18
0
 public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProcedure hookProcedure, IntPtr moduleHandle, UInt32 threadID);
Example #19
0
 internal static extern HookProcedureHandle SetWindowsHookEx(
     int idHook,
     HookProcedure lpfn,
     IntPtr hMod,
     int dwThreadId);
Example #20
0
 public Win32()
 {
     hookProcedure = new HookProcedure(ProcessHook);
 }
Example #21
0
 static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure callback, IntPtr hInstance, uint theardID);
Example #22
0
 private static IntPtr SetHook(HookProcedure procedure)
 {
     using (Process process = Process.GetCurrentProcess())
         using (ProcessModule module = process.MainModule)
             return(SetWindowsHookEx(WH_KEYBOARD_LL, procedure, GetModuleHandle(module.ModuleName), 0));
 }
Example #23
0
 public HookResult(HookProcedureHandle handle, HookProcedure procedure)
 {
     m_Handle    = handle;
     m_Procedure = procedure;
 }
Example #24
0
 public static extern IntPtr SetWindowsHookEx(int idHook, HookProcedure lpfn, IntPtr hMod, uint dwThreadId);
Example #25
0
 internal static extern HookProcedureHandle SetWindowsHookEx(
     int idHook,
     HookProcedure lpfn,
     IntPtr hMod,
     int dwThreadId);
 public static extern HookHandle SetWindowsHookEx(
     HookType idHook,
     HookProcedure lpfn,
     IntPtr hMod,
     int dwThreadId);