Garmaine Staff asked 1 year ago

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?