2
votes

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.

1
IntPtr.Size > 4 doesn't seems like a good way of checking iа we are running x64 environment. Instead consider using System.Environment.Is64BitOperatingSystem property. And do you really need to execute your assembly? Maybe Assembly.Load (and then creating objects from that assembly you're need) is what you're searchnig for?Andy Korneyev
Does this error happen when you run locally in visual studio or when you run it on a server? Also, are both the 32 and 64 bit managed code? Does your program contain web services?Paul Zahra
@Left4Cookies Take a look at Rubens answer stackoverflow.com/questions/323140/…Paul Zahra
@AndyKorneyev Is64BitOperatingSystem sure is a prettier way of identifying the environment. I don't need to execute the assembly to run the program, but to be able to use a certain plugin for it, the assembly has to run.Left4Cookies
What you are doing can only affect the loading of .NET assemblies. Which do not have a bitness dependency, unless you are using C++/CLI. This DLL probably contains native code. It is simply a matter of copying the right flavor of the DLL to the target machine of course. If you want to do it dynamically anyway then look at this post.Hans Passant

1 Answers

2
votes

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