开发者

C# interface C++ DLL?

How do I call functions from a C++ DLL?

The C++ DLL contains functions like this:

__declspec(dllexport) bool Setup () { return simulation.Setup (); } 

The C# program does this:

 [DllImport("mydll.dll")]
 [return: MarshalAs开发者_如何学运维(UnmanagedType.Bool)]
 public static extern bool Setup();

The C# program crashes with the following message when it tries to load (?) the DLL:

An unhandled exception of type 'System.BadImageFormatException' occurred in TestFrame.exe

Additional information: There was an attempt to load a file with a wrong format (exception from HRESULT: 0x8007000B)

The C++ DLL is a standard DLL (no MFC, no ATL).


This error occurs when you try to load a 32-bit DLL into a 64-bit process. (Or vice-versa)
Until VS2010, C# projects are target any CPU by default and will run as 64-bit on a 64-bit OS.

You need to go to the Build tab in Project Properties and set the C# project to x86 only.


Is it possible that your exe and dll have different bitness (i.e. one is 64 and the other 32)?


Have you tried compiling the code for x86 platform? See this blogpost for instructions.


It may not be appropriate for you to set the CPU architecture (platform target) for the assembly, but instead you can specify an alternate path to find the correct DLL to load.

How to use the correct unmanaged DLL file according CPU architecture? (32 / 64 bits)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜