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.

Hossein
  • 24,202
  • 35
  • 119
  • 224
Left4Cookies
  • 567
  • 2
  • 7
  • 19
  • 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? – Andrey Korneyev Sep 30 '14 at 09: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? – Paul Zahra Sep 30 '14 at 09:51
  • 1
    @Left4Cookies Take a look at Rubens answer http://stackoverflow.com/questions/323140/system-badimageformatexception-could-not-load-file-or-assembly-from-installuti – Paul Zahra Sep 30 '14 at 09: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. – Left4Cookies Sep 30 '14 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](http://stackoverflow.com/a/11936113/17034). – Hans Passant Sep 30 '14 at 11:29
  • @PaulZahra I'm currently running locally in VS, but I can see in the error log that it also happens when running the exe. Both the 32 and 64 bit environments should be managed code. There are a few web services but they're in a totally different part of the program. I'm currently looking at Rubens answer. – Left4Cookies Sep 30 '14 at 11:32

1 Answers1

2

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