private static IntPtr SetHook(WinHookCallback callback) { using (var process = Process.GetCurrentProcess()) using (var module = process.MainModule) { var name = module.ModuleName; var handle = GetModuleHandle(name); return(SetWindowsHookEx(WH_KEYBOARD_LL, callback, handle, 0)); } }
/// <summary> /// 安装Window钩子到主线程 /// </summary> /// <param name="hookType">钩子类型</param> /// <param name="callBack">回调函数</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <returns></returns> public static bool HookWindow(WinHookType hookType, WinHookCallback callBack) { if (callBack == null) { throw new ArgumentNullException("callBack"); } if (Enum.IsDefined(typeof(WinHookType), hookType) == false) { throw new ArgumentException("hookType的值无效"); } var callBackPointer = Marshal.GetFunctionPointerForDelegate(callBack); var state = Extern.HookWindow((int)hookType, callBackPointer); if (state == true) { ReferenceTable.AddRef(callBack); } return(state); }
public static extern IntPtr SetWindowsHookEx(int idHook, WinHookCallback lpfn, IntPtr hMod, uint dwThreadId);
public static extern IntPtr SetWindowsHookEx(WindowHookTypes idHook, WinHookCallback lpfn, IntPtr hmod, int dwThreadId);
public Interceptor() { processCallback = HookCallback; }
internal HotkeyHookManager() { this.hookCallback = LowLevelKeyboardProc; this.hookHandle = NativeMethods.SetWindowsHookExA(WH_KEYBOARD_LL, this.hookCallback, IntPtr.Zero, 0); }