private static void CheckHook(object obj, EventArgs args) { HOOK old_hook = m_hook; // Reinstall the hook in case Windows disabled it without telling us. It’s // OK to have two hooks installed for a short time, because we check for // recursive calls to ourselves in OnKey(). if (m_active) { m_hook = NativeMethods.SetWindowsHookEx(WH.KEYBOARD_LL, m_callback, NativeMethods.LoadLibrary("user32.dll"), 0); if (m_hook == HOOK.INVALID) { Log.Debug("Unable to install hook: {0}", new Win32Exception(Marshal.GetLastWin32Error())); } } else { m_hook = HOOK.INVALID; } if (old_hook != HOOK.INVALID) { // XXX: this will crash if the hook is not removed from the same // thread that installed it. int ret = NativeMethods.UnhookWindowsHookEx(old_hook); if (ret == 0) { Log.Debug("Unable to uninstall hook: {0}", new Win32Exception(Marshal.GetLastWin32Error())); } } }
public static void Fini() { // XXX: this will crash if called from the GC Finalizer Thread because // the hook needs to be removed from the same thread that installed it. if (m_hook != HOOK.INVALID) { int ret = NativeMethods.UnhookWindowsHookEx(m_hook); if (ret == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); m_hook = HOOK.INVALID; } m_callback = null; }
public static void Init() { if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32S || Environment.OSVersion.Platform == PlatformID.Win32Windows || Environment.OSVersion.Platform == PlatformID.WinCE) { m_callback = OnKey; // Keep a reference on OnKey m_hook = NativeMethods.SetWindowsHookEx(WH.KEYBOARD_LL, m_callback, NativeMethods.LoadLibrary("user32.dll"), 0); if (m_hook == HOOK.INVALID) throw new Win32Exception(Marshal.GetLastWin32Error()); } }
public static extern int CallNextHookEx(HOOK hhk, HC nCode, WM wParam, IntPtr lParam);
public static extern int UnhookWindowsHookEx(HOOK hhk);