CXXI Brings Advanced C++ Interop To Mono
CXXI, a new C++ Interop framework, allows easy interoperability between C# and C++ in Mono. Developers can, from C#, easily instantiate C++ objects, invoke C++ methods, subclass C++ classes, and more.
Normally, for any managed code to call native libraries, you need to either use P/Invoke, Com Interop or an interceptor, each options having a lot of boiler plate code. Some things like subclassing are not even possible. With CXXI, this is made much simpler.
How does it work? The CXXI toolchain creates a CXXI Binding and produces a .NET library. The library contains a C# class, which acts as a wrapper to the underlying C++ class in the native library. Miguel de Icaza explains in detail about how this is made possible.
How does this compare with the alternatives? .NET already has C++/CLI which allows managed C++ code to interop with C#. However this is not the same as using existing native C++ libraries directly in C#, which still requires one of the above three methods (besides it is not supported by mono). Then there is SWIG which generates a wrapper/interface around C++ classes to expose it to various higher level programming languages including C#. However, CXXI is different – Miguel explains:
This one supports not only creating a binding and consuming a C++ API, but creating new subclasses of the existing C++ class and passing those instances back to C++ for consumption. This is done by patching the vtables when the user overrides methods.
Brandon Holt, Preston Briggs, Luis Ceze, Mark Oskin May 21, 2015
Kai Kreuzer, Olaf Weinmann May 21, 2015