So I am trying to write my first program using P/Invoke to create a wrapper around a c++ library to allow use in a c# application. One of the features of the library is it takes a callback function (delegate), with the signature void callback_func(string log, int time)
.
I am trying to determine how I can wrap this so that the C# library can process std::strings handed to this function by the c++ library.
I tried look at this Passing strings from C# to C++ DLL and back -- minimal example, however I couldn't determine how this would apply in the this context, where a delegate is being provided to the library.
The delegate function is as follows:
C# App
public delegate void log_delegate(string log, int time);
static void logFunc(string log, int time)
{
Console.WriteLine($"{log} {time}");
}
And it is passed to the C++ library as a argument in the constructor of an object Foo, which has member functions that will call this function
C# app
log_delegate my_del = logFunc;
IntPtr foo = Foo_Create(6, 6, my_del);
The C++ library currently calls the callback function, handing in relevant logging information as a string in the first parameter.
Currently, I just get garbage values back however, (i.e. " ►>üv⌂☻")