I’ve got problems passing a member function of a C++ CLI class to a native C callback from a library.
To be precise its the Teamspeak 3 SDK.
You can pass a non member function using the following code without problem:
struct ClientUIFunctions funcs;
/* Initialize all callbacks with NULL */
memset(&funcs, 0, sizeof(struct ClientUIFunctions));
funcs.onConnectStatusChangeEvent = onConnectStatusChangeEvent;
But I need to pass a pointer to a member function, for example:
funcs.onConnectStatusChangeEvent = &MyClass::onConnectStatusChangeEvent;
Any other idea how to use the event within a non static member function is welcome to.
Thanks in advance!
This can only be done via a static class function because C doesn’t know anything about the vtable or what object the function is part of. See below for a C++ and Managed C++ example
This could however be a work around, build a wrapper class which handles all the callbacks you need.
Another possibility would be if the callback supports and
void *argslikevoid (*onConnectStatusChangeEvent)(void *args);which you can set from the plugin. You could set the object in this args space so in de sOnConnectStatusChangeEvent you would have something like this:For managed C++ it should be something like this, however I can’t get it to compile because it doesn’t like the template brackets..
wrapper.h:
wrapper.cpp: