简体   繁体   中英

Using C++ dll object within C#

I have a third party C++ .dll which exports some classes which I wish to use from C#. The dll is a Win32 application and I'm working on Windows 8.1 using VS2013. When I set up the C# program running under 'Any CPU', I get the error "System.BadImageFormatException". I've read other places that this can be caused by combining x86 and x64. So I recompiled the C# program as x86. Now I get "Unable to load DLL 'libsword.dll': The specified module could not be found". I've made sure the dll is in the proper directory.

All of this works just fine on Linux using Mono.

At this point, I've got no idea how to proceed.

Thanks, Jon

BadImageFormatException is thrown when you treat regular native C++ code in a DLL as if it was .NET code. .NET also puts code in the .DLL files.

You'll need to create C# declarations for the C++ code, so that .NET knows how to transition from managed (.NET/C#) code to native (C++) code. This is fairly complicated, depending on what the code in your 3rd party library looks like. Too much for me to speculate what you need to do.

The problem was in the C++ library, which was originally written and developed for Linux. Export was defined as:

define SWDLLEXPORT _declspec( dllexport )

Which was used as:

const char * SWDLLEXPORT  function_name(args)

which worked in Linux but not in Windows. Changing the function signature to:

const char SWDLLEXPORT * function_name(args)

Works in both Linux and Windows.

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