Unmanaged application recieving callback from a c# delegate

  • Thread starter Thread starter sergio.cs
  • Start date Start date
S

sergio.cs

Guest
Disclaimer: There are several questions similar to this, but all refer to the opposite procedure (receiving native callbacks as delegates). Im looking for the opposite (receiving events/delegates in native code).

I have a native, unmanaged C++ application, which uses a C# library by wrapping it with a C++/CLI class wrapper (Using something like : http://pragmateek.com/using-c-from-native-c-with-the-help-of-ccli-v2/).

The C# library raises some events, which I can capture in C# code and handle them.

I want to send those events to an unmanaged C++ class, as a callback. Which would be the way to go for this?

All examples out there, show how callbacks from a Cpp DLL can be captured as delegates. I want the opposite, I need to send events captured in C# to a native application, as a non-static function pointer of an instantiated class.

Edit:

I have managed to make a two way C++/CLI wrapper, it is not ideal but I can now receive C#.NET callbacks and handle them in C++ Native code. Heres an overview of how this works:

618723


In order to call C# code from native C++, see the pipeline formed by the following classes:

  • NativeClass
  • NativeToNetWrapper
  • ExposedNetPrivate
  • ManagedClass

The other way around (managed event handled in native code):

  • ManagedClass event
  • NetToNative::callbackMethod
  • UnmanagedHandler::handleEvent
  • NativeClass::handleEvent



In my case, the ManagedClass generated some events, which I wanted to receive from the NativeClass instance. In order to do this, I attach a managed C++/CLI class to the C# delegate (ManagedClass.delegate in the image). This managed class is NetToNativeWrapper, which also holds a reference to my NativeClass.

I use a single entry point to handle all events, for both my NativeClass and the NetToNativeWrapper to know where to find it, I created the UnmanagedHandler interface, which is a header only class defining the handleEvent method.

How to attach the C# delegate to the managed C++/CLI class:

// Within NativeToNetWrapper
// Get the delegate from C#.Net and attach a new delegate
// pointing to the C++/CLI managed wrapper

Delegate ^ del = privateClass->netObject->delegate;
del += gcnew Delegate(privateClass->callbackObject.get(), &NetToNativeWrapper::callbackMethod);


Then, in the NetToNativeWrapper, just call the UnmanagedHandlers handleEvent function:

void callbackMethod()
{
EventParameters ep;
// fill ep as needed
m_handler->handleEvent(ep);
}


Afterwards, within pure C++ code, just implement handleEvent in order to parse and execute any code necessary that handles the raised event.

I would like though to be able to pass any function pointer, and listen to any type of event. This would require me to bridge managed delegates and function pointers. However, I have only found GetDelegateForFunctionPointer, which only works for static functions. I would need to set the object instance and the function pointer both.

Any feedback is welcome!

Continue reading...
 
Back
Top