c# - 在 64 位进程中加载​​ 32 位 dll

标签 c# windows dll .net-4.5 32bit-64bit

<分区>

我希望我的 C# 应用程序有条件地运行 native 方法,有条件地选择运行 dll 的 x86 或 x64 版本。每当我尝试加载 32 位 dll 时,我都会收到以下错误:

Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at <exeName>.MiniDumpMethods.MiniDumpWriteDumpX86(IntPtr hProcess, UInt32 processId, SafeHandle hFile, MINIDUMP_TYPE dumpType, IntPtr expParam, IntPtr userStreamParam, IntPtr callbackParam)

背景上下文:我希望我的二进制文件获取给定进程的内存转储。根据进行内存转储的进程是 32 位还是 64 位,它将选择从 dbghelp.dll 的 x86 或 x64 版本运行 MiniDumpwriteDump 方法。

我目前正在做以下事情:

[SuppressUnmanagedCodeSecurity]
internal static class MiniDumpMethods
{
    [DllImport("dbghelp.dll",
        EntryPoint = "MiniDumpWriteDump",
        CallingConvention = CallingConvention.StdCall,
        CharSet = CharSet.Unicode,
        ExactSpelling = true,
        SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MiniDumpWriteDump(
        IntPtr hProcess,
        uint processId,
        SafeHandle hFile,
        MINIDUMP_TYPE dumpType,
        IntPtr expParam,
        IntPtr userStreamParam,
        IntPtr callbackParam);

[DllImport("dbghelpx86.dll",
EntryPoint = "MiniDumpWriteDump",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Unicode,
ExactSpelling = true,
SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MiniDumpWriteDumpX86(
        IntPtr hProcess,
        uint processId,
        SafeHandle hFile,
        MINIDUMP_TYPE dumpType,
        IntPtr expParam,
        IntPtr userStreamParam,
        IntPtr callbackParam);
}

知道如何有条件地加载 dll 的 x86 或 x64 版本吗?

(注:dbghelpx86.dll是我重命名的x86版本的dbghelp.dll)

谢谢

最佳答案

您不能将 32 位 DLL 加载到 64 位进程中。要支持这一点,您必须有两个不同的 EXE,一个编译为 64 位,一个编译为 32 位。

如果您运行 64 位进程并遇到 32 位转储,则必须启动 32 位版本的 EXE 来处理转储文件。处理完成后,您可以使用某种 IPC(进程间通信)机制将结果发送回 64 位进程。

关于c# - 在 64 位进程中加载​​ 32 位 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24314325/

相关文章:

c# - 有没有办法在 Windows 10 设备上以编程方式启用/禁用(灰色)操作中心中的旋转锁定,而无需重新启动?

c++ - CRT 静态链接的 COM InProc 服务器的资源(例如 FLS 索引)耗尽

c++ - 在 VS 中编译 C++,运行时不需要 MSVCP120D.dll

c# - 阻止用户调整 ListView 中的列宽?

windows - 为 Windows 7 创建上下文菜单项,例如扫描方式

c# - 调试动态 Javascript

java - 服务如何检测 Windows 何时完全启动?

使用抽象接口(interface)的 C++ Dll 边界 -> header 中的智能指针?调用删除?

c# - 将两列的值合并到第三列

C# .NET MySql 只返回最后的结果