Exemplo n.º 1
0
        protected void Install()
        {
            // make sure not already installed
            if (_HookHandle != IntPtr.Zero)
            {
                return;
            }

            var mod = ModuleHelper.GetCurrentModule();

            // install system-wide hook
            _HookHandle = HookAPI.SetWindowsHookEx(
                _HookType,
                _HookFunction,
                mod.BaseAddress,
                0);

            if (_HookHandle == IntPtr.Zero)
            {
                try
                {
                    EventLog.WriteEntry(
                        "HookApp",
                        string.Format("Failed to set hook. Error {0}.", Marshal.GetLastWin32Error()),
                        EventLogEntryType.Warning);
                }
                catch
                {; }
            }
        }
Exemplo n.º 2
0
 protected void Uninstall()
 {
     if (_HookHandle != IntPtr.Zero)
     {
         // uninstall system-wide hook
         HookAPI.UnhookWindowsHookEx(_HookHandle);
         _HookHandle = IntPtr.Zero;
     }
 }
Exemplo n.º 3
0
        // hook function called by system
        protected int HookCallback(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code < 0)
            {
                return(HookAPI.CallNextHookEx(_HookHandle, code, wParam, lParam));
            }

            HookParam structData = (HookParam)Marshal.PtrToStructure(lParam, typeof(HookParam));

            if (ProcessHook(wParam, structData))
            {
                return(HookAPI.CallNextHookEx(_HookHandle, code, wParam, lParam));
            }

            return(-1);
        }