简体   繁体   中英

Call exported functions from C++ DLL library in C#

I have no problems to call C++ DLL functions from C# by using dynamic linking, but got problem when I tried to make it in dynamic way:

Trying to get DLL handler:

var DLL = Assembly.LoadFile(@"C:\project\mydll.dll");

And got exception:

An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll

Additional information: The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)

How to solve this problem?

Assembly.LoadFile is used to load .NET assemblies, it cannot load an a DLL that does not contain an assembly.

You'll need to use PInvoke to access the functions in the DLL. Also, you'll need to make sure that the names of the exported functions are not mangled as it can be almost impossible to work out what the function name is, and the name can change when built with different versions of a C++ compiler.

The Assembly.LoadFile should only be used for loading .NET assemblies. Pure Win32 dlls cannot be loaded this way.

In order to load a Win32 library in managed code use LoadLibrary Win32 call via PInvoke .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM