使用非托管 DLL 来自 .NET 的系统范围 Shell 挂钩
我过去曾成功地使用过以下 CodeProject 文章中提供的代码,但它似乎只能在 Vista/7 上部分工作(我猜是因为 UAC)。它适用于当前线程,但不会获取系统范围的活动。我尝试在清单中添加 requireAdministrator 并对非托管和托管程序集进行签名,但这似乎没有帮助。
我的 main目标是使用 SetWindowsHookEx 捕获非托管程序集中的 HSHELL_WINDOWCREATED 和 HSHELL_WINDOWDESTROYED 消息,并通知 C# 应用程序,以便它可以实时使用这些信息。它基本上只需要知道系统中何时创建或销毁窗口。
我还发现了一个纯 C/C++ 示例应用程序,在 Vista/7 中运行得很好,但我真的不想用 C/C++ 编写整个应用程序,因为我更喜欢 C#。
任何人都知道如何修复第一篇 CodeProject 文章中的代码,以便它也可以在 Vista/7 上运行吗?或者有任何其他示例说明我无需在 C# 中创建计时器和类似 hack 即可实现此目的吗?
谢谢, 马克
I have used the code supplied in the following CodeProject article in the past with success, but it only seems to partially work on Vista/7 (I'm guessing because of UAC). It works for the current thread, but it doesn't pick up system wide activity. I've tried adding requireAdministrator in the manifests and signing both the unmanaged and managed assemblies, and that doesn't appear to help.
Using Window Messages to Implement Global System Hooks in C#
My main goal is to use SetWindowsHookEx to catch the HSHELL_WINDOWCREATED and HSHELL_WINDOWDESTROYED messages in the unmanaged assembly, and notify the C# application so that it can use the information in real time. It basically just needs to know whenever a window is created or destroyed in the system.
I also found an example application that is pure C/C++ that works just fine in Vista/7, but I really don't want to have to write the entire application in C/C++ as I prefer C#.
System Wide Hook Example that Works
Anyone know how to fix the code in the first CodeProject article so that it works on Vista/7 as well? Or have any other examples of ways I can do this without creating timers and similar hacks in C#?
Thanks,
Marc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看我的程序:http://wpfklip.codeplex.com。
我已经实现了系统范围的 shell 挂钩,因此程序能够捕获窗口的创建、激活等。它是用 c# 编写的
have a look at my program: http://wpfklip.codeplex.com.
I've implemented system-wide shell hook so the program is able to catch window created, activated and so on. And it's written in c#
不过这里有一个关键点:
1)Lusid 正在寻找一个 shell hook。这些在 .NET 中不起作用,因为 Lusid 链接到的第一篇文章中解释了“DLL 注入”原因。
2)Trickster 写了一个键盘钩子。这些在 C# 中工作得很好(因为它们是从 Windows 的某些部分而不是特定的应用程序挂钩的,并且它可以应对注入 .NET)。
一个
There's a key point here though:
1) Lusid is looking for a shell hook. These don't work in .NET, for the "DLL injection" reasons explained in the first article that Lusid linked to.
2) Trickster has written a keyboard hook. These work fine in C# (because they are hooked from some part of windows, rather than a specific application, and it can cope with injecting .NET).
a