0
votes

When I am providing 64bit C++ dll as a reference to a C# project which will generate AnyCPU C# dll. I am able to generate the C# dll but when I launch application An error comes saying,

Type : System.BadImageFormatException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxx Message : Could not load file or assembly 'MyDLLName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

As the C# dll is unable to fetch the C++ dll. So, how can I resolve this error?

1
Force a 64 bits build (x64 platform). You cannot generate for AnyCPU.Yves Daoust
AnyCPU tends to be confused with the solution platform name. Irrelevant for a C# project. Only the settings for the EXE project matters. Right-click > Properties > Build tab. The "Prefer 32-bit" checkbox must be off, "Platform target" must be AnyCPU or x64. Repeat for the Release configuration.Hans Passant

1 Answers

1
votes

You are trying to load a 64 bit library from a 32 bit process. Your C# dll can be AnyCPU, but the executing program has to be 64 bit to be able to load your C++ dll. In your project settings, check if you got the "prefer 32 bit" flag set and uncheck it - or simply build a 64 bit executable.