I need to use the unmanaged API from C ++ / CLI. This API stores a void pointer to arbitrary user data and multiple callbacks. Then it calls these callbacks, passing the user data as void *.
So far, I had my own class, passing its "this" pointer as user data and using this pointer to call the API in this class, that is:
static void __stdcall Callback(void* userData) {
((MyType*)userData)->Method();
}
class MyType {
public:
MyType() { RegisterWithApi((void*)this, Callback); }
void Method();
};
I am trying to translate this using a managed class. I found that the gcroot type can be used to safely store a managed link in its own code, so now, as I do it now:
static void __stdcall Callback(void* userData) {
(*(gcroot<MyType^>*)userData)->Method();
}
ref class MyType {
gcroot<MyType^>* m_self;
public:
MyType() {
m_self = new gcroot<MyType^>;
RegisterWithApi((void*)m_self, Callback);
}
~MyType() { delete m_self; }
void Method();
}
++/CLI, . , , gcroot - , GC. , * ? ?
.