hook - Win32 将 DLL 注入(inject)到针对 "Any CPU"构建的应用程序中

标签 hook dll-injection setwindowshookex

我正在开发一个捕获所有用户交互的项目。 MSDN 告诉 (this)

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.



我的问题是,如果根据 Any CPU 构建应用程序会发生什么? .我需要调用SetWindowsHookEx来自针对 Any CPU 构建的 DLL .

我已经写了 HookLogger_32.exe 加载 HookFunctions_32.dll(均为 x86)和 HookLogger_64.exe 加载 HookFunctions_64.dll(均为 x64)设置 WH_CBTWH_MOUSE全局(不是特定线程)。

HookLogger_32.exe、HookLogger_64.exe、HookFunctions_32.dll 和 HookFunctions_64.dll 是用 C++ 编写的。

当我单击针对 Any CPU 构建的 .NET 应用程序时,这些 DLL 被注入(inject)(通过 SetWindowHookEx )。 Windows 操作系统挂起,我必须强制重启我的机器。

当针对 x86 或 x64 构建相同的 .NET 应用程序时,当我在 HookLoggers(32 位和 64 位)启动后单击该应用程序时,一切正常。

这种未定义行为的任​​何原因。

我工作的平台是 64 位机器。

最佳答案

您需要从具有相应位元的 DLL 注入(inject) - 即“任何 CPU”在运行时变为 32 位或 64 位......并且您的 DLL 必须与运行时位数匹配!

在您的情况下有用的东西被称为“并行程序集”(同一程序集的两个版本,一个 32 位和另一个 64 位)......我认为您会发现这些很有帮助:

  • Using Side-by-Side assemblies to load the x64 or x32 version of a DLL
  • http://blogs.msdn.com/b/gauravseth/archive/2006/03/07/545104.aspx
  • http://www.thescarms.com/dotnet/Assembly.aspx

  • 在这里您可以找到一个不错的演练,其中包含许多有用的信息 - 它描述了 .NET DLL wrapping C++/CLI DLL referencing a native DLL

    更新:

    要使 Hook 变得非常简单和强大,请参阅 this well-tested and free library - 除其他外,它适用于 AnyCPU!

    关于hook - Win32 将 DLL 注入(inject)到针对 "Any CPU"构建的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031379/

    相关文章:

    php - svn钩子(Hook)编码问题

    c - 在没有 LD_PRELOAD 的情况下 Hook mmap/munmap

    c++ - 最后 Hook 程序

    c++ - SetWindowshookEx 有时在 dll 注入(inject)后不起作用

    winapi - 解除全局 CBT Hook 后从所有进程中卸载 DLL

    c++ - 尝试 Hook 窗口的窗口过程。 SetWindowsHookEx 失败返回 NULL HHOOK 且 GetLastError 返回错误代码 126

    c++ - 在不使用 QT 的情况下运行 C++ 事件循环

    c++ - 在进程执行之前注入(inject) DLL

    c++ - 逆向工程得到函数原型(prototype)

    c++ - 确定消息发送到哪个窗口(SetWindowsHookEx & WH_KEYBOARD)