0

When I transfer my source on to a Win7 64-bit machine I get a BadImageFormatException when I try to compile and run the solution targetting AnyCPU.

This does not happen on XP 32-bit.

What is the problem?

CJ7
  • 22,579
  • 65
  • 193
  • 321
  • This is a runtime error, not a compile time error. An obvious solution is to target x86 instead if you have no idea what 32-bit component you might be using. – Hans Passant Nov 09 '12 at 03:31
  • 1
    But when I target `AnyCPU` on `XP 32-bit` it is fine. If I target `x86` will the compiled app run on `x64`? – CJ7 Nov 09 '12 at 05:13
  • Do you have any PInvoke/unsafe/COM etc. code in your assembly? – Pawel Nov 11 '12 at 23:12
  • @Pawel: there are COM and external DLL calls in the code. – CJ7 Nov 14 '12 at 00:25
  • It seems to me that your app is trying to run in 64-bit mode but one of the native dlls you are using (COM or dlls) does not have 64-bit version (or may be one of assemblies you use are set to x86? - I am not sure if this would trigger the BadImageFormatException). Then the dll is being loaded to 64-bit process and you have the crash. Note that 32-bit native code works on 64-bit machines thanks to the built-in x86 emulator. So if you target AnyCPU your app will work on both x86 and x64. – Pawel Nov 14 '12 at 00:56

1 Answers1

1

It can typically occur when you changed the target framework of .csproj and reverted it back to what you started with.

Make sure 1 if supportedRuntime version="a different runtime from cs project target" under startup tag in app.config.

Make sure 2 That also means checking other autogenerated or other files in may be properties folder to see if there is no more runtime mismatch between these files and one that is defined in .csproj file.

These might just save you lot of time before you start trying different things with project properties to overcome the error.

purvin
  • 144
  • 1
  • 3