求大神分析下C#的hook

Veary 2014-04-10 10:47:21
麻烦帮忙分析下每一步都是做什么的,本人新手,不太懂,求详细

Set a mouse hook

To set a hook, call the SetWindowsHookEx function from the User32.dll file. This function installs an application-defined hook procedure in the hook chain that is associated with the hook.

To set a mouse hook and to monitor the mouse events, follow these steps:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
In the New Project dialog box, click Visual C# Projects under Project Types, and then click Windows Application under Templates. In the Name box, type ThreadSpecificMouseHook. By default, a form that is named Form1 is created.
Add the following line of code in the Form1.cs file after the other using statements.
using System.Runtime.InteropServices;
Add following code in the Form1 class.
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

//Declare the hook handle as an int.
static int hHook = 0;

//Declare the mouse hook constant.
//For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK.
public const int WH_MOUSE = 7;
private System.Windows.Forms.Button button1;

//Declare MouseHookProcedure as a HookProc type.
HookProc MouseHookProcedure;

//Declare the wrapper managed POINT class.
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;
}

//Declare the wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

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

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

//This is the Import for the CallNextHookEx function.
//Use this function to pass the hook information to the 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);
Add a Button control to the form, and then add the following code in the Button1_click procedure.
private void button1_Click(object sender, System.EventArgs e)
{
if(hHook == 0)
{
// Create an instance of HookProc.
MouseHookProcedure = new HookProc(Form1.MouseHookProc);

hHook = SetWindowsHookEx(WH_MOUSE,
MouseHookProcedure,
(IntPtr)0,
AppDomain.GetCurrentThreadId());
//If the SetWindowsHookEx function fails.
if(hHook == 0 )
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "UnHook Windows Hook";
}
else
{
bool ret = UnhookWindowsHookEx(hHook);
//If the UnhookWindowsHookEx function fails.
if(ret == false )
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "Set Windows Hook";
this.Text = "Mouse Hook";
}
}
Add the following code for the MouseHookProc function in the Form1 class.
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//Marshall the data from the callback.
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
//Create a string variable that shows the current mouse coordinates.
String strCaption = "x = " +
MyMouseHookStruct.pt.x.ToString("d") +
" y = " +
MyMouseHookStruct.pt.y.ToString("d");
//You must get the active form because it is a static function.
Form tempForm = Form.ActiveForm;

//Set the caption of the form.
tempForm.Text = strCaption;
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
}
Press F5 to run the project. Click the button on the form to set the hook. The mouse coordinates appear on the form caption bar when the pointer moves on the form. Click the button again to remove the hook.
...全文
385 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Veary 2014-04-11
  • 打赏
  • 举报
回复
引用 13 楼 shawn_yang 的回复:
[quote=引用 12 楼 u011785544 的回复:] [quote=引用 10 楼 jy251 的回复:] 1.C#的hook不能做全局钩子 2.你的代码太长,没仔细看,你的代码中明显就是用的pinovke调用的钩子函数 3.钩子技术,你可以去查查百度,这个api跟C#这个语言没什么太大的关系,你需要的是概念解释 4.钩子就是设置一个钩子,指定这个钩子的回调函数是什么,触发钩子回调之后,你处理这个钩子消息,如果要截断钩子消息,那么就不调用next的方法 5.如果你想用纯粹的C#加上pinvoke做按键精灵或者外挂之类的功能甚至根据句柄盗取密码之类的,你可以放弃了
好厉害的样子 不过我想做的主要供能 现在是在WPF中的文本框内部取词,然后弹出一个消息框来解释这个词语的意思 , 就像有道词典,或者金山词霸那种,但是不用那么广泛的去满屏幕取词,我只想去取在这个文本框内部的值,请问有什么好办法吗?[/quote] 如果是这样,没必要用钩子,因为文本框就是你写的,你完全可用事件实现[/quote] 不是的 窗口中会有很多很多的textbox 总不能每一个都设置一个鼠标悬停事件吧 要钩子挂到谁 谁才弹出
shawn_yang 2014-04-11
  • 打赏
  • 举报
回复
引用 12 楼 u011785544 的回复:
[quote=引用 10 楼 jy251 的回复:] 1.C#的hook不能做全局钩子 2.你的代码太长,没仔细看,你的代码中明显就是用的pinovke调用的钩子函数 3.钩子技术,你可以去查查百度,这个api跟C#这个语言没什么太大的关系,你需要的是概念解释 4.钩子就是设置一个钩子,指定这个钩子的回调函数是什么,触发钩子回调之后,你处理这个钩子消息,如果要截断钩子消息,那么就不调用next的方法 5.如果你想用纯粹的C#加上pinvoke做按键精灵或者外挂之类的功能甚至根据句柄盗取密码之类的,你可以放弃了
好厉害的样子 不过我想做的主要供能 现在是在WPF中的文本框内部取词,然后弹出一个消息框来解释这个词语的意思 , 就像有道词典,或者金山词霸那种,但是不用那么广泛的去满屏幕取词,我只想去取在这个文本框内部的值,请问有什么好办法吗?[/quote] 如果是这样,没必要用钩子,因为文本框就是你写的,你完全可用事件实现
Veary 2014-04-11
  • 打赏
  • 举报
回复
引用 10 楼 jy251 的回复:
1.C#的hook不能做全局钩子 2.你的代码太长,没仔细看,你的代码中明显就是用的pinovke调用的钩子函数 3.钩子技术,你可以去查查百度,这个api跟C#这个语言没什么太大的关系,你需要的是概念解释 4.钩子就是设置一个钩子,指定这个钩子的回调函数是什么,触发钩子回调之后,你处理这个钩子消息,如果要截断钩子消息,那么就不调用next的方法 5.如果你想用纯粹的C#加上pinvoke做按键精灵或者外挂之类的功能甚至根据句柄盗取密码之类的,你可以放弃了
好厉害的样子 不过我想做的主要供能 现在是在WPF中的文本框内部取词,然后弹出一个消息框来解释这个词语的意思 , 就像有道词典,或者金山词霸那种,但是不用那么广泛的去满屏幕取词,我只想去取在这个文本框内部的值,请问有什么好办法吗?
Veary 2014-04-11
  • 打赏
  • 举报
回复
引用 9 楼 caozhy 的回复:
不都是有注释么?英文看不懂回中学问你的英文老师。
我承认我英文白痴 但我主要对代码不懂
Veary 2014-04-10
  • 打赏
  • 举报
回复
引用 6 楼 rtdb 的回复:
这说明是我见过的最详细的了, 若还看不懂, 洗洗睡吧
运行会报空引用,而且 我真不懂 我只是想做个在某个文本框内捕捉词汇这样的东西
Veary 2014-04-10
  • 打赏
  • 举报
回复

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

//Declare the hook handle as an int.
static int hHook = 0;

//Declare the mouse hook constant.
//For other hook types, you can obtain these values from Winuser.h in the Microsoft SDK.
public const int WH_MOUSE = 7;
private System.Windows.Forms.Button button1;

//Declare MouseHookProcedure as a HookProc type.
HookProc MouseHookProcedure;	

//Declare the wrapper managed POINT class.
[StructLayout(LayoutKind.Sequential)]
public class POINT 
{
public int x;
public int y;
}

//Declare the wrapper managed MouseHookStruct class.
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct 
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}

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

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

//This is the Import for the CallNextHookEx function.
//Use this function to pass the hook information to the 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);
public static int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//Marshall the data from the callback.
MouseHookStruct MyMouseHookStruct = (MouseHookStruct) Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
//Create a string variable that shows the current mouse coordinates.
String strCaption = "x = " + 
MyMouseHookStruct.pt.x.ToString("d") + 
"  y = " + 
MyMouseHookStruct.pt.y.ToString("d");
//You must get the active form because it is a static function.
Form tempForm = Form.ActiveForm;
        
//Set the caption of the form.
tempForm.Text = strCaption;
return CallNextHookEx(hHook, nCode, wParam, lParam); 
}
}
rtdb 2014-04-10
  • 打赏
  • 举报
回复
这说明是我见过的最详细的了, 若还看不懂, 洗洗睡吧
Veary 2014-04-10
  • 打赏
  • 举报
回复
引用 3 楼 xdashewan 的回复:
代码太长而且没格式懒的看了,钩子主要作用是用来截获事件,比如鼠标的单击双击,键盘按键什么的。用法简单的说就是生成一个类型的钩子及响应事件,然后给钩子指定一个界面控件,然后当执行该类型操作就会触发响应事件,然后在事件里对该操作做相应处理,包括屏蔽次操作。
谢谢 一会改格式~
Veary 2014-04-10
  • 打赏
  • 举报
回复
有木有人~在线等~
xdashewan 2014-04-10
  • 打赏
  • 举报
回复
代码太长而且没格式懒的看了,钩子主要作用是用来截获事件,比如鼠标的单击双击,键盘按键什么的。用法简单的说就是生成一个类型的钩子及响应事件,然后给钩子指定一个界面控件,然后当执行该类型操作就会触发响应事件,然后在事件里对该操作做相应处理,包括屏蔽次操作。
Veary 2014-04-10
  • 打赏
  • 举报
回复
沙发,求大神帮忙,每一步都是干什么的,整体是干什么的,钩子用来干什么?我想在Application中做到文本框内取词,应该怎么做?
six-years 2014-04-10
  • 打赏
  • 举报
回复
看的头都大了,不懂帮顶
jy251 2014-04-10
  • 打赏
  • 举报
回复
1.C#的hook不能做全局钩子 2.你的代码太长,没仔细看,你的代码中明显就是用的pinovke调用的钩子函数 3.钩子技术,你可以去查查百度,这个api跟C#这个语言没什么太大的关系,你需要的是概念解释 4.钩子就是设置一个钩子,指定这个钩子的回调函数是什么,触发钩子回调之后,你处理这个钩子消息,如果要截断钩子消息,那么就不调用next的方法 5.如果你想用纯粹的C#加上pinvoke做按键精灵或者外挂之类的功能甚至根据句柄盗取密码之类的,你可以放弃了
threenewbee 2014-04-10
  • 打赏
  • 举报
回复
不都是有注释么?英文看不懂回中学问你的英文老师。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧