How to call a C# function in C++?
-
- Kobold
- Posts: 27
- Joined: Fri Dec 10, 2004 7:08 am
How to call a C# function in C++?
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!!!!!!!!!!!!!
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!!!!!!!!!!!!!
- Nek
- Gnoblar
- Posts: 21
- Joined: Mon Aug 18, 2003 6:06 pm
- Location: Houston, Tx
- Contact:
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=
Check out my comments from this post.
http://www.ogre3d.org/phpBB2/viewtopic. ... highlight=
- Nek
- Gnoblar
- Posts: 21
- Joined: Mon Aug 18, 2003 6:06 pm
- Location: Houston, Tx
- Contact:
-
- Kobold
- Posts: 34
- Joined: Mon Jan 03, 2005 5:07 am
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.
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.
- haffax
- OGRE Retired Moderator
- Posts: 4823
- Joined: Fri Jun 18, 2004 1:40 pm
- Location: Berlin, Germany
- x 7
- Contact:
-
- Kobold
- Posts: 34
- Joined: Mon Jan 03, 2005 5:07 am
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.
-
- Kobold
- Posts: 27
- Joined: Fri Dec 10, 2004 7:08 am
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.
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.
-
- Kobold
- Posts: 27
- Joined: Fri Dec 10, 2004 7:08 am
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..........................
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..........................
-
- Kobold
- Posts: 34
- Joined: Mon Jan 03, 2005 5:07 am
Trust me, you don't want to go about making a .net ogre wrapper
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
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
- Nek
- Gnoblar
- Posts: 21
- Joined: Mon Aug 18, 2003 6:06 pm
- Location: Houston, Tx
- Contact:
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,
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
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...
..
}
}
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