我可以在主EXE文件中放入LowLevelMouseProc和LowLevelKeyboardProc吗? [英] Can I put LowLevelMouseProc and LowLevelKeyboardProc in the main EXE?

查看:187
本文介绍了我可以在主EXE文件中放入LowLevelMouseProc和LowLevelKeyboardProc吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全局Windows挂钩必须位于DLL中,因为该挂钩将在其他进程的上下文中被调用,因此必须将挂钩过程的代码注入该进程中.但是,存在限制:

Global Windows hooks must be in a DLL because the hook is going to be called in the context of a different process, so the hook procedure's code must be injected into that process. However, there are limitations:

SetWindowsHookEx可用于注入 一个DLL进入另一个进程. 32位 DLL无法注入64位 进程,并且不能将64位DLL 注入到32位进程中.如果 应用程序需要使用挂钩 在其他过程中,这是必需的 一个32位应用程序调用 SetWindowsHookEx注入32位 DLL转换为32位进程,并且 64位应用程序调用 SetWindowsHookEx注入64位 DLL转换为64位进程. 32位 和64位DLL必须具有不同的 名称.

SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.

由于这个原因,我宁愿使用低级钩子WH_MOUSE_LLWH_KEYBOARD_LL,而不是WH_MOUSEWH_KEYBOARD.从他们的 文档:

For this reason, I'd rather use the low-level hooks WH_MOUSE_LL and WH_KEYBOARD_LL, instead of WH_MOUSE and WH_KEYBOARD. As seen from their documentation:

此钩子在以下环境中被调用 安装它的线程.通话 通过发送消息到 安装挂钩的线程. 因此,安装的线程 挂钩必须具有消息循环.

This hook is called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.

这使我认为这些特定的挂钩过程不需要在单独的DLL中,而可以驻留在将它们挂钩的EXE中.但是,SetWindowsHookEx文档,说:

This leads me to think that these particular hook procedures do not need to be in a separate DLL, and can just live inside the EXE that hooked them up. The documentation for SetWindowsHookEx, however, says:

lpfn

[in]指向钩子过程的指针.如果dwThreadId参数 为零或指定的标识符 由其他人创建的线程 过程中,lpfn参数必须指向 到DLL中的挂钩过程.

[in] Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL.

没有提到两个低级钩子的显式异常.

No explicit exception for the two low-level hooks is mentioned.

我已经看到几个使用低级挂钩的.NET应用程序,而它们的挂钩过程没有放在单独的DLL中.这是另一个可以接受的暗示.但是,由于文档禁止这样做,我自己有点害怕.

I have seen several .NET applications that use the low-level hooks without having their hook procedures in a separate DLL. That is another hint that this is acceptable. However, I'm a bit scared to do this myself since the documentation forbids it.

如果我不使用DLL,而只是将这些低级钩子程序直接放入我的EXE中,有人能预见到任何麻烦吗?

Does anyone foresee any trouble if I don't use a DLL and just put these low-level hook procedures straight into my EXE?

编辑:对于悬赏,我想明确地说是的,这是可以的,因为..."或否,这可能是错误的,因为...".

Edit: For the bounty, I would like a definitive "yes, this is ok, because..." or "no, this can go wrong, because...".

推荐答案

dll规则中的全局挂钩函数有一个例外.低级鼠标和键盘挂钩是在调用进程的上下文中执行的,而不是在挂钩的进程中执行(内部,Windows通过Windows消息通知您的挂钩).因此,挂钩代码不会在任意进程中执行,而可以用.Net编写.请参见 http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx例如.

There is one exception to the global hooking function in dll rule. Low level mouse and keyboard hooks are executed in the context of the calling process, not the process being hooked (internally, Windows notifies your hook via a windows message). Therefore the hook code is not executed in an arbitrary process and can be written in .Net. See http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx for an example.

对于其他钩子,您确实需要调用32位版本的SetWindowsHookEx并在32位进程中传递钩子函数,并调用64位版本的SetWindowsHookEx并在64位进程中传递钩子函数.

For other hooks you do need to call the 32 bit version of SetWindowsHookEx and pass a hook function in a 32bit process and call the 64bit version of SetWindowsHookEx and pass a hook function in a 64bit process, though.

这篇关于我可以在主EXE文件中放入LowLevelMouseProc和LowLevelKeyboardProc吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆