Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.BadImageFormatException When Installing Program From VS2010 Installer Project

I am getting this error when trying to install a Windows Service from a VS2010 .NET 4 Installer project:

"Exception occurred while initializing the installation: System.BadImageFormatException. Could not load the file [file name].exe or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."

I can't figure out what is causing this. All the projects in my solution are compiling against .NET Framework 4 and the installer solution dependencies require .NET 4. I have cleaned/rebuilt the solutions and projects to no avail. Is there something obvious I am missing?

like image 757
Matt Avatar asked Dec 03 '22 09:12

Matt


2 Answers

This can happen if your installer is installing 64-bit dlls.

If you add a 64-bit managed custom action to a Setup project, the Visual Studio build process embeds a 32-bit version of InstallUtilLib.dll into the MSI as InstallUtil. In turn, the 32-bit .NET Framework is loaded to run the 64-bit managed custom action and causes a BadImageFormatException exception.

For the workaround, replace the 32-bit InstallUtilLib.dll with the 64-bit version.

  1. Open the resulting .msi in Orca from the Windows Installer SDK.
  2. Select the Binary table.
  3. Double click the cell [Binary Data] for the record InstallUtil.
  4. Make sure "Read binary from filename" is selected and click the Browse button.
  5. Browse to %WINDIR%\Microsoft.NET\Framework64\v2.0.50727.
  6. The Framework64 directory is only installed on 64-bit platforms and corresponds to the 64-bit processor type.
  7. Select InstallUtilLib.dll.
  8. Click the Open button.
  9. Click the OK button.
like image 89
Greg Sansom Avatar answered Dec 06 '22 10:12

Greg Sansom


You likely have the wrong installer prerequisites. Go to your setup project's properties window, click Prerequisites... under build, and ensure that .NET Framework 4 is checked. You likely still have .NET Framework 3.5 SP1 still checked. You probably need to use Windows Installer 4.1 as well (in the same dialog).

Also check to make sure that in launch conditions your .NET Framework version points to 4.

like image 31
Matt Eland Avatar answered Dec 06 '22 09:12

Matt Eland