[Mono-dev] c# / c++ interop question

Zac Bowling zac at zacbowling.com
Fri Mar 10 01:09:37 EST 2006


I'm sort of cunfused on the direction (CLI to C++ or C++ to CLI?) and 
what is going to be hosting which. I believe this might help though.

There are few methods for access classes in CLI using C/C++.

One method is by calling the mono embedding api as miguel said its a 
tad slow in my experience but its nicer for somethings. In fact there 
is an application right now in the mcs source under "mcs/tools/cilc/" 
that will generate a binding library for you automaticly, but that 
won't give you nice C++ classes, just C based GObject classes (but its 
still totally accessable in C++).

The other big one is using function pointers (delagates in C#). I 
uploaded this example I created to show how: 
http://polystimulus.com/PinvokeCallbackTest.tar.gz

In this method I store all the function pointers (delegates in C#) in a 
C# struct and pass that to the C lib on the backend using p/invoke. You 
have to declare a struct in C that matches the one in C# so that it 
will marshal correctly, but its not all that hard once you get the hang 
of it. (See http://www.mono-project.com/Interop_with_Native_Libraries 
or the msdn docs on the Marshal class and MarshalAs attribute for more 
reference)

So even though it can be a little bit of work, it's totally possible.


-- 
Zac Bowling
http://zacbowling.com/


----- Message from jaytau at yahoo.com ---------
    Date: Thu, 9 Mar 2006 15:10:55 -0800 (PST)
    From: J <jaytau at yahoo.com>
Reply-To: J <jaytau at yahoo.com>
Subject: [Mono-dev] c# / c++ interop question
      To: mono-devel-list at lists.ximian.com


> Hi, I wrote a MS .NET 2.0 C# wrapper of a c++ 2d game engine.   The 
> engine (Torque Game Builder) is cross platform, so obviously it would 
> be benificial to port my wrapper to mono instead of Msft's 
> implementation.
>
>  To talk from c# to C++, I use PInvoke, which should work in the mono 
> world with little change needed.
>
>  However, going from C++ to C#, I am using C++/CLI.   Obviously, that 
> will not fly in the mono world, so I'd like to find out what 
> alternatives there are to do this in an elegant way.    (fyi, I am an 
> ok c++ dev, but i am only as advanced as kinda knowing how to use 
> function pointers and run-time linking)
>
>  So I am looking for the best way to talk from C++ to C#.  As I 
> mentioned, I mostly understand the concept of using function 
> pointers, would that be the best way?   or is there some way of 
> referencing a mono assembly inside a c++ project, so I can link to it 
> like a normal library?
>
>  Or, is there a better solution?
>
> Thanks for your time,
>
>  -Jason
>
>  PS:  (disclaimer: i pinged a similar question to the mono dev list 
> maybe 8 months ago, I am repinging to see what any answer deltas may 
> be :)
>
>
>
>
>
>
> ---------------------------------
> Brings words and photos together (easily) with
> PhotoMail  - it's free and works with Yahoo! Mail.


----- End message from jaytau at yahoo.com -----





More information about the Mono-devel-list mailing list