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

.Net64 BitPinvoke32 BitBadimageformatexception

.Net Problem Overview


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. ;)

.Net Solutions


Solution 1 - .Net

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 2 - .Net

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 3 - .Net

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 - .Net

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 - .Net

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.

Solution 6 - .Net

If you are using Any CPU, you might encounter this issue if the Prefer 32-bit option is checked:

http://i.imgur.com/d6lCOlP.png">

Make sure you uncheck this option in the project's property's Build tab!

enter image description here

Solution 7 - .Net

In my case I was using a native DLL in C#. This DLL depended on couple of other DLLs that were missing. Once those other DLLs were added everything worked.

Solution 8 - .Net

A bit off topic for this post, but searching for this error message brought me here.

If you are building through team system and getting this error, the build definition process tab has a "MSBuild Platform" setting. If this is set to "Auto", you may experience this problem. Changing it to "X86" can also resolve the error.

Solution 9 - .Net

  1. Go to: Tools → Options → Projects and Solutions → Web Projects → Use the 64 bit version of IIS Express.
  2. Change below setting for web service project:

enter image description here

Solution 10 - .Net

With Visual Studio 2019 I had a similar issue when I wanted to run tests (MSTest directly from VS). In my case I only had an x64 native DLL and I received this error message. First, I thought it is because Visual Studio runs as x86 but this page helped me to solve the issue:

Run unit test as a 64-bit process

It says

  1. Set your projects to Any CPU
  2. Explicitly define processor architecture

I did both (I explicitly set x64) and then my tests started to work.

Set processor architecture explicitly to x64

Solution 11 - .Net

We had a similar issue and we managed to fix it by setting the Platform target to x86. Project Properties-> build

Solution 12 - .Net

Building on the answer of @paibamboo

He said: Go to: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express

My coworker had this box checked (he explicitly looked for it), but had the error message in question. After some hours he unchecked the box and checked it again. Lo and behold: The code now ran with success.

It seems, that there are two places where the state of this box ist saved which became out of sync. Un- and rechecking it synced it again.

Question for more knowledgable users: Was there an update or something last week (for VS 2015) which de-synced the states?

Solution 13 - .Net

Also see this answer, which solved the same problem for me.

> Posted by Luis Mack on 5/12/2010 at 8:50 AM I've found the same problem, only for a specific project when compiling on a 64-bit machine. A fix that SEEMS to work is to manually alter one character in the image stream EVERY TIME the usercontrol or form is edited in the designer > > AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w > >Change to > > AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj0yLjAuMC4w > >That is 00LjAuMC4w back to 0yLjAuMC4w at the end of the line (00 back to 0y)

Solution 14 - .Net

In my case it was wrong content of the file. DLL was downloaded from the web, but content of the DLL was HTML page :D Try to check if it is binary file, if it seems like correct DLL :)

Solution 15 - .Net

In my case, I am using a tiny .exe that reloads the referenced DLLs via Reflection. So I just do these steps which saves my day:

From project properties on solution explorer, at build tab, I choose target platfrom x86

Solution 16 - .Net

In my case, I was running tests through MSTest and found out that I was deploying both a 32-bit and 64-bit DLL to the test directory. The program was favoring the 64-bit DLL and causing it to fail.

TL;DR Make sure you only deploy 32-bit DLLs to tests.

Solution 17 - .Net

If you are importing unmanaged DLL then use

CallingConvention = CallingConvention.Cdecl 

in your DLL import method.

Solution 18 - .Net

I got this issue solved in the 'Windows' way. After checking all my settings, cleaning the solution and rebuilding it, I simply close the solution and reopened it. Then it worked, so VS probably didn't get rid of some stuff during cleaning. When logical solutions don't work, I usually turn to illogical (or seemingly illogical) ones. Windows doesn't let me down. :)

Solution 19 - .Net

I was able to fix this issue by matching my build version to the .NET version on the server.

I double clicked the .exe just to see what would happen and it told me to install 4.5....

So I downgraded to 4.0 and it worked!

So make sure your versions match. It ran on my dev box fine, but server had older .NET version.

Solution 20 - .Net

We were having the same issue in .NET core. The solution was to download 32-bit .netcore runtime, and having your project target x86

In your csproj file add

  <PropertyGroup>
    <PlatformTarget>x86</PlatformTarget>  
  </PropertyGroup>

  <PropertyGroup>
    <RunCommand Condition="'$(PlatformTarget)' == 'x86'">$(MSBuildProgramFiles32)\dotnet\dotnet</RunCommand>    
  </PropertyGroup>

This was used for a Windows machine, you'd have to adjust paths and such for Linux/OSX

Solution 21 - .Net

In my case, I didn't have the correct project set as the start-up project. I went to solution settings and selected the correct startup project and it worked

Solution 22 - .Net

In my case this same error happened after publishing. I had published before with another platform configuration.

The solution was to cleanup the publish folder first, then it worked.

(alternatively set the "delete existing files" option to true)

Solution 23 - .Net

Another reason that may cause this exception, is the C++ Redistributables missing for the target platform of your Dll. I had a tough time finding out when testing on a VM.

Solution 24 - .Net

For .net core, make sure Ijwhost.dll is in the output directory, sometimes it isn't being copied and this will cause the error. See https://github.com/dotnet/runtime/issues/38231 and also https://stackoverflow.com/a/58773266/9665729

Solution 25 - .Net

Please also note that the version of used dll file(s) (in my case "WebView2Loader.dll") which is in use is very crucial. I had almost the same problem with "Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64" when I tried to use the WebView2 component in the MMC Snap-Ins with types of "HTMLView" or "FormView".

I just copied the referenced dll file in a proper path that was accessible for the project (you could just put it beside your project output files first to test it) and then WebView2 browser started to function as expected. Microsoft error messages sometimes (at least in my case) was a little bit misleading and did not convey enough and to the point information.

I received "BadImageFormatException" that normally occurs when you mix platform targets (for example using a dll file compiled in X64 in an application that targeted for x86 or vice versa) or mix native code and .NET but that was not my problem at all. I hope this help one who may stuck in.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid BrownView Question on Stackoverflow
Solution 1 - .Netbluwater2001View Answer on Stackoverflow
Solution 2 - .NetDavid BrownView Answer on Stackoverflow
Solution 3 - .NetMarvin ThobejaneView Answer on Stackoverflow
Solution 4 - .NetpaibambooView Answer on Stackoverflow
Solution 5 - .NetDenisView Answer on Stackoverflow
Solution 6 - .NetDrew NoakesView Answer on Stackoverflow
Solution 7 - .NetTomasz StypichView Answer on Stackoverflow
Solution 8 - .NetStingyJackView Answer on Stackoverflow
Solution 9 - .NetManiView Answer on Stackoverflow
Solution 10 - .NetluviktorView Answer on Stackoverflow
Solution 11 - .NetJoeView Answer on Stackoverflow
Solution 12 - .NetMilConDoinView Answer on Stackoverflow
Solution 13 - .NetShaul BehrView Answer on Stackoverflow
Solution 14 - .NetLudwoView Answer on Stackoverflow
Solution 15 - .Netasdf_enel_hakView Answer on Stackoverflow
Solution 16 - .NetMike CluckView Answer on Stackoverflow
Solution 17 - .NetRamkumar ShanmugamView Answer on Stackoverflow
Solution 18 - .Netuser1771386View Answer on Stackoverflow
Solution 19 - .NetNateousView Answer on Stackoverflow
Solution 20 - .NetSamir BanjanovicView Answer on Stackoverflow
Solution 21 - .NetConnerView Answer on Stackoverflow
Solution 22 - .NetAllieView Answer on Stackoverflow
Solution 23 - .NetcrankedrelicView Answer on Stackoverflow
Solution 24 - .NetNeoView Answer on Stackoverflow
Solution 25 - .NetamirfgView Answer on Stackoverflow