Mario Sanz Gómez
Mario Sanz Gómez

Reputation: 49

Passing unmanaged method as callback to managed C++/CLI class

I want to pass as callback a C++ member function to a C# project. I have other project in C++/CLI and I want to do it through it.

So, in unmanaged C++ of my C++/CLI project I have a function object:std::function<void(int)>callback;

This function is coming from my C++ project and it works fine, I save it there as example to avoid the previous step. Now, I would like to pass this callback function to my C# project. For this, I create a method in unmanaged C++, pass it to managed C++ and from this point pass it finally to C#. I'd like something like this:

// Unmanaged class
public class Wrapper
{
public:
    std::function<void(int)>callback;

    void ReturnToCallback(int data)
    {
        callback(data);
    }

    void PassCallback()
    {
        NETWrapper netWrapper;
        netWrapper.TestCallback(ReturnToCallback);
    }
};

//Managed class
public ref class NETWrapper
{
public:
    void TestCallback(Action<int>^ callback)
    {
       StartGenerator^ startGen = gcnew StartGenerator(callback);
    }
};

// C# 
public class StartGenerator
{
    private Communication comm;

    public StartGenerator(Action<int> callback)
    {
        comm = Communication.Instance;
        comm.callback = callback;
    }
}

This solution, of course, gives me back an error when compiling:

Error 3 error C3867: 'IfaceXXX::Wrapper::ReturnToCallback': function call missing argument list; use '&IfaceXXX::Wrapper::ReturnToCallback' to create a pointer to member d:\XXX.h

I have tried other ways such as Get the delegate for the function pointer so I can work on Managed C++ and pass it to C# but I am not able to implement it right. What do you think is the best way to try this?

Upvotes: 1

Views: 1126

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283941

  1. Make Wrapper::callback a pointer to the std::function.
  2. Change Wrapper to a ref class.

That's it.

public ref class Wrapper
{
public:
    std::function<void(int)>* callback;

    void ReturnToCallback(int data)
    {
        (*callback)(data);
    }

    void PassCallback()
    {
        NETWrapper netWrapper;
        netWrapper.TestCallback(gcnew Action<int>(this, &Wrapper::ReturnToCallback));
    }
};

You do then need to manage the lifetime of the std::function now, perhaps my clr_scoped_ptr could show you how to do that.

Upvotes: 2

Related Questions