I have developed a service on a 64 bit system using Visual Studio 2010. The service is just a skeleton that accesses the Start and Stop methods in a secondary library. The library accesses a 64 bit COM object, and must be built as x64. The COM object is a dll that was built separately as x64 and tested in a 64-bit environment. I have an installer that sets up the project and installs the service through a custom action.
I debug the code with a test app that bypasses the service, and so I can confirm that the library the service is accessing is working correctly.
The problem I am having is a BadImageFormatException when installing. I am using an installer with a target platform of x64. If I build everything as x64, I get the following message:
Error 1001. Exception occured while initializing the installation: System.BadImageFormatException. Could not load file or assembly... or one of its dependencies. An attempt was made to load a program with an incorrect format.
If I set the service to build as Any CPU, the installation works, and the service can access the library, but the COM object cannot be found. The installation will also work if I remove the custom action that installs the service. If I then try to install the service manually using installutil, I get the same error message as above.
Below is a list of the libraries I am using in the service.
using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using MAPIMail;
using MAPIMail.Logging;
Note that the MAPIMail project is being built as x64.
Does anyone have an idea about why I cannot install the service specifically as x64 when it installs fine as Any CPU on an x64 system. I appreciate any and all suggestions.
You need to use the 64bit version of installutil.exe to install for 64bit. The normal 32bit version cannot do this.
Install using InstallUtil.exe utility From the Start menu, select the Visual Studio <version> directory, then select Developer Command Prompt for VS <version>. The Developer Command Prompt for Visual Studio appears. Access the directory where your project's compiled executable file is located.
The InstallUtil binary may also be digitally signed by Microsoft and located in the . NET directories on a Windows system: C:\Windows\Microsoft.NET\Framework\v \InstallUtil.exe and C:\Windows\Microsoft.NET\Framework64\v \InstallUtil.exe .
I tried both Alex's and Jacob Seleznev's solutions, and they both worked. I found that the second answer, by Greg Sansom, from Jacob's link provided the best solution. To assist others, I have copied his answer below:
This can happen if your installer is installing 64-bit dlls. The following is copied from MSDN:
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.
share|edit|flag answered Jul 23 '11 at 3:09 Greg Sansom 6,5781335
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With