/// <summary> /// 安装钩子 /// </summary> /// <returns></returns> public bool InstallHook(string flagsinfo) { this.flags = flagsinfo; IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule); //pInstance = (IntPtr)4194304; // IntPtr pInstanc2 = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly()); // Assembly.GetExecutingAssembly().GetModules()[0] // 假如没有安装鼠标钩子 if (this.m_pMouseHook == IntPtr.Zero) { this.m_MouseHookProcedure = new HookProc(this.MouseHookProc); this.m_pMouseHook = Win32API.SetWindowsHookEx(WH_Codes.WH_MOUSE_LL, this.m_MouseHookProcedure, pInstance, 0); if (this.m_pMouseHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } if (this.m_pKeyboardHook == IntPtr.Zero) { this.m_KeyboardHookProcedure = new HookProc(this.KeyboardHookProc); this.m_pKeyboardHook = Win32API.SetWindowsHookEx(WH_Codes.WH_KEYBOARD_LL, this.m_KeyboardHookProcedure, pInstance, 0); if (this.m_pKeyboardHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } return(true); }
///<summary> ///安装钩子 ///</summary> ///<returns></returns> public bool InstallHook(string flagsinfo) { this.flags = flagsinfo; IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule); //pInstance = (IntPtr)4194304; //注意:很多时候得到的pInstanc无法安装钩子,请检查值是否为4194304,在应用程序中可以直接取得pinstance的 // IntPtr pInstanc2 = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly()); // Assembly.GetExecutingAssembly().GetModules()[0] //安装鼠标钩子 if (this.m_pMouseHook == IntPtr.Zero) { this.m_MouseHookProcedure = new HookProc(this.MouseHookProc); this.m_pMouseHook = Win32API.SetWindowsHookEx(WH_Codes.WH_KEYBOARD_LL, this.m_MouseHookProcedure, pInstance, 0); // WH_Codes.WH_MOUSE_LL为全局钩子即系统钩子,否则应该为WH_Codes.WH_KEYBOARD,即普通钩子 if (this.m_pMouseHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } if (this.m_pKeyboardHook == IntPtr.Zero) //安装键盘钩子 { this.m_KeyboardHookProcedure = new HookProc(this.KeyboardHookProc); // WH_Codes.WH_KEYBOARD_LL为全局钩子即系统钩子,否则应该为WH_Codes.WH_KEYBOARD,即普通钩子 this.m_pKeyboardHook = Win32API.SetWindowsHookEx(WH_Codes.WH_KEYBOARD_LL, this.m_KeyboardHookProcedure, pInstance, 0); if (this.m_pKeyboardHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } return(true); }