33

Any thoughts on what might be causing this exception?

I have a webservice proj, when i load the link i get

Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Exception Details: System.BadImageFormatException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Inner exceptions:

[BadImageFormatException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

[ConfigurationErrorsException: Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

[HttpException (0x80004005): Could not load file or assembly 'Interop.DIB' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

Version Information:
Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

Sharpeye500
  • 8,775
  • 25
  • 95
  • 143
  • I don't know what "dib" is but looks like external dll missing or not supported (compiled for 32bit but used in 64bit?) – Simon Feb 23 '12 at 19:05
  • Why haven't you turned on fusion logging yet? Also, what is "interop.DIB", where is it, and is it compiled for the same architecture as the rest of the solution? –  Feb 23 '12 at 19:06
  • **Related posts** - `1.` [Could not load file or assembly … An attempt was made to load a program with an incorrect format (System.BadImageFormatException)](https://stackoverflow.com/q/11370344/465053) **&&** `2.` [Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format](https://stackoverflow.com/q/1648213/465053) – RBT Apr 17 '18 at 06:20

14 Answers14

92

Ok the answer is Got to Start->Run->type inetmgr and on the left application pools, select DefaultAppPool and the virtual directory name of the app and for both make sure to enable 32 -bit applications to true, am using IIS7.0 and windows 7 64-bit. enter image description here

Lucky
  • 16,787
  • 19
  • 117
  • 151
Sharpeye500
  • 8,775
  • 25
  • 95
  • 143
  • Wow, thank goodness for this answer. I wish the error messages would reference the policy/property which was specifically causing the error. It would have been nice to hear that my dll had an incorrect format because IIS was set not to allow 32-bit applications and was thus expecting only 64-bit components – Erikest Sep 20 '12 at 00:10
  • 1
    Seem's you made my day, as new application pool on x64 machine doesn't work with x32 dll by default – Johnny_D Oct 24 '12 at 12:19
  • It is weird that the default ASP.NET 4.0 app pool is set not to allow 32-bit applications. Anyway thanks, this solved the issue for me! – Jaanus Varus May 10 '13 at 06:12
  • Hey... Could u tell us whene we can do it "ake sure to enable 32 -bit applications to true" ? – NoWar Jun 22 '13 at 15:08
  • @Sharpeye500 after all issues i faced this is the final issue i faced and solving this has started my webservice..thanks a lot.. – Lucky Feb 06 '14 at 12:56
30

BadImageFormatException usually means 64 vs 32 bit conflict. One of the assemblies is set to a specific platform i.e. 64 bit or 32 bit while the other is set or defaults to a different one. Check if both assemblies are for the same platform, preferably "Any CPU". In other words it could be that a 64 bit assembly is trying to load 32 bit one or vice versa.

This also applies if you're calling a COM or a DLL which is compiled for different platform, for example you call 32 bit COM/DLL from an assembly on a 64 bit system where assembly's platform would default to x64. In this case adjust your assembly's platform to match.

To change platform go to project Properties -> Build -> Platform.

Maciej
  • 7,871
  • 1
  • 31
  • 36
16

I had this issue, when trying to use 64-bit .dlls in my ASP.Net project, in Visual Studio 2013.

The solution was to click on Tools\Options, and tick this box:

enter image description here

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
6

I've started to get this error with Visual studio 2022.

My project is set to AnyCPU, but some dll is compiled to x86, and I was unable to change it because I don't have the source. In previous versions of VS the project works.

I realized that in previous versions of visual studio (vs 2019 for example) the default IIS is to run on x86 (after all it is an x86 process too) as you can see here: enter image description here

but in VS 2022 this option is checked by default.

Solutions:

The first solution is to uncheck the option 'Use the 64 bit version of IIS Express for web sites and projects' in Options -> Projects and Solutions. However, this will change the IIS Express version for all projects.

The second solution is to change IIS Express only for that solution (I prefer that because most of my projects don't have specific compiled dll's)

1- Go to Project Properties -> Web. In Servers section change bitness to x86: enter image description here

Conclusion

The best solution is to have all dll compiled to the same platform (x86, x64, AnyCPU, etc.), but if like me you were unable to do it, this will solve for you.

fsbflavio
  • 664
  • 10
  • 22
3

The most common cause in my experience is that you made a change to a referenced assembly that requires rebuilding other assemblies using that changed assembly, and didn't rebuild them.

Example #1: you have an EXE that references a DLL. You add something to the referenced DLL that adds a new method, new parameter, whatever. This changes the external "signature" of the DLL; that is, the location in memory of various entry points. You don't rebuild the EXE. When the EXE loads and tries to reference the new DLL, its old entry point is no longer valid, so it cannot execute the code it needs.

Example #2: you have an x86 EXE that references a DLL. This DLL must also be compiled for x86 (or Any CPU). If you rebuild it for x64, the EXE, running in a 32-bit space, will not understand the instructions and register references to the 64-bit "extended" world, and will cry uncle.

KeithS
  • 70,210
  • 21
  • 112
  • 164
2

I had the same issue on Visual Studio 2015 on Windows 10 x64. I was already compiling in Any CPU mode. This was a default MVC4 application (nothing added). I found a simple solution over here that worked for me: https://github.com/aspnet/Home/issues/524

In VS 2015: Tools > Options > Projects and Solutions > Web Projects > Use the 64 bit version of IIS Express for websites and projects

1

My Error fixed, with change of build option to:

in my C#.net win project:

Properties > Build > Platform Target > 'x86'

I accidentally change its value to 'Any CPU' and forget to change it.

Zolfaghari
  • 1,259
  • 1
  • 15
  • 14
1

If I had to take a guess, it is either a) not finding the interoped assembly or b) the COM DLL is not registered in the local registry. Simply copying DIB to the /bin folder is not enough when you are monkeying with COM.

I believe (B) is the most likely answer to what is happening.

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
0

What worked for me was to add the assembly to GAC. To do that I ran gacutil -i PATH_TO_ASSEMBLY from Visual Studio Command Prompt

  • UPDATE: What also worked was to find out the application domain using **AppDomain.CurrentDomain.BaseDirectory** and copy the assembly there – Dublin7 May 29 '13 at 08:54
0

I finally got around this exception by deleting the entry in the applicationhost.config for IIS Express (C:\Users{username}\Documents\IISExpress\config\applicationhost.config).

I had also stopped the IIS express instance, cleaned and rebuilt in VS. Then change the config file, then restarted VS 2013.

AlignedDev
  • 8,102
  • 9
  • 56
  • 91
0

An alternate to this issue is to change the build properties of your project.

For vb.net got to Project--> Properties --> Compile --> Advanced Compile Options Here change the target CPU

0

What worked for me is doing a BIOS update on my machine!

radkan
  • 599
  • 4
  • 9
0

This work for me, if not work for you, don't vote down. It's impolite

So it works if I change in the c# project from Any CPU to x86(all dlls are compiled in WIN32) from Properties -> Build -> Platform Target. I work on a 64-bit Win7 pc and aparently if I use Any CPU as target it does't find the wrapper dll.

https://www.codeproject.com/Questions/358717/Could-not-load-file-or-assembly-dll-file
Trương Long
  • 100
  • 1
  • 11
-2

My Error fixed, with change of build option to:

in my C#.net win project:

Properties > Build > Platform Target > 'x86'

I accidentally change its value to 'Any CPU' and forget to change it.

It's work for me

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
H.Ken
  • 1