Exemplo n.º 1
0
        /// <summary>
        /// 安装勾子
        /// <summary>
        /// <param name="hookProcess">外部调用的键盘处理事件</param>
        public void InstallHook(ProcessKeyHandle clientMethod)
        {
            _clientMethod = clientMethod;


            // 安装键盘钩子
            if (_hHookValue == 0)
            {
                _KeyBoardHookProcedure = new HookHandle(GetHookProc);


                _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);


                _hHookValue = SetWindowsHookEx(
                    WH_KEYBOARD_LL,
                    _KeyBoardHookProcedure,
                    _hookWindowPtr,
                    0);


                //如果设置钩子失败.
                if (_hHookValue == 0)
                {
                    UninstallHook();
                }
            }
        }
Exemplo n.º 2
0
 public void Start(ProcessKeyHandle clientMethod)
 {
     _clientMethod = clientMethod;
     // 安装键盘钩子
     if (hKeyboardHook == 0)
     {
         KeyboardHookProcedure = new HookProc(KeyboardHookProc);
         hKeyboardHook         = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
         //hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
         //************************************
         //键盘线程钩子
         //SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId());//指定要监听的线程idGetCurrentThreadId(),
         //键盘全局钩子,需要引用空间(using System.Reflection;)
         //SetWindowsHookEx( 13,MouseHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
         //
         //关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数:
         //idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13,
         //线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的
         //线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何
         //消息后便调用这个函数。hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子
         //程代码位于当前进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。threaded 与安装的钩子子程相关联的线程的标识符
         //如果为0,钩子子程与所有的线程关联,即为全局钩子
         //************************************
         //如果SetWindowsHookEx失败
         if (hKeyboardHook == 0)
         {
             Stop();
             throw new Exception("安装键盘钩子失败");
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 安装钩子处理方法
 /// </summary>
 /// <param name="keyhandle"></param>
 public void InstallHook(ProcessKeyHandle keyhandle)
 {
     if (!handles.Contains(keyhandle))
     {
         handles.Add(keyhandle);
     }
     //this.clientMethod = keyhandle;
     //GCHandle.Alloc(this.clientMethod, GCHandleType.Normal);
 }
Exemplo n.º 4
0
        /// <summary> 
        /// 安装勾子 
        /// </summary> 
        /// <param name="hookProcess">外部调用的键盘处理事件</param> 
        public void InstallHook(ProcessKeyHandle clientMethod)
        {
            _clientMethod = clientMethod;

            // 安装键盘钩子
            if (_hHookValue == 0) {
                _KeyBoardHookProcedure = new Win32.HookHandle(OnHookProc);

                _hookWindowPtr = Win32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

                _hHookValue = Win32.SetWindowsHookEx(
                WH_KEYBOARD_LL,
                _KeyBoardHookProcedure,
                _hookWindowPtr,
                0);

                //如果设置钩子失败.
                if (_hHookValue == 0) UninstallHook();
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 卸载钩子
 /// </summary>
 public void UninstallHook(ProcessKeyHandle keyhandle)
 {
     handles.Remove(keyhandle);
 }