System.BadImageFormatException: How to fix .NET version mismatch?
Asked Answered
M

3

9

I wrote a dll in VS 2005 that will be loaded by another program that was developed in VS 2003. When that program tries to load my dll, it throws a System.BadImageFormatException: The format of the file 'Foo.dll' is invalid.

I searched SO and Google, and it seems that the different version of .NET is the culprit. My question is: How do I fix this?

(I cannot modify the program developed in VS 2003.)

Mesocratic answered 5/5, 2009 at 17:29 Comment(2)
This link was also helpful: devlicio.us/blogs/ziemowit_skowronski/archive/2008/08/22/…Mesocratic
A native (unmanaged) library with a different word size can also throw this exception. You will need to get a version for the correct word size or build the hosting application to match.Deniable
E
8

Compile the VS2005 Project with the same .Net Version as your VS2003 Project uses. Or, depending of the size of your VS2003 Project, reflector and rewrite it. That's common practice for projects over here, where for some obscure reason there is no more VSS version available.

Euripus answered 5/5, 2009 at 17:54 Comment(4)
"Compile the VS2005 Project with the same .Net Version as your VS2003 Project uses." Can you please explain how to do that? I don't see how to choose the .NET version of my project.Mesocratic
Rightclick your Project in VS, on the very first tab you can select the targetframework.Euripus
Thank you, Markus. Although that didn't work in VS 2005, it did work in the 2008 version. I appreciate your help.Mesocratic
As an addition, in my case, I also had to change the project build Platform target to x86 (Project Properties, Build tab) when using x86 libraries.Janeyjangle
F
17

I got this error when I was running the 64bit version of the CLR and trying to load an assembly that was marked 32bit only. The specific assembly in my case was the Oracle.DataAccess.dll that comes as part of ODP.NET.

Fionafionna answered 5/5, 2009 at 17:31 Comment(3)
I am getting the same error too (with a different assembly), do you have any solution for that?Paquette
You need to just make sure you run the 32bit CLR when loading 32bit only assembliesFionafionna
This was the answer to my problem. I had not faced this in quite a while, but when one is using COM interop it's fairly common to run into this scenario when using the 'Any CPU' build config and then deploying to a 64-bit server. The x86 configs are handy in those cases.Autocratic
N
14

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.

Nietzsche answered 3/8, 2010 at 18:53 Comment(0)
E
8

Compile the VS2005 Project with the same .Net Version as your VS2003 Project uses. Or, depending of the size of your VS2003 Project, reflector and rewrite it. That's common practice for projects over here, where for some obscure reason there is no more VSS version available.

Euripus answered 5/5, 2009 at 17:54 Comment(4)
"Compile the VS2005 Project with the same .Net Version as your VS2003 Project uses." Can you please explain how to do that? I don't see how to choose the .NET version of my project.Mesocratic
Rightclick your Project in VS, on the very first tab you can select the targetframework.Euripus
Thank you, Markus. Although that didn't work in VS 2005, it did work in the 2008 version. I appreciate your help.Mesocratic
As an addition, in my case, I also had to change the project build Platform target to x86 (Project Properties, Build tab) when using x86 libraries.Janeyjangle

© 2022 - 2024 — McMap. All rights reserved.