An attempt was made to load a program with an incorrect format (.dll)

2

My program compiles nicely, but when I run the program I get this error: "System.BadImageFormatException thrown calling IO_Init(). Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"

As far as I understand, this error is because my program tries to use a dll which is not the same bitness as my program (which is compiled in 64-bit). I have a 32-bit version of the dll as well, and when I run the 32-bit version of the program, it doesn't throw any error, which is fine.

At the start of my program, I run this to check if we're in 64-bit and if so, go to a folder containing the 64-bit file:

if ((IntPtr.Size > 4) && ((ad.RelativeSearchPath == null) || !ad.RelativeSearchPath.Contains(ad.BaseDirectory + "64\\")))
        {
            AppDomainSetup aset = new AppDomainSetup();
            aset.PrivateBinPath = ad.BaseDirectory + "64\\;" + ad.BaseDirectory;
            aset.PrivateBinPathProbe = ad.BaseDirectory + "64\\;" + ad.BaseDirectory;
            ad = AppDomain.CreateDomain("_NM64SWITCH_", Assembly.GetAssembly(typeof(NaviModel.Program)).Evidence, aset);
            ad.ExecuteAssemblyByName(Assembly.GetAssembly(typeof(NaviModel.Program)).FullName, par);
            return;
        }

I'm a little lost as to where to look now.

c#
visual-studio
dll
asked on Stack Overflow Sep 30, 2014 by Left4Cookies

1 Answer

0

Switching from ANY CPU To x86 fixed this problem for me.

answered on Stack Overflow Apr 16, 2018 by Eoin Kavanagh

User contributions licensed under CC BY-SA 3.0