BadImageFormatException Occurs When Instantiating Types Defined in a Referenced Assembly at Run-time

Article ID: 967163 – Last Review: January 29, 2009 – Revision: 1.0

BadImageFormatException Occurs When Instantiating Types Defined in a Referenced Assembly at Run-time

Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action

You have developed a .NET application that references a class library assembly. The class library assembly has been compiled to specifically target a particular platform (x86 or x64).

Result

When your application attempts to instantiate a type in the referenced assembly, you receive the following error message.

BadImageFormatException was unhandled

Exception Details:

System.BadImageFormatException was unhandled

Message=Could not load file or assembly ‘<Assembly Name>, Version=<Assembly Version>, Culture=<culture>, PublicKeyToken=<PublicKeyToken>’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Cause

32-bit processes can only load and execute 32-bit modules in process. Likewise, 64-bit processes can only load and execute 64-bit modules in process.

If your application is compiled to target a specific platform (x86 or x64), but attempts to load and execute an assembly that targets a different platform, the BadImageFormatException will occur. This behavior will also occur if your application is compiled targeting the AnyCPU platform, and then is run an operating system whose platform differs than the assembly whose type you are trying to instantiate. For example, if the main application is compiled to AnyCPU, and references an assembly that targets x86, then if you run the application on an x64 operating system, the BadImageFormatException will occur.

This behavior is by design.

Resolution

Rebuild the referenced assembly using the AnyCPU setting. This would allow the class contained in it to be instantiated within a 32-bit process, or in a 64-bit process.

Advanced Compiler Settings Dialog Box (Visual Basic)
http://msdn.microsoft.com/en-us/library/07bysfz2.aspx (http://msdn.microsoft.com/en-us/library/07bysfz2.aspx)

/platform (Specify Output Platform) (C# Compiler Options)
http://msdn.microsoft.com/en-us/library/zekwfyz4.aspx (http://msdn.microsoft.com/en-us/library/zekwfyz4.aspx)

If you are referencing a third party component and cannot rebuild it yourself, contact the component vendor to see if a version of the assembly is available that has been compiled targeting AnyCPU. This will allow the Common Language Run-time (CLR) to Just-In-Time (JIT) compile to run as either a 32-bit or 64-bit module, depending on the machine architecture the operating system and host process are targeting.

More Information

While Visual Studio allows you to add references to assemblies that are compiled to target a different platform than the main application, you will receive compiler warnings to indicate possible problems. For example,

warning CS1607: Assembly generation — Referenced assembly ‘<referenced assembly.dll>’ targets a different processor

vbc : warning BC40010: Possible problem detected while building assembly ‘<project name.exe>’: Referenced assembly ‘<referenced assembly.dll>’ targets a different processor

For more information on the limitations of process interoperability, please see the following MSDN article.

Process Interoperability
http://msdn.microsoft.com/en-us/library/aa384231(VS.85).aspx (http://msdn.microsoft.com/en-us/library/aa384231(VS.85).aspx)

DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.


APPLIES TO
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Team Suite
  • Microsoft Visual Studio 2008 Professional Edition
  • Microsoft Visual Studio 2008 Standard Edition
  • Microsoft Visual Studio Team System 2008 Team Suite
Keywords: 
kbnomt kbrapidpub KB967163

 

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image