Hook.cs
上传用户:yzllxs
上传日期:2020-11-17
资源大小:530k
文件大小:5k
源码类别:

中间件编程

开发平台:

C#

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Reflection;
  6. using System.IO;
  7. // Download by http://www.codefans.net
  8. namespace HookLock
  9. {
  10.     public class Hook
  11.     {
  12.         #region 私有变量
  13.         /// <summary>
  14.         /// 键盘钩子句柄
  15.         /// </summary>
  16.         private IntPtr m_pKeyboardHook = IntPtr.Zero;
  17.         /// <summary>
  18.         /// 钩子委托声明
  19.         /// </summary>
  20.         /// <param name="nCode"></param>
  21.         /// <param name="wParam"></param>
  22.         /// <param name="lParam"></param>
  23.         /// <returns></returns>
  24.         public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
  25.         /// <summary>
  26.         /// 键盘钩子委托实例
  27.         /// </summary>
  28.         /// <remarks>
  29.         /// 不要试图省略此变量,否则将会导致
  30.         /// 激活 CallbackOnCollectedDelegate 托管调试助手 (MDA)。 
  31.         /// 详细请参见MSDN中关于 CallbackOnCollectedDelegate 的描述
  32.         /// </remarks>
  33.         private HookProc m_KeyboardHookProcedure;
  34.         // 底层键盘钩子
  35.         public const int idHook = 13;
  36.         /// <summary>
  37.         /// 安装钩子
  38.         /// </summary>
  39.         /// <param name="idHook"></param>
  40.         /// <param name="lpfn"></param>
  41.         /// <param name="hInstance"></param>
  42.         /// <param name="threadId"></param>
  43.         /// <returns></returns>
  44.         [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  45.         public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn,
  46.             IntPtr pInstance, int threadId);
  47.         /// <summary>
  48.         /// 卸载钩子
  49.         /// </summary>
  50.         /// <param name="idHook"></param>
  51.         /// <returns></returns>
  52.         [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  53.         public static extern bool UnhookWindowsHookEx(IntPtr pHookHandle);
  54.         /// <summary>
  55.         /// 传递钩子
  56.         /// </summary>
  57.         /// <param name="pHookHandle">是您自己的钩子函数的句柄。用该句柄可以遍历钩子链</param>
  58.         /// <param name="nCode">把传入的参数简单传给CallNextHookEx即可</param>
  59.         /// <param name="wParam">把传入的参数简单传给CallNextHookEx即可</param>
  60.         /// <param name="lParam"></param>
  61.         /// <returns></returns>
  62.         [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  63.         public static extern int CallNextHookEx(IntPtr pHookHandle, int nCode,
  64.             Int32 wParam, IntPtr lParam);
  65.         #endregion 私有变量
  66.         #region 私有方法
  67.         /// <summary>
  68.         /// 键盘钩子处理函数
  69.         /// </summary>
  70.         /// <param name="nCode"></param>
  71.         /// <param name="wParam"></param>
  72.         /// <param name="lParam"></param>
  73.         /// <returns></returns>
  74.         /// <remarks>此版本的键盘事件处理不是很好,还有待修正.</remarks>
  75.         private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
  76.         {
  77.             //return 1;
  78.             KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
  79.             if (m_pKeyboardHook != IntPtr.Zero)
  80.             {
  81.                 switch (((Keys)m.vkCode))
  82.                 {
  83.                     case Keys.LWin:
  84.                     case Keys.RWin:
  85.                     case Keys.Delete:
  86.                     case Keys.Alt:
  87.                     case Keys.Escape:
  88.                     case Keys.F4:
  89.                     case Keys.Control:
  90.                     case Keys.Tab:
  91.                         return 1;
  92.                 }
  93.             }
  94.             return 0;
  95.         }
  96.         #endregion 私有方法
  97.         #region 公共方法
  98.         /// <summary>
  99.         /// 安装钩子
  100.         /// </summary>
  101.         /// <returns></returns>
  102.         public bool InstallHook()
  103.         {
  104.             //IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
  105.             IntPtr pInstance = (IntPtr)4194304;
  106.             if (this.m_pKeyboardHook == IntPtr.Zero)
  107.             {
  108.                 this.m_KeyboardHookProcedure = new HookProc(KeyboardHookProc);
  109.                 this.m_pKeyboardHook = SetWindowsHookEx(idHook, m_KeyboardHookProcedure, pInstance, 0);
  110.                 if (this.m_pKeyboardHook == IntPtr.Zero)
  111.                 {
  112.                     this.UnInstallHook();
  113.                     return false;
  114.                 }
  115.             }
  116.             return true;
  117.         }
  118.         /// <summary>
  119.         /// 卸载钩子
  120.         /// </summary>
  121.         /// <returns></returns>
  122.         public bool UnInstallHook()
  123.         {
  124.             bool result = true;
  125.             if (this.m_pKeyboardHook != IntPtr.Zero)
  126.             {
  127.                 result = (UnhookWindowsHookEx(this.m_pKeyboardHook) && result);
  128.                 this.m_pKeyboardHook = IntPtr.Zero;
  129.             }
  130.             return result;
  131.         }
  132.         [StructLayout(LayoutKind.Sequential)]
  133.         public struct KeyMSG
  134.         {
  135.             public int vkCode;
  136.             public int scanCode;
  137.             public int flags;
  138.             public int time;
  139.             public int dwExtraInfo;
  140.         }
  141.         #endregion 公共方法
  142.     }
  143. }