我希望我的C#应用程序有条件地运行本机方法,有条件地选择运行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位,它将选择从x86或x64版本的dbghelp.dll运行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是我重命名的dbghelp.dll的x86版本)
由于
答案 0 :(得分:3)
您无法将32位DLL加载到64位进程中。为了支持这一点,你必须有两个不同的EXE,一个编译为64位,一个编译为32位。
如果运行64位进程并遇到32位转储,则必须启动32位版本的EXE来处理转储文件。处理完成后,您可以使用某种IPC(进程间通信)机制将结果发送回64位进程。