How to call a C# function in C++?

Get answers to all your basic programming questions. No Ogre questions, please!
Post Reply
limbo
Kobold
Posts: 27
Joined: Fri Dec 10, 2004 7:08 am

How to call a C# function in C++?

Post by limbo »

Hi there,

I have C# code that I want to use in my Ogre programs. I just don't know how to call C# functions in C++. I've read many books these days, but got no answer. So, state my question more clear:

1. Can unmanaged C++ code use C# class, like by using dll, or some other ways?
2. I know for sure that managed C++ code can use C# function, but I just don't know how. Can some one tell me how to do this?

Thanks!!!!!!!!!!!!!
User avatar
Nek
Gnoblar
Posts: 21
Joined: Mon Aug 18, 2003 6:06 pm
Location: Houston, Tx
Contact:

Post by Nek »

You first need to add an Ogre project to a managed application by messing with the project properties. Once you have an Ogre project in a managed space using managed C++, package your C# code in a dll and add it as a reference. Remember to add your C# code to a particular namespace. From your C++ project, add the "using namespace csharpnamespace;" where csharpnamespace is the name of your namespace. This takes a while to set up but is well worth it. You can use standard out of the box Ogre without changing the base code, although you will have to change an example to fit your needs.

Check out my comments from this post.

http://www.ogre3d.org/phpBB2/viewtopic. ... highlight=
User avatar
Nek
Gnoblar
Posts: 21
Joined: Mon Aug 18, 2003 6:06 pm
Location: Houston, Tx
Contact:

Post by Nek »

Then again, if you just want to avoid using C++, check out Axiom. Personally, I love the power of C++ and stick to Ogre for the rendering engine and only use vb.net and C#.net for my sound and tools.
alphabeta
Kobold
Posts: 34
Joined: Mon Jan 03, 2005 5:07 am

Post by alphabeta »

I think what he's asking is could unmanaged c++ call managed c# code.
Unfortunately, calling managed code requires the managed environment.

If you want to code managed, you should use axiom instead of wrapping ogre yourself. It you want it unmanaged, you'll unfortunately have to do without calling c# code.

Managed code can however call unmanaged code.
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 7
Contact:

Post by haffax »

I don't know if your question is Ogre specific. If not and it is important for whatever reason, you could use some middleware like COM. A COM-Wrapper for a C#-lib is easy to write, iirc (altough I have never done such things). And you could then call it from C++. But this is really circuitous.
team-pantheon programmer
creators of Rastullahs Lockenpracht
alphabeta
Kobold
Posts: 34
Joined: Mon Jan 03, 2005 5:07 am

Post by alphabeta »

Ugh. I'm personally not a big fan of COM, if you'd really like to call managed code from unmanaged c++, you can do it through a wrapper (http://www.codeproject.com/dotnet/bridge.asp), but you'll have to marshal your types yourself. There's no way to directly use managed code in an unmanaged environment, and there is a performance penalty with excessive marshalling, so use with caution.
limbo
Kobold
Posts: 27
Joined: Fri Dec 10, 2004 7:08 am

Post by limbo »

Thanks all!

Ok, now I know 1. I can't call C# functions with unmanaged C++ code. 2.As for wrapper, I never use COM before, so I'll leave it as a second choice. 3. Axiom seems a better choice, but I just heard that it is not updated that often, and I may miss some features that Ogre supports. Can someone tell me what features are lost in Axiom? 4. I've tried made managed Ogre project. The problem is that all project needs to inherit from ExampleApplication (or other classes) which is wirtten in C++. Managed class can't inherit from unmanaged class. So inheritance cause problem. 5. I've learned that there're some ways that you can compile unmanaged code to mangaged one. May be this will help? I don't know and don't know how to compile. :(
limbo
Kobold
Posts: 27
Joined: Fri Dec 10, 2004 7:08 am

Post by limbo »

Hi Nek,

I read your previous post. I can make a managed C++ project ( add __gc, and use .NET projects). Seems such projects can call c# functions, but they can't use Ogre base classes now.

The problem is, managed c++ can talk with C#, but not Ogre base classes;

unmanaged C++ can communicate with Ogre, but not C#.

So..........................
alphabeta
Kobold
Posts: 34
Joined: Mon Jan 03, 2005 5:07 am

Post by alphabeta »

Trust me, you don't want to go about making a .net ogre wrapper :wink:
It's true that axiom lags behind a bit, but it's certainly not bad. As for features you'll miss, not much..
Material scripts are almost up to date, mesh parsing caught up a bit ago. Nothing that would make you want to take the time to make a complete ogre3d wrapper
User avatar
Nek
Gnoblar
Posts: 21
Joined: Mon Aug 18, 2003 6:06 pm
Location: Houston, Tx
Contact:

Post by Nek »

I haven't measured the performance impact, which leads me to believe that I should, but you can call both. As alphabeta is hinting, Axiom may be your safest bet, but if you really want to do this... create some static manager classes that reference your classes, for instance, in the main application, do something like,

Code: Select all

void createscene() ...
{
..
..
Ogre stuff
..
..
ManagedCombatSession::InitializeCombat(mSceneMgr)
..
}

__gc class ManagedManager
{
   Ogre::SceneManager mSceneMgr;
   ..
   .. 
   yourdll::class *mSettings = load settings()

   static void InitializeCombatOgre::SceneManager *ogrSceneMgr)
   {
        mSceneMgr = ogrSceneMgr;
        mSceneMgr->setFog(FOG_EXP, mSettings->density, etc...
..
   }
}

Where ManagedCombatSession is a C++ managed class that can call both ogre methods as well as managed ones. I chose to create my class as a static class, although a singleton class would be preferrable.

Any place you need to make a managed call, use the managed manager equivalent. As alphabeta mentioned, this may have a significant performance impact(gotta test that), but for me, it is well worth it. If your goal is to use C# for everything, obviously you should go with Axiom. Hope this helps :P
Whitebear
Greenskin
Posts: 147
Joined: Mon Oct 25, 2004 5:22 am
Location: RF
x 1

Post by Whitebear »

limbo,

IMHO, it's better keep things as simple as possible. Maybe you should re-write your C# code in C++ and avoid headache? :wink:
\_ :roll:_/
Post Reply