如何拦截发送到窗口的消息?

时间:2023-01-17 23:52:41

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钩子时,我看不到消息,我不确定我是否可以跨进程子类化?任何帮助将非常感激。

2 个解决方案

#1


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:

我可以推荐两篇关于这个主题的优秀文章:

  1. Joseph Newcomber's Hooks and DLLs
  2. Joseph Newcomber的钩子和DLL

  3. Robert Kuster's Three Ways to Inject Your Code into Another Process
  4. 罗伯特·卡斯特将你的代码注入另一个过程的三种方法

#2


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 does imply some experience...

如果我无意中在这里说话,请阻止我 - 你的问题太短了我无法推断你的专业水平。但乱搞钩子确实意味着一些经验......

#1


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:

我可以推荐两篇关于这个主题的优秀文章:

  1. Joseph Newcomber's Hooks and DLLs
  2. Joseph Newcomber的钩子和DLL

  3. Robert Kuster's Three Ways to Inject Your Code into Another Process
  4. 罗伯特·卡斯特将你的代码注入另一个过程的三种方法

#2


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 does imply some experience...

如果我无意中在这里说话,请阻止我 - 你的问题太短了我无法推断你的专业水平。但乱搞钩子确实意味着一些经验......