0

I know that this question has been asked a lot but none of the solutions have worked for me so far. I am trying to build a project using a third party dll. I am using VS2015 and targeting .NET 4.5.2 framework. I have set the solution platform to AnyCPU and platform target for the project to AnyCPU as well. I get the following exception when I run the project "An unhandled exception of type System.BadImageFormatException occurred in mscorlib.dll

Additional information: Could not load file or assembly XXX, Version=1.0.5842.29961, Culture=neutral, PublicKeyToken=null or one of its dependencies. An attempt was made to load a program with an incorrect format."

I have tried to figure out the headers of the dll using corflags and following are the results

Version : v4.0.30319
CLR Header : 2.5
PE : PE32
CorFlags : 0x10
ILONLY : 0
32BITREQ : 0
32BITFREQ : 0
Signed : 0

As per this answer i should be setting platform targets to AnyCpu which is I am doing, but still can't get it to work.

Community
  • 1
  • 1
pxjcode
  • 51
  • 1
  • 1
  • 6
  • A library referenced in your project is X86_64 only, the key is `or one of its dependencies`. – Gusman Apr 27 '17 at 17:28
  • Do you know if the third party dll that you are referencing is Mixed or pure managed dll? – Mudi Apr 27 '17 at 17:28
  • 1
    The solution platform selection is completely irrelevant to C# projects. Right-click the EXE project > Properties > Build tab > Platform target selection. This looks like a 64-bit mixed-mode assembly so you need AnyCPU here and untick Prefer 32-bit. Repeat for the Release build. If you still have trouble then *do* avoid talking to complete strangers that cannot possibly know anything about this, pick up a telephone. – Hans Passant Apr 27 '17 at 17:33
  • @mudi I believe it is mixed dll. There were two more dlls along with this. The additional dlls could not be added as VS threw the error that the dll might not be a valid assembly or COM component. Corflags couldn't show the headers of those additional files as it threw the error "The specified file does not have a valid managed header" – pxjcode Apr 27 '17 at 17:34
  • @pxjcode, Those are probably native c++ dlls which is why you can't add those. Those native dlls are consumed by this mixed 3rd party dll, which means that these native dlls need to be available (either in the same location where mixed dlls is or application's search path) when your application tried to load it. Please note that when you add the reference of the mixed dlls, the VS copy it by default in the project's OUTPUT location during the compile time, however, it won't copy the native one as it doesn't know about it... – Mudi Apr 27 '17 at 17:48
  • @pxjcode try to copy the native dlls in the output location of your project (where the exe is) and run the application again.. – Mudi Apr 27 '17 at 17:50
  • @pxjcode @pxjcode So basically, `BadImageFormatException` occurs if the bitness of the referencing dll isn't matched with the dll/exe it's being referenced. If you have project with any CPU, it can reference the mixed dlls (regarding of its bitness). However, during the runtime, based on the OS bitness, you need to provide the same dll with the matching OS bitness... – Mudi Apr 27 '17 at 18:04
  • @Mudi Copying the native dlls to the output location and changing the project to x86 solved it. Thank you so much for your help. – pxjcode Apr 27 '17 at 18:19

0 Answers0