Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

marshal boost::function into C++/CLI

This topic is 3760 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a nice C++ class which I need to use from C#. My C++ use case is to make a new instance, call Instance.Connect(std::string), and call Instance.Register(boost::function<void (int,int)>). Current approach is to write a managed wrapper. I figured out how to marshal System::String^ to std::string, but I can't figure out how to marshal a delegate into a boost.function object. (This can be done in the wrapping layer, I presume that will be the only way) any ideas?

Share this post


Link to post
Share on other sites
Advertisement

//C# land
instance.RegisterResize(new ManagedSS.Resize_callback(this, matching_method);

//C++/CLI land
public ref class ManagedSS { public:
delegate void Resize_callback(int, int);
void RegisterResize(Resize_callback^ callback);
};

void ManagedSS::RegisterResize(Resize_callback^ callback)
{
boost::function<void (int, int)> functor = callback;
ss->RegisterResize(functor);
}

boost/function/function_base.hpp(404) : error C3185: 'typeid' : used on managed type 'Managed::ManagedSS::Resize_callback ^', use 'Managed::ManagedSS::Resize_callback ^::typeid' instead





[Edited by - thedustbustr on July 7, 2008 11:06:00 AM]

Share this post


Link to post
Share on other sites
Hi,

It should work with Marshal::GetFunctionPointerFromDelegate (in System::Runtime::InteropServices), like:

typedef void (*NativeResizeCallback)(int, int);

void ManagedSnapstream::RegisterResize(Resize_callback^ callback)
{
IntPtr ptr = System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(callback);
NativeResizeCallback nativeCB = reinterpret_cast<NativeResizeCallback>(ptr.ToPointer());
boost::function<void (int, int)> functor = nativeCB;
snapstream->RegisterResize(functor);

// DANGER: as soon as the mananged delegate gets GC'ed the native function pointer
// won't be valid anymore, so you should register the managed callback somewhere, too.
}





Beware of the problem stated in the text (if the delegate gets GC'ed you're doomed *g*)

Regards,
Andre

Share this post


Link to post
Share on other sites
I don't have to pin the managed delegate objects anywhere? (as I am taking their pointers in c++/cli land)

[Edited by - thedustbustr on July 2, 2008 9:27:44 AM]

Share this post


Link to post
Share on other sites
No, as long as you keep the delegate alive (ie. prevent it from being garbage collected), the pointer should remain valid.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!