0

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?

  • This funcion is not static. If it would be static, you need a native wrapper, that can be used as a pointer. If you want a member function to an object to be used the definition is wrong. – xMRi Jan 12 '17 at 15:41
  • I've made my Error function as static, but I have a C3374 error saying "C3374: can't take address of ''Renderer::StreamingContext::Error' unless creating delegate instance" –  Jan 12 '17 at 16:02

0 Answers0