Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException x64 Issue

I swear the more Microsoft "evolves" Visual Studio, the more ignorant the entire process becomes.

I have a Windows Service with 3 class libraries in it. I went into the properties for each class library and set the platform target to x64. I did this same thing to the Windows Service as well as to the Test Console project I added to verify everything.

I can run the console application on my Windows 2008 R2 x64 server with no issues but the stupid installer keeps bombing out and telling me that I have a bad image. I did ensure that I had set the target platform of the installer to x64.

I have no idea idea was this stupid thing would be bombing out and would really appreciate any insights. I do reference other DLLs and those might be x86 but I thought if the main libraries were set properly that things would work...

like image 635
Ken Tola Avatar asked Apr 23 '12 05:04

Ken Tola


People also ask

How to fix system BadImageFormatException?

SOLUTION: If you are on IIS7, Right Click/ Advanced Settings on the Specific Application Pool of the website and select True on Enable 32-Bit Applications. It should work.

What is System BadImageFormatException?

A System. BadImageFormatException error often occurs when a 64-bit assembly is opened by a 32-bit application. In this case, Visual Studio is a 32-bit application and the Design view is attempting to load a 64-bit assembly. Visual Studio assemblies are located in the project references tree.

Why did I get badimageformatexception 6/8/2014?

Date Time: 6/8/2014 10:57:55 AM: System.InvalidOperationException: Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format.

What is badimageformatexception or fileloadexception in Visual Studio Code?

When you have one assembly trying to use another assembly, and they don’t have matching bitness (x64 or x86), then you’ll get an exception: either BadImageFormatException or FileLoadException. If your project references another assembly, and the bitness doesn’t match, you’ll get this exception:

Why do I get a badimageformatexception when using Gurobi on Windows?

The Gurobi .NET assembly (e.g. gurobiXX.NET.dll) is compiled for AnyCPU (64-bit preferred). Your application needs to ensure that the proper native library (gurobiXX.dll) is used at runtime for the target platform. A BadImageFormatException occurs if you mix 32/64 bit libraries at the moment when the .NET runtime loads the native library.


Video Answer


3 Answers

I have finally figured this out – it has NOTHING to do with architecture, references or any other nonsense and everything to do with the installer itself. As this article explains – the Visual Studio Installer, by default, uses a 32 bit DLL and that is what causes the failures.

To overcome this problem, simply follow these steps:

  1. Make sure that you go into the Properties ⇒ Build tab for every project and set the Target Platform to x64
  2. Click on the name of your Installation project and then Properties and ensure that the Target Platform is x64
  3. Build your solution – if the solution does not compile, right click and Unload Project and then Load Porject for those projects whose references fail.
  4. Go here and download and install the 7.0 INstaller SDK
  5. Go into the C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin folder and install Orca by double-clicking on the Orca.Msi file
  6. Run Orca and open your project's MSI folder
  7. Select the Binary table
  8. Double click the cell [Binary Data] for the record InstallUtil
  9. Make sure "Read binary from filename" is selected
  10. Click the Browse button Browse to C:\Windows\Microsoft.NET\Framework64\v4.0.30319
  11. Select InstallUtilLib.dll
  12. Click the Open button and then the OK button

That is it - save your MSI file in Orca and then deploy it – the x64 installation should work without any further issues.

like image 86
Ken Tola Avatar answered Oct 08 '22 23:10

Ken Tola


I just ran into this issue myself, in Visual Studio 2017, building an installer for an x64 version of an application that's been x86 for a long time.

I don't doubt that Ken's answer is definitive, but it occurred to me that as the Custom Actions are called by the installer, not by the installed application, there is no need, in my case at least, for the project containing the Custom Actions to have the same bitness as the rest of the application, as its classes are never instantiated by the application itself.

So I changed the platform for that project alone back to x86, and rebuilt the installer.

It all 'just worked'.

This depends, of course, on having Custom Actions that are completely isolated from the rest of the solution. Quite a relief not to have to use Orca however.

like image 44
ChrisA Avatar answered Oct 08 '22 21:10

ChrisA


This error happened to me installing a .Net 4.5+ service to a 64-bit machine.

  • The installer was set to x64
  • The service project build platform was set to x64

Installation fails with a BadImageFormatException.

For me, the solution was to go to the service project properties, and change the build platform to "Any CPU", and also uncheck the "Prefer 32bit" checkbox that was checked by default (see also what does it mean).

like image 23
ciencia Avatar answered Oct 08 '22 21:10

ciencia