Beispiel #1
0
        protected virtual IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam)
        {
            IntPtr result = UNHANDLED;

            if (pfnClientHookProc != null)
            {
                try
                {
                    // try to ensure CallNextHookEx is always called, even if client
                    // hook proc code or MarhsalToStructure except
                    result = pfnClientHookProc(code, wParam, lParam);
                }
                catch (System.Exception ex)
                {
                    LocalStaticLogger.WriteLine(ex.ToString());
                }
            }

            try
            {
                // only call CallNextHook if client hook proc did not run or did run but didn't return handled
                if (result == UNHANDLED)
                {
                    result = Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam);
                }
            }
            catch (System.Exception ex)
            {
                LocalStaticLogger.WriteLine(ex.ToString());
            }

            return(result);
        }
Beispiel #2
0
        protected override IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam)
        {
            try
            {
                bool processed = ThreadPool.QueueUserWorkItem(new WaitCallback(ClientHookProcInvoker), new WindowHookProcArgs(code, wParam, lParam));
            }
            catch (NotSupportedException nse)
            {
                LocalStaticLogger.WriteLine(nse.ToString());
            }

            return(Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam));
        }
Beispiel #3
0
        public static bool UnhookWindowsHook(IntPtr hHook)
        {
            bool result = false;

            try
            {
                result = Win32HookAPI.UnhookWindowsHookEx(hHook);
            }
            catch (System.Exception ex)
            {
                LocalStaticLogger.WriteLine(ex.ToString());
            }

            return(result);
        }
Beispiel #4
0
        // http://msdn.microsoft.com/en-us/library/windows/desktop/ms644985%28v=vs.85%29.aspx:
        // If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx.
        // If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly
        // recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that
        // have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result.
        // If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing
        // the message to the rest of the hook chain or the target window procedure.

        protected override IntPtr InternalHookProc(int code, IntPtr wParam, IntPtr lParam)
        {
            IntPtr result = IntPtr.Zero;

            // protect this instance from ever calling InternalHookProc
            // at the same time
            lock (lockObj)
            {
                if (code >= 0)
                {
                    if (pfnClientHookProc != null)
                    {
                        try
                        {
                            // protected ourselves from exceptions thrown in client hook procedures
                            LowLevelKeyboardHookProcArgs args = new LowLevelKeyboardHookProcArgs(code, wParam, lParam);
                            result = pfnClientHookProc(args.HookCode, args.KeyEvent, args.Info);
                        }
                        catch (Exception e)
                        {
                            LocalStaticLogger.WriteLine(e.ToString());
                        }
                    }
                }

                try
                {
                    // try to ensure CallNextHookEx is always called, even if client
                    // hook proc code or MarhsalToStructure except
                    if (result == IntPtr.Zero)
                    {
                        result = Win32HookAPI.CallNextHookEx(hHook, code, wParam, lParam);
                    }
                }
                catch (Exception e)
                {
                    LocalStaticLogger.WriteLine(e.ToString());
                }
            }

            return(result);
        }
Beispiel #5
0
        public static IntPtr SetWindowsHook(HookType hookType, WindowHookProc lpfnHookProc)
        {
            try
            {
                using (var currentProcess = Process.GetCurrentProcess())
                {
                    using (var currentModule = currentProcess.MainModule)
                    {
                        IntPtr hModule = Win32HookAPI.GetModuleHandle(currentModule.ModuleName);
                        if (hModule != IntPtr.Zero)
                        {
                            return(Win32HookAPI.SetWindowsHookEx(hookType, lpfnHookProc, hModule, 0));
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                LocalStaticLogger.WriteLine(ex.ToString());
            }

            return(IntPtr.Zero);
        }