再问:关于Hook的问题

sarmoo 2003-01-08 10:09:55
设置钩子:
HHOOK SetWindowsHookEx(
int idHook, // hook type
HOOKPROC lpfn, // hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // thread identifier
);
SetWindowsHookEx的第二个参数,所声明的代理必须是这种形式吗:
public delegate int HOOKPROC(int nCode, int wParam, int lParam);
如果是的话,int nCode, int wParam, int lParam,应该怎样理解和使用?
...全文
65 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qaqaqa 2003-01-09
  • 打赏
  • 举报
回复
我们好像问的是同一个问题。。我的oicq:3515831

请速与我联系。。。。为盼
qaqaqa 2003-01-09
  • 打赏
  • 举报
回复
刚刚看到你的贴子。。我支持一下。。上去吧你
sarmoo 2003-01-09
  • 打赏
  • 举报
回复
谁还有话说???
我要结贴了………………
sarmoo 2003-01-09
  • 打赏
  • 举报
回复
qaqaqa兄若到此请留步,感谢从你那里也学到不少东西!
xaep 2003-01-08
  • 打赏
  • 举报
回复
Yes it must be this format.
The parameters have different content by different hook type. For example if hook type is WH_KEYBOARD_LL, lParam is a KBDLLHOOKSTRUCT pointer, you could use Marshal.PtrToStructure to get structure from pointer.

Hope it could be help.
yarshray 2003-01-08
  • 打赏
  • 举报
回复
我是说一般情况

你把代理改成

public delegate int HOOKPROC(IntPtr hWnd, int msg, int wParam, int lParam);

一般情况下第一个参数传递的是一个窗口句柄,第二个是消息定义

后面两个参数根据你具体消息来决定,我上面给的例子就是捕获到

鼠标的一些消息的处理,实际也就是在输出调试信息.
sarmoo 2003-01-08
  • 打赏
  • 举报
回复
to yarshray(saga jion):

不好意思,我怎么越看越不懂了? :p
这个HookProc跟上面的代理是什么关系?& Who defines the constants for Windows messages?
谢谢,还请继续回答!:)


yarshray 2003-01-08
  • 打赏
  • 举报
回复
一般说来是这样的
[C#]

// Defines the constants for Windows messages.

const int WM_SETFOCUS = 0x0007;
const int WM_INITDIALOG = 0x0110;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_RBUTTONDOWN = 0x0204;
const int WM_MOVE = 0x0003;

// Overrides the base class hook procedure...

protected Overrides IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
{

// Evaluates the message parameter to determine the user action.

switch(msg)
{

case WM_INITDIALOG:
System.Diagnostics.Trace.Write("The WM_INITDIALOG message was received.");
break;
case WM_SETFOCUS:
System.Diagnostics.Trace.Write("The WM_SETFOCUS message was received.");
break;
case WM_LBUTTONDOWN:
System.Diagnostics.Trace.Write("The WM_LBUTTONDOWN message was received.");
break;
case WM_RBUTTONDOWN:
System.Diagnostics.Trace.Write("The WM_RBUTTONDOWN message was received.");
break;
case WM_MOVE:
System.Diagnostics.Trace.Write("The WM_MOVE message was received.");
break;

}

// Always call the base class hook procedure.

return base.HookProc(hWnd, msg, wParam, lParam);

}
TheAres 2003-01-08
  • 打赏
  • 举报
回复
Look on this article, there is some demo code.
http://www.csdn.net/develop/read_article.asp?id=15534
http://www.csdn.net/develop/read_article.asp?id=15535

>>文档中心?怎么走?:)
www.csdn.net/develop
sarmoo 2003-01-08
  • 打赏
  • 举报
回复
文档中心?怎么走?:)
是在CSDN上吗?
yarshray 2003-01-08
  • 打赏
  • 举报
回复
要不,你去文档中心找找,我记得以前有人贴过一个

只几行代码,不过不是全局的.应该是去年10月份的吧

我记不起来了.你自己找找.
sarmoo 2003-01-08
  • 打赏
  • 举报
回复
我晕!!!

yarshray兄,不是我不去读,实在是太“深奥”了,直接调试也不通。
能否浅显些,毕竟我也是尚在初学行列中呀!
sarmoo 2003-01-08
  • 打赏
  • 举报
回复
Thanks!
I'll try it.
yarshray 2003-01-08
  • 打赏
  • 举报
回复
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
using ShellExt;

[assembly: AssemblyKeyFile(@"..\..\KeyFile.snk")]

namespace ShellExecuteHookTest
{
[StructLayout(LayoutKind.Sequential)]
public class SHELLEXECUTEINFO {
public int cbSize;
public int fMask;
public int hwnd;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpVerb;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpFile;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpParameters;
[MarshalAs(UnmanagedType.LPWStr)]
public string lpDirectory;
public int nShow;
public int hInstApp;
public int lpIDList;
public string lpClass;
public int hkeyClass;
public int dwHotKey;
public int hIcon;
public int hProcess;
}

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214FB-0000-0000-C000-000000000046")]
public interface IShellExecuteHook{
[PreserveSig()]
int Execute(SHELLEXECUTEINFO sei);
}

[Guid("6156C6FC-4DD9-4f82-8200-0446DABB7F35"), ComVisible(true)]
public class DateParser : IShellExecuteHook {

private static string clsid="{6156C6FC-4DD9-4f82-8200-0446DABB7F35}";
private int S_OK=0;
private int S_FALSE=1;

public static void Main(string[] args) {

Assembly asm=Assembly.GetExecutingAssembly();
RegistrationServices reg=new RegistrationServices();

if(args.Length==1 && args[0].Equals("/u")) {
// Unregister with COM
reg.UnregisterAssembly(asm);

// Remove from GAC
if (FusionInstall.RemoveAssemblyFromCache("DateParser") == 0) {
Console.WriteLine("DateParser - shell extension successfully unregistered");
}
} else {
// Register with COM
reg.RegisterAssembly(asm, 0);

// Add to GAC
if(FusionInstall.AddAssemblyToCache("DateParser.exe") == 0) {
Console.WriteLine("DateParser - shell extension successfully registered");
}
}
}

public DateParser(){}

public int Execute(SHELLEXECUTEINFO sei) {
try {
DateTime oTime=DateTime.Parse(sei.lpFile + " " + sei.lpParameters);

MessageBox.Show(null, "Date '" + sei.lpFile + " " + sei.lpParameters + "' in ISO 8601 is " + oTime.ToString("s"), "ISO 8601 Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
return S_OK;
}
catch(FormatException) {
return S_FALSE;
} catch(Exception e) {
// Unknown exception. Report it to stderr
Console.Error.WriteLine("Unknown exception parsing Date: " + e.ToString());
}

return S_FALSE;
}
[System.Runtime.InteropServices.ComRegisterFunctionAttribute()]
static void RegisterServer(String zRegKey) {
try {
RegistryKey root;
RegistryKey rk;

root = Registry.LocalMachine;
rk = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks", true);
rk.SetValue(clsid, ".Net ISO 8601 Date Parser Shell Extension");
rk.Close();
}
catch(Exception e) {
System.Console.Error.WriteLine(e.ToString());
}
}

[System.Runtime.InteropServices.ComUnregisterFunctionAttribute()]
static void UnregisterServer(String zRegKey) {
try {
RegistryKey root;
RegistryKey rk;

root = Registry.LocalMachine;
rk = root.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ShellExecuteHooks", true);
rk.DeleteValue(clsid);
rk.Close();

}
catch(Exception e) {
System.Console.Error.WriteLine(e.ToString());
}
}
}
}


关于更多参考:

http://expert.csdn.net/Expert/topic/1321/1321578.xml?temp=.2826654
sarmoo 2003-01-08
  • 打赏
  • 举报
回复
具体一点说,我的目的是截获窗口的键盘消息,就是让我的控件首先响应键盘消息。
键盘的响应函数是这种形式:
private void MyControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e){......}
我该怎样把它跟钩子挂上呢?

110,571

社区成员

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

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

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