DllImport - An attempt was made to load a program with an incorrect format

6

I want my C# application to conditionally run a native method. Deciding at run time whether to run either the x86 or the x64 version of the dll.

This question explains how to choose 32 bit or 64 bit at compile time, but that does not help. I want to make the decision at runtime.

I'm 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 the 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 load either the x86 or the x64 version of the dll?

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

Thanks

c#
dllimport
asked on Stack Overflow Jun 19, 2014 by Zain Rizvi • edited Dec 2, 2017 by DaveInCaz

1 Answer

10

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

Option 1: (Original answer)

Use "Any CPU", and then your program can run as 32-bit on a 32-bit platform and 64-bit on a 64-bit platform. In that case, you can use this code to determine which DLL to use and you would 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 to do this, then you would compile 2 different assemblies. You would compile a 32-bit assembly to be run on 32-bit platforms which uses the 32-bit DLL, and you would compile a separate 64-bit assembly to run in 64-bit platforms.

Option 3:

Use IPC(Inter-process-communications). You would have 1 64-bit program which "connects" to a 32-bit program. The 64-bit program can run the 64-bit DLL function, but when you need to run the 32-bit DLL function, you would have to send a message to the 32-bit program with the information needed to run it, and then the 32-bit program could send a response with any information that you want back.

answered on Stack Overflow Jun 19, 2014 by LVBen • edited Jun 21, 2014 by LVBen

User contributions licensed under CC BY-SA 3.0