1

I am designing a class in C++/CLR that uses a callback function provided by user code.

This works very nicely if the callback function is free ( i.e. not the member of a class ). It is almost the same as in pure C++.

Here is some sample code that works well:

ref class ClassThatUsesCallback
{
public:
    typedef void (*callback_t)( String^ );
    void setCallback( callback_t pfun )
    {
        myCallback = pfun;
    }
    void Run()
    {
        if( myCallback != nullptr ) {
            myCallback("This is a test");
        }
    }
private:
    callback_t myCallback;
};

void FreeFunction( String^ s )
{
    Console::WriteLine( "Free Function Callback " + s );
}

int main(array<System::String ^> ^args)
{
   ClassThatUsesCallback^ theClassThatUsesCallback
       = gcnew ClassThatUsesCallback();
    theClassThatUsesCallback->setCallback( FreeFunction );
    theClassThatUsesCallback->Run();
}

However, I would like the callbacked function to be a member of a class in the user code ( so it can make use of and change the attributes of the user code class )

The following code does not compile

ref class ClassThatProvidesCallback
{
public:
    void MemberFunction( String^ s )
    {
        Console::WriteLine( "Member Function Callback " + s );
    }
    void Run()
    {
        ClassThatUsesCallback^ theClassThatUsesCallback
            = gcnew ClassThatUsesCallback();
        theClassThatUsesCallback->setCallback( 
            &ClassThatProvidesCallback::MemberFunction );
        theClassThatUsesCallback->Run();
    }

};

I get this error

error C3374: can't take address of 'ClassThatProvidesCallback::MemberFunction'
unless creating delegate instance

When I research this, I find numerous explanations of how to call un-managed code from managed code ( and vice-versa ) I do not need to do this - all the code involved is managed. So I am hoping that someone can point me to a simple way to this.

2
  • Use the event keyword instead. Commented Mar 10, 2014 at 15:43
  • Of course, it is better to use standard events, which have the same functionality as callback and allow to have several subscribers. See EventHandler<TEventArgs> delegate. Commented Mar 10, 2014 at 15:58

1 Answer 1

1

This is full solution:

ref class ClassThatUsesCallback
{
public:
    void setCallback( Action<String^>^ callback )
    {
        myCallback = callback;
    }
    void Run()
    {
        if( myCallback != nullptr ) {
            myCallback("This is a test");
        }
    }
private:
    Action<String^>^ myCallback;
};

ref class ClassThatProvidesCallback
{
public:
    void MemberFunction( String^ s )
    {
        Console::WriteLine( "Member Function Callback " + s );
    }
    void Run()
    {
        ClassThatUsesCallback^ theClassThatUsesCallback
            = gcnew ClassThatUsesCallback();
        theClassThatUsesCallback->setCallback(gcnew Action<String^>(this,
            &ClassThatProvidesCallback::MemberFunction));
        theClassThatUsesCallback->Run();
    }

};

int main(array<System::String ^> ^args)
{
    ClassThatProvidesCallback^ c = gcnew ClassThatProvidesCallback();
    c->Run();

    return 0;
}

Native C++ style typedef is replaced with .NET Action delegate. Additional parameter this is added to setCallback call, it is required to define the class instance which contains the callback function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.