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.

6
  • 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? Sep 30, 2014 at 9:48
  • 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?
    – BLoB
    Sep 30, 2014 at 9:51
  • 1
    @Left4Cookies Take a look at Rubens answer stackoverflow.com/questions/323140/…
    – BLoB
    Sep 30, 2014 at 9:54
  • @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. Sep 30, 2014 at 11:27
  • 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. Sep 30, 2014 at 11:29

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.