I have a .NET library which mixes C++ and C++/CLI code.
In this library, I have a managed class written in C++/CLI as follow:
namespace Renderer {
public ref class StreamingContext
{
public:
void Error() {
}
}
And an unmanaged class written in classic C++ as follow:
namespace Renderer {
public class TimeoutHandler
{
private:
int _secondsDelay;
timespec _lastTime;
public:
typedef void (TimeoutHandler::*error)();
static error ErrorFunc;
TimeoutHandler(error error, int secondsDelay) {
ErrorFunc = error;
_secondsDelay = secondsDelay;
}
void Reset() {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
_lastTime = ts;
}
bool IsTimeout() {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
return _lastTime.tv_sec + _secondsDelay > ts.tv_sec;
}
static int check_interrupt(void * t) {
TimeoutHandler* ref = static_cast<TimeoutHandler *>(t);
bool isTimeout = ref->IsTimeout();
if (isTimeout) {
(ref->*ErrorFunc)();
}
return t && isTimeout;
}
};
}
So, I'd like to call the Error method from an instance of unmanaged TimeoutHandler class (inside check_interrupt function).
I have passed my Error method address as follow:
Renderer::TimeoutHandler* th = new Renderer::TimeoutHandler(&Renderer::StreamingContext::Error, 5);
I'm having an issue because StreamingContext is a managed class, so I can't literally pass an address of a member of StreamingContext to my unmanaged class TimeoutHandler.
How can I get a reference of a managed class method and call it within an unmanaged context?