views:

45

answers:

1

First of all, I'd like to point out that I have no experience using Mono.NET, so bear with me if the questions are silly.

I'm working on an application that is written in native C++ code and works in Windows (VS2008) and Linux/Mac (gcc). There is a library that I am looking to integrate with, which is currently compiled in MS.NET (version 2.x).

I have researched and found that I can call into the library using the CCW interface that MS provides. Hence, there shouldn't be too many implementation woes in Windows.

My question is, if I manage to compile the library with the Mono .NET in Linux or Mac, how would I call into that code from my native application? I guess a shorter question may be, is it possible to compile libraries with the Mono compiler that can be called from native C++ code on non-Windows platforms?

Thanks!

+2  A: 

Yes, but you would have to embed Mono. Your native "host" app would have to initialize the Mono runtime then look up and execute MonoMethods on MonoObjects.

Note that because of P/Invoke it's a lot easier to call C APIs (i.e with extern "C" if you use C++) from Mono than it is to call Mono code from C. So your best strategy IMO is make a small C# "glue" library that does a lot of the interop work and exposes a simpler API for your C++ code to use.

For example, you might structure it so the host can call a static method in the glue library which would then create and manipulate objects from the library you're using, and make callbacks into the native host using P/Invoke.

There are also several tricks you can use with "unsafe" code in C# to simplify things - for example, the host could pass IntPtr values (pointers) into the C# glue library, then in an unsafe C# context you could cast them to pointers to C# structs with layouts matching the host's structs, and write directly into the host's structs.

mhutch