[Mono-list] Pure wrapper calling native code

Jonathan Pryor jonpryor at vt.edu
Thu Aug 5 13:54:03 EDT 2010

Short [0] version:  http://www.mono-project.com/dllimport

> Brendon Chung wrote:
> > I need this program to access an unmanaged C++ dll.

Which prompts the single most important question: do you have a version
of this C++ DLL built for the Mac platform?  (For example, if the C++
DLL is `foo.dll`, do you have a `libfoo.dylib` file?)

If you don't have a Mac platform version, do you have source for the C++
DLL and the ability to port+compile the source for Mac?

If you don't have a .dylib, and you don't have source, then NO, you
cannot use this C++ library on Mac (or any other non-Windows platform).
> > My first attempt at this was using the DllImport call, but the call always
> > fails out for me.

DllImport requires two things:

 1. A platform-native library (e.g. libfoo.dylib)
 2. An 'extern "C"' API.  No C++ classes, exceptions, name mangling, 
    etc.  This can be written in C++, you just can't expose C++-specific
    details from the API.

SWIG[1] can help to write (2).

> I'm now trying to write a managed C++ wrapper dll to
> > bridge between my C# program and the unmanaged C++ dll.
> > 
> > My question is: is this even possible?  In order to be Mac-compatible, I
> > need to compile my C++ wrapper with /clr:pure and /nodefaultlib. But after
> > some research, it seems that /clr:pure precludes the possibility of
> > importing/using any unmanaged code.

Yup.  /clr:pure is only useful for complete C++/CLI code bases which
don't use native code.  This does not describe your scenario;
consequently, the only way to use C++/CLI would be to avoid /clr:pure,
and thus produce platform-specific code, which would prevent you from
using it on non-Windows platforms.

 - Jon

[0] OK, very, very Long version.
[1] http://www.swig.org

More information about the Mono-list mailing list