views:

2063

answers:

2

I am trying to install a Windows service using InstallUtil.exe and am getting the error message

 System.BadImageFormatException - invalid format

What gives?

+1  A: 

Make sure the newest Framework (the one you compiled your app with) is first in the PATH. That solved the problem for me. Found here: http://www.issociate.de/board/post/280574/Installutil_System.BadImageFormatException.html

Epaga
+2  A: 

Some more detail for completeness in case it helps someone...

Note that the most common reason for this exception these days is attempting to load a 32 bit-specific (/platform:x86) DLL into a process that is 64 bit or vice versa (viz. load a 64 bit-specific (/platform:x64) DLL into a process that is 32 bit). If your platform is non-specific (/platform:AnyCpu), this won't arise (assuming no referenced dependencies are of the wrong bittedness).

In other words, running:

%windir%\Microsoft.NET\Framework\v2.0.50727\installutil.exe <My x64 DLL/EXE>

or:

%windir%\Microsoft.NET\Framework64\v2.0.50727\installutil.exe <My x86 DLL/EXE>

will not work (substitute in other framework versions: v1.1.4322 (32 bit only, so this issue doesn't arise) and v4.0.30319 as desired in the above).

Obviously, as covered in the other answer one will also need the .NET version number of the installutil you are running to be >= (preferably =) that of the EXE/DLL you are running the installer of.

Finally, note that in VS2010, the tooling will default to generating x86 binaries (rather than AnyCpu as previously)

Complete details of System.BadImageFormatException (saying the only cause is mismatched bittedness is really a gross oversimplification!)

Ruben Bartelink