windows 如何拦截发送到窗口的消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/843711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I intercept messages being sent to a window?
提问by Jon Tackabury
I want to intercept messages that are being sent to a window in a different process. What is the best way to do this? I can't see the messages when I use the WH_GETMESSAGE hook, and I'm not sure if I can subclass across processes? Any help would be much appreciated.
我想拦截在不同进程中发送到窗口的消息。做这个的最好方式是什么?使用 WH_GETMESSAGE 挂钩时看不到消息,我不确定是否可以跨进程子类化?任何帮助将非常感激。
采纳答案by Shog9
You need to inject your own code into the process that owns the windows you wish to intercept messages from. Fortunately, SetWindowsHookEx()
makes this fairly easy, although you may have a bit of trouble at first if you've only used it for in-process hooking up to now.
您需要将自己的代码注入到拥有您希望从中拦截消息的窗口的进程中。幸运的是,SetWindowsHookEx()
这使这变得相当容易,尽管如果您到现在为止只将它用于进程内连接,您可能会在开始时遇到一些麻烦。
I can recommend two excellent articles on the subject:
我可以推荐两篇关于这个主题的优秀文章:
- Joseph Newcomber's Hooks and DLLs
- Robert Kuster's Three Ways to Inject Your Code into Another Process
- Joseph Newcomber 的Hooks 和 DLL
- 罗伯特·库斯特 (Robert Kuster)将代码注入另一个进程的三种方法
回答by Bob Moore
If the message is sent rather than posted WH_GETMESSAGE
won't see it. You need WH_CALLWNDPROC
. If you're working across processes you'll need a system-wide hook in a DLL. You don't mention how you invoked SetWindowsHookEx, so I don't know if your hooking failed because it wasn't global, or because the message you were looking for was sent.
如果消息是发送而不是发布,WH_GETMESSAGE
则不会看到它。你需要WH_CALLWNDPROC
. 如果您正在跨进程工作,您将需要一个 DLL 中的系统范围的钩子。您没有提到您如何调用 SetWindowsHookEx,所以我不知道您的挂钩失败是因为它不是全局的,还是因为您正在寻找的消息已发送。
If you haven't worked with system-wide hooks before, I have an example of a system-wide hook on my website here. That's a keyboard hook, but the principle is the same.
如果你还没有全系统挂钩工作之前,我有一个全系统钩子的例子在我的网站在这里。那是一个键盘钩子,但原理是一样的。
Stop me if I'm unintentionally talking down here - your question was so short I can't infer your expertise level. But messing around with hooks doesimply some experience...
如果我无意中在这里说话,请阻止我 - 你的问题太短了,我无法推断出你的专业水平。但是搞乱钩子确实意味着一些经验......