Net实现钩子函数(Hook)以及通过SendMessage实现自动点击按钮和给文本框赋值

原文:Net实现钩子函数(Hook)以及通过SendMessage实现自动点击按钮和给文本框赋值

1.实现钩子函数

钩子(Hook)的实现需要三个主要的函数和一个委托

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//设置系统钩子

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook);//卸载系统钩子

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);//调用下一个钩子函数

public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);//用于处理Hook住的消息

当我们在执行一个操作的时候,首先不是由我们的窗体获得消息,而是系统获得,然后系统再把消息发送到对应的窗体,Hook就是在窗体获取到信息之前抓住信息,然后对信息进行处理,然后可以传递给船体继续执行,或者就不传递给窗体

当在HookProc处理消息的时候,如果return 1,那么消息就会被截断,不会再传递到目标窗口,如果return的是CallNextHookEx那么就会继续调用下一个钩子,如果下面没有钩子了,那么消息就会被传递到目标窗体进行处理

SetWindowsHookEx第一个参数是需要勾住的消息类型,总共14种消息类型,如下

public const int WH_JOURNALRECORD = 0;
public const int constWH_JOURNALPLAYBACK = 1;
public const int WH_KEYBOARD = 2;
public const int WH_GETMESSAGE = 3;
public const int WH_CALLWNDPROC = 4;
public const int WH_CBT = 5;
public const int WH_SYSMSGFILTER = 6;
public const int WH_MOUSE = 7;
public const int WH_HARDWARE = 8;
public const int WH_DEBUG = 9;
public const int WH_SHELL = 10;
public const int WH_FOREGROUNDIDLE = 11;
public const int WH_CALLWNDPROCRET = 12;
public const int WH_KEYBOARD_LL = 13;
public const int WH_MOUSE_LL = 14;

第二个参数就是HookProc委托,用于对钩住的消息进行处理,

第三个参数是需要钩住的实例的句柄,最后一个是钩住的线程,如果是0则是全局钩住

返回值为抓住的钩子的ID

UnhookWindowsHookEx卸载掉钩子,参数为上面返回的ID

 

辅助函数

[DllImport("kernel32.dll")]

public static extern IntPtr GetModuleHandle(string name);//根据模块名称获取到对应的句柄

[DllImport("user32.dll", EntryPoint = "FindWindow")]

private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//查询一个窗体

[DllImport("User32.dll", EntryPoint = "FindWindowEx")]

private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);//获取窗体中的所有子窗体(文本框,按钮等,都属于窗体)

 

        [DllImport("user32.dll")]

        public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);//枚举窗体中的所有子窗体

public delegate bool CallBack(IntPtr hwnd, int lParam);

此委托是EnumChildWindows的回调函数,用于遍历的时候对窗口进行处理

 

根据Module的名字获取到对应的句柄SetWindowsHookEx的第三个参数可以使用这个函数来获得。

下面是一个示例程序,设置一个全局钩子,作用是,如果输入的字符是小写字母,则直接转换为大写字母。

1.1  HookProc的方法实现

  private int MessageHandle(int nCode, Int32 wParam, IntPtr lParam)

        {

            if (0x100 == wParam || 0x101 == wParam)  //如果按键为按下状态,如果没有这句判断,则内部代码会执行两遍,一遍是KeyDown一遍是KeyUp

            {

                KBDLLHOOKSTRUCT ks = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));

               //将所有的小写字母直接加1

                if (ks.vkCode >= 65 && ks.vkCode <= 90)

                {

                    string cUpper = Convert.ToChar(ks.vkCode).ToString().ToUpper();

                    SendMessage(txtHandle, 0x0c, IntPtr.Zero, cUpper);

                }

            }

            return CallNextHookEx(result, nCode, 0, lParam);

 

        }

1.2 KBDLLHOOKSTRUCT结构体(这个结构体因为不同的钩子内容会不一样)

public struct KBDLLHOOKSTRUCT

    {

        public int vkCode;

        public int scanCode;

        public int flags;

        public int time;

        public IntPtr dwExtraInfo;

    }

 

1.3设置钩子和卸载钩子(两个按钮的事件)

  private void btnInstallHook_Click(object sender, EventArgs e)

        {

            HookProc hProc = new HookProc(MessageHandle);

            IntPtr cInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName);

            result = SetWindowsHookEx(HookHelper.WH_KEYBOARD_LL, hProc, cInstance, 0);

        }

 

        private void btnUnhook_Click(object sender, EventArgs e)

        {

            UnhookWindowsHookEx(result);

        }

辅助方法:为了获取到窗体中的文本框的句柄

//枚举窗体中的子窗体的回调函数

        private bool EnumWindow(IntPtr hwnd, int lParam)

        {

            StringBuilder sb=new StringBuilder();

            GetWindowText(hwnd, sb, 10);

            if (sb.ToString() == "HookTest")

            {

                txtHandle = hwnd;

            }

            return true;

        }

 

 

2.SendMessage的使用

可以使用SendMessage模拟给发送一条系统消息

[DllImport("user32.dll", EntryPoint = "SendMessage")]

        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

 

        [DllImport("User32.dll", EntryPoint = "SendMessage")]

        private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);//发送消息,此重载方法可以直接给文本框赋值

 

下面是一个自动点击按钮和自动给文本框赋值的示例

 

   private void btnTest_Click(object sender, EventArgs e)

        {

            #region 自动点击按钮

            //IntPtr cProcess = FindWindow(null, "测试Hook");

            //winHandle = FindWindowEx(cProcess, IntPtr.Zero, null, "点击显示界面");

            ////SendMessage(winHandle, 0xf5, 0, 0);//0xf5 BM_CLICK 按钮单击对应的消息--经过测试,直接使用0xf5无法实现点击按钮的功能    

            ////测试结果发现,如果想要实现单击按钮的功能,必须先按下鼠标左键,再抬起鼠标左键

            //SendMessage(winHandle, 0x201, IntPtr.Zero, IntPtr.Zero);//0x201 WM_LBUTTONDOWN 按下鼠标左键对应的消息

            //SendMessage(winHandle, 0x202, IntPtr.Zero, IntPtr.Zero);//0x201 WM_LBUTTONUP 抬起鼠标左键对应的消息

            #endregion

 

            #region 自动输入文本

            //IntPtr cProcess = FindWindow(null, "Test.txt - 记事本");

            //winHandle = FindWindowEx(cProcess, IntPtr.Zero, null, "");

 

            //IntPtr cProcess = FindWindow(null, "测试Hook");

            //winHandle = FindWindowEx(cProcess, IntPtr.Zero, null, null);

            ////winHandle = new IntPtr(0xE10F2);//这种方式是先通过Spy++找到控件的句柄,然后再使用这个句柄进行数据交互(此方法每次重启窗体,对应的句柄都会发生变化)

            //SendMessage(txtHandle, 0x0c, IntPtr.Zero, "ABCDEFGHIJKLMN");//0x0c wm_settext 给窗体设置文本     

            #endregion

        }

 源代码:https://files.cnblogs.com/files/ckym/HookTest.rar

上一篇:【项目实战-MATLAB】:基于EMD的心音信号特征提取


下一篇:三星S8+手机,刷机经验