[Mono-dev] C++ to C# to C++ interop, how can I do this in a mono-compliant way?

Robert Jordan robertj at gmx.net
Fri Sep 2 08:30:32 EDT 2005

J wrote:
> I am looking for a way of having a C++ exe integrate with a .NET dll.  I have a solution working under Windows (Microsoft.NET 1.1) however I would like to make this function under Mono.
> Here are the details:
> I currently am writing a C# wrapper of a C++ game engine.  (T2D by garagegames)  I wrote this on Windows (CLR v1.1) and the main way this works is by adding Managed code to the C++ engine (so now it is mixed Managed/Unmanaged C++) so it can directly call into my C# DLL, and using PInvoke to have the C# DLL talk balk to the C++ engine.
> The C++ engine itself is OS agnostic (it works on windows, linux and mac), and I would love to make this C# wrapper work under mono, so that it is OS agnostic as well.
> However, http://www.go-mono.com/faq.html#63  informs me that Mixed mode assemblies do not work under mono.
> Is there any way to have this work under Mono?   Please realize that the basic need is to have 1 instance of a C++ exe call into a .NET dll, and have that DLL be able to then execute functions in the C++ exe that called it.     So this requires a mono-equivlant of PInvoke, plus a way to have the C++ app call the C# app. 
> Help on this would be appreciated, otherwise it'll be Windows only!

You have 2 options to make this work under Mono:

1. Embed the Mono runtime in you C++ app. Have a look at samples/embed
    of the Mono source package.
    From C++ you can access the managed code using Mono's metadata API.
    C# can access unmaged code using internal calls you have to provide.

      - pretty straightforward (IMHO) but still a lot of work.

      - only for Mono. It won't work unter MSFT's runtime.

2. Build a C wrapper for the C++ library making it p/invoke

      - the wrapper can be made platform independend

      - a lot of work

I'd go for (2) because it is portable. (1) has more optimizing
potential because you can mix C++ and C# at some level.


More information about the Mono-devel-list mailing list