现在的位置: 首页 > 综合 > 正文

局部钩子

2017年11月06日 ⁄ 综合 ⁄ 共 5950字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace Hook
{
    public class Class1
    {
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct tagCWPSTRUCT
    {
        public IntPtr lparam;
        public IntPtr wparam;
        public int message;
        public IntPtr hwnd;
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct LVITEM
    {
        public int mask;
        public int iItem;
        public int iSubItem;
        public int state;
        public int stateMask;
        public string pszText; // string
        public int cchTextMax;
        public int iImage;
        public IntPtr lParam;
        public int iIndent;
        public int iGroupId;
        public int cColumns;
        public IntPtr puColumns;
    }
    public class GlobalHook
    {
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        //public GlobalHook()
        //{
        //    //Start();
        //}

        //~GlobalHook()
        //{
        //    //Stop();
        //}

        public void ks()
        {
            Start();

        }

        public void js()
        {
            Stop();
        }

 

        static int hMouseHook = 0; //Declare mouse hook handle as int.

        //values from Winuser.h in Microsoft SDK.
        public const int WH_CALLWNDPROC = 4; //mouse hook constant
        public const int LVM_FIRST = 0x1000;
        public const int LVM_INSERTITEM = LVM_FIRST + 7;//1列     
        public const int LVM_INSERTITEMW = LVM_FIRST + 77;//1列
        public const int LVM_SETITEMTEXT = LVM_FIRST + 46;//2列
        public const int LVM_SETITEMTEXTW = LVM_FIRST + 116;//2列
        HookProc MouseHookProcedure; //Declare MouseHookProcedure as HookProc type.
       

        //Import for SetWindowsHookEx function.
        //Use this function to install a hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);

        //Import for UnhookWindowsHookEx.
        //Call this function to uninstall the hook.
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //Import for CallNextHookEx.
        //Use this function to pass the hook information to next hook procedure in chain.
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
            IntPtr wParam, IntPtr lParam);

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

        public void Start()
        {
            // install Mouse hook
            if (hMouseHook == 0)
            {
                // Create an instance of HookProc.
                MouseHookProcedure = new HookProc(MouseHookProc);

                try
                {
//                    hMouseHook = SetWindowsHookEx(WH_CALLWNDPROC,
//                        MouseHookProcedure,
//                        IntPtr.Zero,//GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName)
////Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                    //                        AppDomain.GetCurrentThreadId());   GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName)
                    IntPtr h = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
                    hMouseHook = SetWindowsHookEx(4, MouseHookProcedure, h, 0);
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.ToString());
                }

                //If SetWindowsHookEx fails.
                if (hMouseHook == 0)
                {
                    Stop();
                    MessageBox.Show("SetWindowsHookEx failed.");
                }
            }

        }
        public void Start2()
        {
            // install Mouse hook
            if (hMouseHook == 0)
            {
                // Create an instance of HookProc.
                MouseHookProcedure = new HookProc(MouseHookProc);

                try
                {
                    hMouseHook = SetWindowsHookEx(WH_CALLWNDPROC,
                        MouseHookProcedure,
                        IntPtr.Zero,//GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName)
                        //Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
                        AppDomain.GetCurrentThreadId());

                }
                catch (Exception err)
                {
                    //MessageBox.Show(err.ToString());
                }

                //If SetWindowsHookEx fails.
                if (hMouseHook == 0)
                {
                    Stop();
                    MessageBox.Show("SetWindowsHookEx failed.");
                }
            }

        }
        public void Stop()
        {
            bool retMouse = true;
            if (hMouseHook != 0)
            {
                retMouse = UnhookWindowsHookEx(hMouseHook);
                hMouseHook = 0;
            }

          
            //If UnhookWindowsHookEx fails.
            if (!retMouse)
                throw new Exception("UnhookWindowsHookEx failed.");
        }

        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lparam)
        {
            tagCWPSTRUCT lp = (tagCWPSTRUCT)Marshal.PtrToStructure(lparam, typeof(tagCWPSTRUCT));

          // if(lp.message>0x1000)
           // MessageBox.Show(lp.ToString());

           if (lp.message == LVM_INSERTITEM || lp.message == LVM_INSERTITEMW)    

         {       
          //LVITEM *pLvitem = (LVITEM*)p->lParam;
                MessageBox.Show("范围是A");
          //::MessageBox(NULL, "ok", "LVM_INSERTITEM", MB_SYSTEMMODAL | MB_ICONERROR );
         }
           else if (lp.message == LVM_SETITEMTEXT || lp.message == LVM_SETITEMTEXTW)    

         {
                MessageBox.Show("范围是B");
                //LVITEM *pLvitem = (LVITEM*)p->lParam;
         // ::MessageBox(NULL, "ok", "LVM_SETITEMTEXT", MB_SYSTEMMODAL | MB_ICONERROR );
         }
            return CallNextHookEx(hMouseHook, nCode, wParam, lparam);
        }
      

    }
}

抱歉!评论已关闭.