Jump to content
  • Advertisement
Sign in to follow this  

[.net] Converting C++ callbacks to C# events and delegates

This topic is 4155 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

Hi Guys, Hope you can help me witha rather challenging problem. I am writing a CLR complient C++ wrapper around some unmanaged C++ objects. I have constructed a ref class which holds all of the methods I am exposing to .net languages which will provide various functions that allow me to manipulate the unmanaged objects from managed code. The problem I am having at the moment is that the unmanaged object I am using uses a function pointer as a callback mechanism. This in turn then invokes other function pointers to carry out various tasks. My code looks like the following: #pragma once #include "mpgmux.hpp" #include "bufstrm.hpp" #include "bufstrm.h" #include "mpgvout.hpp" #include "mpg_dlg.hpp" #include "stdio.h" #include <msclr\auto_gcroot.h> using namespace System::Runtime; using namespace System::Runtime::InteropServices; using namespace System; namespace Mainconcept_SDK { #pragma managed // Various methods which are invoked by the callback mechanism void Progress_printf(int percent, char *fmt) { } void Error_printf(char *fmt) { // do something with the error message received in the char array... }; void *get_rc(char* name) { if (!strcmp(name , "malloc")) { return malloc; } else if (!strcmp(name , "free")) { return free; } else if (!strcmp(name,"err_printf")) { return Error_printf; } else if (!strcmp(name,"prg_printf")) { return Progress_printf; } else if (!strcmp(name,"wrn_printf")) { //return OutputDebug_printf; } else if (!strcmp(name,"inf_printf")) { //return OutputDebug_printf; } return NULL; }; public ref class MultiPlexor { // Create an mpegmuxer_tt object pointer mpegmuxer_tt* mpegmuxer_object; // create a bufstream pointer bufstream_tt* fileStream; public: MultiPlexor(bool UseDefaultSettings, String^ OutputFileName) { mpeg_m_settings mpegmuxer_settings; if (UseDefaultSettings) { mpegOutMuxDefaults(&mpegmuxer_settings, MPEG_MPEG2); } // create the multiplexor object - this is where the function pointer to the get_rc method is passed in mpegmuxer_object = mpegOutMuxNew(get_rc, &mpegmuxer_settings); }; } As you can see, the actual methods which are called by the various function pointers seem to have to sit outside of my ref class - I've tried moving them in, but get errors that i have not been able to resolve. Because of this I cannot find a good way of pushing this information through the ref class as an event so I can receive it from .net languages such as C#. Ultimately, I am looking for a way to somehow get these callback methods to raise events from within the ref class so when I create a new instance of the class in a language such as C#, I will be able to attach handlers to the events also defined within this object. Could someone show me how to accomplish this? It has been baffling me for several days. As you can probably tell, I'm relatively new to C++ and the CLR, I'm using mainly as a means of accessing unmanaged code from C#. Many thanks for any help you might be able to provide, Damien.

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!