I have a c++ class that triggers some method like an event.
class Blah {
virtual void Event(EventArgs e);
}
How can I wrap it so whenever the method is called a C# (managed) event will be called?
I thought of inheriting that class and overloading the event method, and then somehow call the managed event. I'm just not sure how to actually do it.
Something like this (now compile-tested):
#include <vcclr.h>
struct blah_args
{
int x, y;
};
struct blah
{
virtual void Event(const blah_args& e) = 0;
};
public ref class BlahEventArgs : public System::EventArgs
{
public:
int x, y;
};
public ref class BlahDotNet
{
public:
event System::EventHandler<BlahEventArgs^>^ ItHappened;
internal:
void RaiseItHappened(BlahEventArgs^ e) { ItHappened(this, e); }
};
class blah_event_forwarder : public blah
{
gcroot<BlahDotNet^> m_managed;
public:
blah_event_forwarder(BlahDotNet^ managed) : m_managed(managed) {}
protected:
virtual void Event(const blah_args& e)
{
BlahEventArgs^ e2 = gcnew BlahEventArgs();
e2->x = e.x;
e2->y = e.y;
m_managed->RaiseItHappened(e2);
}
};