"An attempt was made to load a program with an incorrect format" even when the platforms are the same
Solution 1
Somehow, the Build checkbox in the Configuration Manager had been unchecked for my executable, so it was still running with the old Any CPU build. After I fixed that, Visual Studio complained that it couldn't debug the assembly, but that was fixed with a restart.
Solution 2
If you try to run 32-bit applications on IIS 7 (and/or 64-bit OS machine), you will get the same error. So, from the IIS 7, right click on the applications' application pool and go to "advanced settings" and change "Enable 32-Bit Applications" to "TRUE".
Restart your website and it should work.
Solution 3
In Visual Studio, Right Click your project -> On the left pane click the Build tab,
under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)
I hope this helps someone! :)
Solution 4
If you encounter this error when you click green arrow button to run the application, but still want to run the app in 64 bit. You can do this in VS 2013, 2015, 2017, and 2019
Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express
Or you can do it per project at Project Properties > Web > Bitness
Solution 5
I just had this problem also. Tried all the suggestions here, but they didn't help.
I found another thing to check that fixed it for me. In Visual Studio, right-click on the project and open "Properties". Click on the "Compile" (or "Build") tab and then click on "Advanced Compile Options" at the bottom.
Check the dropdown "Target CPU". It should match the "Platform" you are building. That is, if you are building "Any CPU" then "Target CPU" should say "Any CPU". Go through all of your Platforms by making them active and check this setting.
![David Brown](https://i.stack.imgur.com/SOucS.jpg?s=256&g=1)
David Brown
Updated on July 09, 2022Comments
-
David Brown almost 2 years
I'm calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is:
BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
At first, I had my projects set to the Any CPU platform, so I changed them both to x86, but this error is still occurring. That's really the only fix I know for this.
The DLLs aren't corrupt or anything, because I can use them with other programs (that I don't have the source to). I thought that perhaps it wasn't finding a dependency, but I checked and they're all there. Plus, wouldn't it throw a
DllNotFoundException
in that case?What else can I do? And before you say "Use a 64-bit unmanaged DLL instead," let me point out that there isn't one. ;)
-
notidaho almost 12 yearsOh my days I've been fishing around installing extra IIS components when this was the answer... Can anyone suggest a downside to having this option selected?
-
Ben Power over 11 yearsHere's a good discussion on the question of performance regarding this: stackoverflow.com/questions/507820/…
-
Urchin about 11 yearsAnd for those of us just using the compiler, my fix was to add "/platform:x86" to the compiler flags.
-
Dan Gøran Lunde about 11 yearsThanks a lot. This got me too. Checked build in Configuration Manager and now it works (WPF desktop application).
-
Will Calderwood almost 11 yearsIf you've done all the above, and checked your platform setting, build configuration settings, cleaned the solution and it's still not working - search for all instances of the DLL and delete them.
-
Marvin Thobejane over 10 yearsA brief summary of the link would be helpful @Shaul :)
-
Marvin Thobejane over 10 yearsMagnificent. Thanks, the briefing adds content to your comment
-
user2173353 over 10 yearsI have a problem with SharpSvn and this doesn't help. :( Sth is very wrong with this assembly I tell you...
-
Jowen over 10 yearsThis fixed it for me as well. I had to adjust the "platform target" on the "Build" tab.
-
greg over 10 yearsthis is the closest answer to what i was experiencing. I had a dll that had to be x86. I used it in another project , which was AnyCPU by default. They just need to match. In this case, it didnt make much difference, so I changed the new project to x86.
-
trysis almost 10 yearsIt would be helpful if you could indicate where in Visual Studio to find this option.
-
Drew Noakes almost 10 years@trysis, this option is in the Build page of the project settings pane.
-
trysis almost 10 yearsI was saying it would be helpful to put it in. As this answer stands, there is no context to tell the hapless, possibly new user of StackOverflow where to find it.
-
user1069816 about 9 yearsThis fixed my issue in VS2013, I found an alternative fix is to leave "Platform target" as "Any CPU" but check the "Prefer 32-bit" check box.
-
user1069816 almost 9 yearsAlthough you need to be using .NET 4.5 or higher to be able to check the "Prefer 32-bit" check box
-
increddibelly over 8 yearswith VS 2015 this is still valid - although I didn't need to restart :)
-
kristianp over 7 yearsThis answer is a wtf for me, because the question doesn't mention IIS at all!
-
afterxleep about 7 yearsThat's it! Re-visit your Configuration Manager settings is the answer.
-
Adolfo Correa almost 7 yearsWhat is the path for get this form? I can't find it
-
Drew Noakes almost 7 yearsIn Visual Studio, right click the project and choose 'Properties'.
-
Nathan over 6 yearsThanks. I tried so many and nothing helped. You are my life saver. I have 64bit OS, installed a 64 bit Visual studio [which still runs as 32bit for unknown reason]. When I put my Platform Target to x64, it was throwing error BadImageFormatException. With your fix, it worked. I gave you an upvote. You rock
-
paibamboo over 6 yearsI'm glad I could help :)
-
yushulx about 6 yearsThis is the answer I need. Thanks a lot!
-
samir105 about 6 yearsThanks a lot, this solved the problem. It works well with Visual Studio 2017 32-bit process.
-
Ismayil S almost 6 yearsYes, but I converted my project in 'Any CPU' to 'x64'. My 32 bit project working fine but the same code I converted to 64 bit, that project not working fine as 32 bit. Can you please give me the proper 64 bit conversion process...
-
Marvin Thobejane almost 6 years@IsmayilS make sure you're using a 64-bit version of the library you're linking to
-
N4ppeL about 5 yearsif you're on 64 bit, also check the "prefer 32 bit" flag. I had to disable it for me.
-
NoBugs over 4 yearsSo x86 dlls are different from x64? Is there a way to detect this on a referenced dll file?
-
Joe over 4 years@NoBugs Looks like you can. Have a look at this thread
-
Mani about 4 yearswith above setting only it was not working, I did one ore change in a project setting
-
Saumil about 4 yearsI think something to keep an eye on is when you do change it 'True' it might remove the added 'Applications' which you will have to add again.
-
ransems almost 4 yearsWas the simple answer to the problem presented. Worked for me.
-
Manuel Plaza almost 4 yearsHooorahhhh! I will save this tip to my knowledge box for the future. Thanks! This worked for my Web API that had to reference a DLL that ran only in x64.
-
ouflak about 3 yearsNice one. This is going to help a few people in my department. Now we just have to sort why.
-
Roger Far over 2 yearsI upgraded to VS2022 and for some reason 1 project switched the platform target to x86. Even though the solution manager shows Any CPU, it was still trying to compile to x86.
-
Moruling James over 2 yearsThis worked for me. Thanks!
-
Erik over 2 yearsIt worked perfect for me. Thanks !
-
Stefan almost 2 yearsThis answer is hidden between all repeating solution for checking 'target architecture'. This answer should stand out more as it presents a new idea (which lead for my case to the resolution).