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