Click here to Skip to main content
15,889,462 members
Articles / All Topics
Technical Blog

Visual Studio 2010: BadImageFormatException was Unhandled

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
9 Nov 2013CPOL2 min read 8.4K   1   1
BadImageFormatException was unhandled in Visual Studio 2010

Introduction

Recently, I was cleaning up some code by refactoring a bunch of similar code into a common utility class library project and when I went to debug my first attempt, I got the following error:

Error

This was frustrating because all I did was take a static method in its entirety from one project to another, something I’ve done a thousand times before. On a whim, I tried running the exact same code in Release mode and it ran just fine.  So what I was trying to do was legit, I had an odd configuration mode issue.

After a little research, I found the culprit. Viewing the properties on the new class library project and also the console application, I found that someone on my team had changed the platform target to be x86 instead of “Any CPU”.  Some of the references that I added for the class library were built for other platforms.  After changing the platform target to “Any CPU”, it worked just fine.

Location

There were several only resources that I found saying to change the platform target and be done with it, in this case it worked for me however I wanted to point out one thing.  As a convention, I typically set the Platform for all of my projects to Any CPU so it is as portable as possible however there are cases where you cannot simply change the platform.  As an example:

Recap

This is the Configuration Manager found when you right click on the Solution itself.  You will notice that three of the projects in the solution have a Platform defined as x86 and the active solution platform for the Debug configuration is set to “Mixed Platforms”.  In this case, these three projects cannot run on x64 because of the project requirements.

So keep in mind that even though you may read elsewhere that Any CPU is always the answer, you need to look at your environment and the project requirements to make sure it does work.  So even though I normally use Any CPU as well, it is not always the right answer for every project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRoman Badillo Pin
Member 109456014-Nov-14 8:04
Member 109456014-Nov-14 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.