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/

    相关文章:

    c++ - 在当前进程中 Hook API 调用?

    linux - SvnNotify : svn log message gets bad chars when executed from hook but not from the command line

    c# - DLL 注入(inject)不断失败并出现不一致的错误

    c# - 使用 SetWindowsHookEx 设置的低级键盘 Hook 停止在 C# 中调用函数

    python - 如何从 Mercurial Input 或 Changeset Hook 访问提交消息

    c++ - Hook WM_SETTEXT 消息

    portable-executable - PE格式: Why the IAT can be empty,和MS绕道神话

    c - 使用C语言注入(inject)dll

    multithreading - 低级键盘 Hook 不在 UI 线程

    c++ - SetWindowsHookEx + WH_CBT 不起作用?或者至少不是我认为应该的方式?