How to correctly convert a CLR type, say Foo^, to void*and back after a while?
The script has some unmanaged code in a DLL that can be summarized as
class Handler {
void* _obj;
void (*_call)(void* obj, int detail);
void handle_event() { _call(_obj, 1234); }
public:
void set_object(void* obj) { _obj = obj; }
void set_callback(void(*callback)(void*,int)) { _call = callback; }
};
I want to save a CLR object in a field Handler _obj. How to implement it, taking into account that the GC can move the CLR object? ( pin_ptr? gcroot?)
static void event_callback(void* obj, int detail) {
Foo^ clr_obj = undo_magic(obj);
clr_obj->DoStuff(detail);
}
public ref class Foo {
Handle* h;
public:
void Start() {
h = new Handler;
void* obj = do_magic(this);
h->set_object(obj);
h->set_callback(event_callback);
}
...
}
source
share