DllImport. An attempt was made to download a program with the wrong format.

I want my C # application to conditionally run its own method. Having decided at run time whether to run either the x86 or x64 version of the dll.

This question explains how to select a 32-bit or 64-bit bit at compile time, but this does not help. I want to make a decision at runtime.

I am currently doing the following:

[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); } 

But when I try to call the x86 method, I get an error:

 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) 

Any idea how I can conditionally download either x86 or x64 version for dll?

(Note: dbghelpx86.dll is the x86 version of dbghelp.dll that I renamed)

thanks

+6
source share
1 answer

Your program will be either 32-bit or 64-bit. It is not possible to execute 32-bit code in a 64-bit program, and it is impossible to execute 64-bit code in a 32-bit program. If you try, you will get a runtime exception. Thus, you cannot have 1 program that executes both x86 and x64 code. You have 3 options, depending on what you would like to do.

Option 1: (original answer)

Use "Any processor" and then your program can run as 32-bit on a 32-bit platform and 64-bit on a 64-bit platform. In this case, you can use this code to determine which DLL to use, and you only need 1 assembly to be able to handle both 32-bit and 64-bit platforms, and it will use the correct DLL:

use Environment.Is64BitProcess

 if (Environment.Is64BitProcess) { //call MiniDumpWriteDump } else { //call MiniDumpWriteDumpX86 } 

Option 2:

If you want to use β€œpreprocessor” conditions for this, you must compile two different assemblies. You must compile a 32-bit assembly for 32-bit platforms that uses the 32-bit DLL, and you must compile a separate 64-bit assembly to work on 64-bit platforms.

Option 3:

Use IPC (Inter-process-communication). You will have 1 64-bit program that "connects" to the 32-bit program. A 64-bit program can run a 64-bit DLL function, but when you need to run a 32-bit DLL function, you have to send a message to the 32-bit program with the information necessary to run it, and then the 32-bit program can send an answer with any information you want to return.

+9
source

Source: https://habr.com/ru/post/971028/


All Articles