Superdoo
Superdoo

Reputation: 33

System.BadImageFormatException using .NET 2.0

I'm fairly new at C#, so bear with me if I misunderstand something.

I have an fairly old application at my job that allows you to import dll's to do certain automated tasks. All the old programmers left years ago and I have no support to contact for this application. I created a dll in visual studio 2013 using .NET 2.0 framework and I get this error message when trying to import it:

The format of the file 'MyDll.dll' is invalid.  Type:System.BadImageFormatException
Source: mscorlib Stack Trace: at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Boolean isStringized, Evidence assemblySecurity, Boolean throwOnFileNotFound, Assembly locationHint, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Boolean stringized, Evidence assemblySecurity, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm) 
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Applications.frmApplications.loadFields()
   at Applications.frmApplications.fillMenuItem()
   at Applications.frmApplications.ugApplications_AfterSelectChange(Object sender, AfterSelectChangeEventArgs e) 
   at Infragistics.Win.UltraWinGrid.UltraGrid.OnAfterSelectChange(AfterSelectChangeEventArgs e)
   at Infragistics.Win.UltraWinGrid.UltraGrid.FireEvent(GridEventIds id, EventArgs e)
   at Infragistics.Win.UltraWinGrid.UltraGrid.SelectNewSelection(Type type, Selected selected)
 at Infragistics.Win.UltraWinGrid.UltraGrid.InternalSelectItem(ISelectableItem     item, Boolean clearExistingSelection, Boolean select)
   at Infragistics.Win.UltraWinGrid.UltraGridRow.set_Selected(Boolean value)
   at Applications.frmApplications.searchForApplication()

I looked up this error online and the basic consensious is that I am using the wrong .NET framework version. I have used .NET Reflector to see what version an older dll that works was using, and it comes up as version 1.1.12.30886 . Now my question is is there another way round this error, or can only dll .NET versions 1.1 be imported? And if so is there any way to change my dll's to the 1.1 framework, as I see its past its life cycle and is no longer supported and I don't see a way to install it in visual studio.

Upvotes: 2

Views: 732

Answers (1)

Luaan
Luaan

Reputation: 63722

If you have the sources for the application, you can just recompile it for the latest .NET framework. The .NET and C# teams work very hard to make the versions compatible, so you probably won't have a single error.

Sticking with the 1.1 (or 2.0) framework is probably a bad idea - it's very restrictive compared to modern versions, and as you've noted, it's unsupported. If you really want to, you'd have to download an old version of Visual Studio / SharpDevelop / MonoDevelop / whatever. 1.1 was still in a time when each .NET framework had its own Visual Studio version - you'd need Visual Studio 2003.NET to work with 1.1.

That said, I didn't really have trouble with interop between different .NET framework versions. However, you need to make sure both versions of the framework are installed - newer versions of the framework do not contain the old versions (so 2.0 doesn't include 1.1, and 4.0 doesn't include 2.0 or 3.5). Sometimes you can fix this with an application manifest (assembly rebinding), but it can get tricky.

Another common source of BadImageFormatException is the bit-ness. By default, .NET uses AnyCPU nowadays, which means it will use 64-bit if available, and 32-bit otherwise. It's possible that one of the projects uses 32-bit while the other uses 64-bit, for example.

Upvotes: 2

Related Questions