public static extern IntPtr SetWindowsHookEx ( int hookid, HookPro pfnhook, IntPtr hinst, int threadid );
/// <summary> /// 设置钩子 /// </summary> public void SET_WINDOWS_KEYBOARD_HOOK() { if (hHook == IntPtr.Zero) { HookPro hk = new HookPro(this.KEYBOARD_HOOKPRO); _hookProcHandle = GCHandle.Alloc(hk); //挂钩子 hHook = HookApi.SetWindowsHookEx( WH_KEYBOARD_LL, hk, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (hHook == IntPtr.Zero) { throw new Exception("安装钩子不成功!"); // 挂钩子不成功返回值 0 } } }
/// <summary> /// 设置钩子....调用此方法即可装载钩子 /// </summary> private bool SET_WINDOWS_KEYBOARD_HOOK() { if (hHook == IntPtr.Zero) { HookPro hk = new HookPro(this.KEYBOARD_HOOKPRO); _hookProcHandle = GCHandle.Alloc(hk); hHook = SetWindowsHookEx( WH_KEYBOARD_LL, hk, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (hHook == IntPtr.Zero) { return(false); } return(true); } return(false); }