0

I have built a cpp dll with output configuration manager settings as shown in the snapshot

snapshot.

It builds successfully and dll gets created.Then to consume it I have written a c# project that includes the dll as follows

[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport(@"somePath\ImgProcessorTrial.dll", EntryPoint = "ImgProc")]

After that I built it with the configuration as shown in the figure

consumer_configuration

When i try to run this application I get the following output in the console.

BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

After that When I change the build configurations and try to build it again the cpp project does not build and gives a lot of errors of the type as given below.

unresolved external symbol for all functions of leptonica library

This particular library leptonica and all the necessary ones are built individually on a 64 bit machine.When I try to change the configurations of the c# project I get the exception

DLLNotFoundException

What I do in the project is invoke the dll functions to read an image and manipulate it.I thought it was related to the format of the image i am trying to read.So I tried to Run the application with a .jpeg and .tif but with the same output always.I am doing all of this on a visual studio 2013 and framework 3.5 on a 64 bit machine.What is the exact meaning of the BadImageFormatException and how to solve it?Is it a code issue or architecture issue?

nnm
  • 1,335
  • 5
  • 18
  • 32
  • try to set plateforme as x86 or x64 instaed of any CPU – NicoD Jun 27 '14 at 12:28
  • setting to x64 gives the dllNotFoundException and setting to to x86 has the same result of badImageFormatException – nnm Jun 27 '14 at 12:33
  • so keep x64 settings. where is your dll ? try inside bin folder of the c# project . – NicoD Jun 27 '14 at 12:38
  • Please provide an answer if you have an absolute idea of what the issue is – nnm Jun 27 '14 at 12:53
  • Standard mistake, the Platform name in that dialog is meaningless for .NET programs. Right-click your EXE project, Properties, Build tab. Set the Platform target there, you need to tick the "Prefer 32-bit" option so it is compatible with your 32-bit C++ DLLs. – Hans Passant Jun 27 '14 at 13:11
  • @HansPassant I am not able to enable prefer 32-bit option as is it uneditable. – nnm Jun 27 '14 at 13:17
  • You are targeting an old .NET version, use x86 then. – Hans Passant Jun 27 '14 at 13:23
  • @HansPassant My target when x86 gives the same error and with ANY CPU also gives the same error.Will remaking the project in a higher .net version help.Currently i was using 3.5.If yes do you know how to change it from 3.5 to 4 or higher without remaking if that is at all possible. – nnm Jun 27 '14 at 13:35

1 Answers1

0

BadImageFormat is not related to .jpeg or .tif...

BadImageFormat

This exception is thrown when the file format of a dynamic link library (.dll file) or an executable (.exe file) does not conform to the format that is expected by the common language runtime.

In your case you're calling a DLL that is itself 64 bit so you have to compile your project to x64. You get the BadImageFormat when targeting x86

NicoD
  • 1,179
  • 8
  • 19