"An attempt was made to load a program with an incorrect format" even when the platforms are the same

571,379

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.

enter image description here

Solution 3

In Visual Studio, Right Click your project -> On the left pane click the Build tab,

Project properties, build tab

under Platform Target select x86 (or more generally the architecture to match with the library you are linking to)

Project properties, platform target

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 IIS Express 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.

Share:
571,379
David Brown
Author by

David Brown

Updated on July 09, 2022

Comments

  • David Brown
    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
    notidaho almost 12 years
    Oh 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
    Ben Power over 11 years
    Here's a good discussion on the question of performance regarding this: stackoverflow.com/questions/507820/…
  • Urchin
    Urchin about 11 years
    And for those of us just using the compiler, my fix was to add "/platform:x86" to the compiler flags.
  • Dan Gøran Lunde
    Dan Gøran Lunde about 11 years
    Thanks a lot. This got me too. Checked build in Configuration Manager and now it works (WPF desktop application).
  • Will Calderwood
    Will Calderwood almost 11 years
    If 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
    Marvin Thobejane over 10 years
    A brief summary of the link would be helpful @Shaul :)
  • Marvin Thobejane
    Marvin Thobejane over 10 years
    Magnificent. Thanks, the briefing adds content to your comment
  • user2173353
    user2173353 over 10 years
    I have a problem with SharpSvn and this doesn't help. :( Sth is very wrong with this assembly I tell you...
  • Jowen
    Jowen over 10 years
    This fixed it for me as well. I had to adjust the "platform target" on the "Build" tab.
  • greg
    greg over 10 years
    this 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
    trysis almost 10 years
    It would be helpful if you could indicate where in Visual Studio to find this option.
  • Drew Noakes
    Drew Noakes almost 10 years
    @trysis, this option is in the Build page of the project settings pane.
  • trysis
    trysis almost 10 years
    I 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
    user1069816 about 9 years
    This 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
    user1069816 almost 9 years
    Although you need to be using .NET 4.5 or higher to be able to check the "Prefer 32-bit" check box
  • increddibelly
    increddibelly over 8 years
    with VS 2015 this is still valid - although I didn't need to restart :)
  • kristianp
    kristianp over 7 years
    This answer is a wtf for me, because the question doesn't mention IIS at all!
  • afterxleep
    afterxleep about 7 years
    That's it! Re-visit your Configuration Manager settings is the answer.
  • Adolfo Correa
    Adolfo Correa almost 7 years
    What is the path for get this form? I can't find it
  • Drew Noakes
    Drew Noakes almost 7 years
    In Visual Studio, right click the project and choose 'Properties'.
  • Nathan
    Nathan over 6 years
    Thanks. 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
    paibamboo over 6 years
    I'm glad I could help :)
  • yushulx
    yushulx about 6 years
    This is the answer I need. Thanks a lot!
  • samir105
    samir105 about 6 years
    Thanks a lot, this solved the problem. It works well with Visual Studio 2017 32-bit process.
  • Ismayil S
    Ismayil S almost 6 years
    Yes, 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
    Marvin Thobejane almost 6 years
    @IsmayilS make sure you're using a 64-bit version of the library you're linking to
  • N4ppeL
    N4ppeL about 5 years
    if you're on 64 bit, also check the "prefer 32 bit" flag. I had to disable it for me.
  • NoBugs
    NoBugs over 4 years
    So x86 dlls are different from x64? Is there a way to detect this on a referenced dll file?
  • Joe
    Joe over 4 years
    @NoBugs Looks like you can. Have a look at this thread
  • Mani
    Mani about 4 years
    with above setting only it was not working, I did one ore change in a project setting
  • Saumil
    Saumil about 4 years
    I 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
    ransems almost 4 years
    Was the simple answer to the problem presented. Worked for me.
  • Manuel Plaza
    Manuel Plaza almost 4 years
    Hooorahhhh! 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
    ouflak about 3 years
    Nice one. This is going to help a few people in my department. Now we just have to sort why.
  • Roger Far
    Roger Far over 2 years
    I 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
    Moruling James over 2 years
    This worked for me. Thanks!
  • Erik
    Erik over 2 years
    It worked perfect for me. Thanks !
  • Stefan
    Stefan almost 2 years
    This 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).