一份C#的HOOK类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013613980/article/details/52319037

最近迷上玩手游,然后想写个鼠标宏,奈何网上的东西都不太敢用就自己写吧。这次尝了尝鲜用了C#,C++的Hook写习惯了再用C#感觉有点奇怪- -。

DWORD非得弄成intPtr和Int32,HANDLE非得写成指针实在是尴尬, 我在想64位编译器下直接打int会不会崩掉ORZ
不多说 直接贴代码

class MessageNode
    {
        public MessageNode(Int32 _nCode,Int32 _wParam,Int32 _lParam)
        {

            lParam = _lParam;
            wParam = _wParam;
            nCode = _nCode;
            Timestamp = DateTime.Now;
        }

        /// <summary>
        /// 时间戳 精确至毫秒
        /// </summary>
        public DateTime Timestamp { get; set; }     
        public Int32 lParam { get; set; }
        public Int32 wParam { get; set; }
        public Int32 nCode { get; set; }
    }

    class MessageHook
    {
        public const int WM_MOUSEMOVE = 0x200;
        public const int WM_LBUTTONDOWN = 0x201;
        public const int WM_RBUTTONDOWN = 0x204;
        public const int WM_MBUTTONDOWN = 0x207;
        public const int WM_LBUTTONUP = 0x202;
        public const int WM_RBUTTONUP = 0x205;
        public const int WM_MBUTTONUP = 0x208;
        public const int WM_LBUTTONDBLCLK = 0x203;
        public const int WM_RBUTTONDBLCLK = 0x206;
        public const int WM_MBUTTONDBLCLK = 0x209;
        public const int WM_KEYDOWN = 0x100;
        public const int WM_KEYUP = 0x101;

        public const int WH_MOUSE_LL = 14;
        public const int WH_KEYBOARD_LL = 13;

        public MessageHook()
        {
            hHook = 0;
            messageQueue = new Queue<MessageNode>();
            hookMessageSet = new HashSet<int>();
        }

        private Queue<MessageNode> messageQueue;
        private HashSet<Int32> hookMessageSet;
        private int hHook;

        public delegate Int32 HookProc(Int32 nCode, Int32 wParam, Int32 lParam);

        public static HookProc hookProc;


        [DllImport("User32.dll")]
        extern static  void UnhookWindowsHookEx(Int32 hWnd);

        [DllImport("User32.dll")]
        extern static Int32 SetWindowsHookEx(Int32 hookType, HookProc hookProc, IntPtr hModel, Int32 threadID);

        [DllImport("User32.dll")]
        extern static Int32 CallNextHookEx(Int32 hHook, Int32 nCode, Int32 wParam, Int32 lParam);

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        Int32 AddMessageToQueue(Int32 nCode, Int32 wParam, Int32 lParam)
        {
            if (hookMessageSet.Contains(wParam)) 
                messageQueue.Enqueue(new MessageNode(nCode, wParam, lParam));
            return CallNextHookEx(hHook, nCode, wParam, lParam);
        }

        public bool Start(Int32 hookType)
        {
            if (hHook != 0)
                Stop();

            hookProc = new HookProc(AddMessageToQueue);
            IntPtr ptr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

           // MessageBox.Show(ptr + "");

            hHook = SetWindowsHookEx(hookType, hookProc,ptr , 0);

            return hHook != 0;
        }

        public void Stop()
        {
            UnhookWindowsHookEx(hHook);
            hHook = 0;
        }

        public MessageNode GetAMessage()
        {
            return messageQueue.Dequeue();
        }

        public void AddHookMessageType(Int32 messageType)
        {
            hookMessageSet.Add(messageType);
        }

        public bool Empty { get { return messageQueue.Count == 0; } }
    }
展开阅读全文

没有更多推荐了,返回首页