The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

MichaeB (1) [Avatar] Offline
#1
I am reading CLI/C++ in action trying to find a guidance to the following problem:
I have C++ code that has to use external assembly. This assembly is a component and it has one event delegate. Let's say this component has following code:

namespace MyNamespace.MyModel
{
public partial class ModelGUI: UserControl
{
public delegate void TestDelegate(int intval, String strval);
public ModelGUI() {...}

public event TestDelegate OnTestDelegate;
...
}

I need to be able to handle events raised from ModelGUI in my C++ class.
My C++ class cannot import the assembly, so it loads it dynamicaly and uses Reflection to get to the methods. Here is what I do:

System::Windows::Forms::Control ^ control; // points to the instance of ModelGUI
System::Type^ type = control->GetType();
System::Reflection::EventInfo^ eInfo = type->GetEvent("OnTestDelegate"smilie;

Here is where my problems start:
eInfo->AddEventHandler(control, <eventhandler>smilie;

The problem is that I cannot figure out how to propertly define event handler in my class and use it to add event handler. Note that I cannot directly import the assembly so MyNamespace.MyModel is not visible to my C++ code.