Wrapper & NativePtr


29-11-2010 16:59:46

I am currently working on a wrapper for an Ogre plugin using c++/cli and Ogre/Mogre 1.7.1. It all works fine except that I don't understand, how to get a Ogre::SceneManager* object from a Mogre::SceneManager^ handle.
I figured I have to use the NativePtr property but I can't get it to work:

// A simple method for testing purposes
// This code is inside the c++/cli wrapper project and gets called from c#
// I receives a handle to a SceneManager, get the corresponding native c++ pointer and returns the name of this SceneManager
String^ ParticleSystemManager::GetSceneManagerName(Mogre::SceneManager^ sceneManager)
Ogre::SceneManager* scene = dynamic_cast<Ogre::SceneManager*>(sceneManager->NativePtr);
return marshal_as<String^>(scene->getName());

The code compiles fine and I don't get any runtime exception from dynamic_cast.
The problem is, that after the dynamic_cast all the values in the native SceneManager (Ogre::SceneManager* scene) object are messed up, it looks like it was just filled with random memory, which finally will cause the marshal_as line to throw an exception (it works if I pass a hardcoded std::string instead of scene->getName()).

I am not sure what is wrong or if I misunderstood the purpose of NativePtr, any help is welcome.


30-11-2010 02:48:35

we can find some code in src:

String^ SceneManager::Name::get()
return ( CLR_NULL == _name ) ? (_name = TO_CLR_STRING( static_cast<const Ogre::SceneManager*>(_native)->getName( ) )) : _name;
#define TO_CLR_STRING(ogrestr) gcnew System::String((ogrestr).c_str())

That's all. :D