C++/CLI invoke function from C# Dll with a callback function as the parameter

Viewed 146

I manage to call C# dll from my c++ code as bellow,

#include "stdafx.h"
#include <string>
#include <iostream>


#using "../Release/ClassLibrary1.dll"

using namespace ClassLibrary1;
using namespace System;


int main()
{
    int sum, x, y;
    x = 100000;
    y = 22;
    Class1 ^a = gcnew Class1();
    sum = a->demoAdd(x, y);
    sum = x + y;
    printf("%d", sum);

}

int ttt(std::string &str1) {

}

And this is the C# Dll,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class Class1
    {
        public int demoAdd(int x, int y)
        {
            int sum;
            sum = x + y;
            return sum;
        }

        public bool RunTheMethod(Func<string, int> myMethodName)
        {
            //... do stuff
            int i = myMethodName("My String");
            //... do more stuff
            return true;
        }

    }
}

The demoAdd works like a charm, however, I don't know if it is possible to call RunTheMethod which requires a function as parameter from C++ side.

I tried as below with no luck,

int main()
{
    int sum, x, y;
    x = 100000;
    y = 22;
    Class1 ^a = gcnew Class1();
    sum = a->demoAdd(x, y);
    sum = x + y;
    printf("%d", sum);


    a->RunTheMethod(&ttt);

    return 0;
}

int ttt(std::string &str1) {

}

I'm a newbie to c++, is that possible to make that call happen?

Any help is appreciated!

enter image description here

0 Answers
Related