Reputation: 23479
So I have a C# class that has the following event:
public class CSClient
{
public delegate void OnMessageHandler(Object sender, EventArgs e);
public event OnMessageHandler OnOptionsEvent;
}
Then I have a C++/CLI class, for which I want to subscribe to OnOptionsEvent.
I have tried something like this:
void CSClientWrapper::Start()
{
GCHandle h = GCHandle::FromIntPtr(IntPtr(_impl));
CSClient^ obj = safe_cast<CSClient^>(h.Target);
__hook(&CSClient::OnOptionsEvent, obj, &CSClientWrapper::OnOptions);
}
void CSClientWrapper::OnOptions(Object^ sender, EventArgs^ args)
{
}
error C2039: 'add_OnOptionsEvent' : is not a member of 'CSClient'
error C2750: 'CSClient::OnMessageHandler' : cannot use 'new' on the reference type; use 'gcnew' instead
I am completely new to C++CLI, so I suspect it is something really fundamental that I'm missing.
Upvotes: 1
Views: 997
Reputation: 941317
Yes, that's not appropriate syntax. Best to forget that the __hook keyword exists, it was a fairly mistaken idea to add event handling syntax to native C++. You need to create a managed delegate to subscribe the event, correct syntax should be close to:
CSClient^ obj = safe_cast<CSClient^>(h.Target);
obj->OnOptionsEvent +=
gcnew CSClient::OnMessageHandler(this, &CSClientWrapper::OnOptions);
Upvotes: 3