private void Dispose(bool disposing) { if (disposing) { KeyboardHookAction -= LowLevelKeyboardAction; MouseHookAction -= LowLevelMouseAction; WindowHookAction -= WindowChangedAction; if (KeyboardHookHandle != IntPtr.Zero && !User32.UnhookWindowsHookEx(KeyboardHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (MouseHookHandle != IntPtr.Zero && !User32.UnhookWindowsHookEx(MouseHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (WindowHookHandle != IntPtr.Zero && !User32.UnhookWinEvent(WindowHookHandle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } if (User32Handle != IntPtr.Zero && !Kernel32.FreeLibrary(User32Handle)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
public GlobalHook() { KeyboardHookAction = LowLevelKeyboardAction; MouseHookAction = LowLevelMouseAction; WindowHookAction = WindowChangedAction; KeyboardHookHandle = IntPtr.Zero; MouseHookHandle = IntPtr.Zero; WindowHookHandle = IntPtr.Zero; // load User32 if ((User32Handle = Kernel32.LoadLibrary("User32")) == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } KeyBinding.Load(); }