Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass C# method as a callback to CLI/C++ function?

I have such method in C++/CLI:

void Foo(OnEngineCloseCallback callback);

with such callback definition:

typedef void (*OnEngineCloseCallback)( int, String ^ errorMessage);

The C++/CLI compiles. The C# code looks like this:

static void onCallback( int code, String errorMessage)
{
    System.Diagnostics.Debug.WriteLine(errorMessage);
}

and the call:

Foo(onCallback); // error

"Foo is not supported by the language" (error: CS0570).

So how can I pass my callback to CLI/C++?

like image 597
greenoldman Avatar asked May 28 '26 04:05

greenoldman


1 Answers

If neither of you is going to post an answer I'll do it because I don't like answered questions without answers...

You must declare a public delegate, not a function pointer. Basic how-to article is here. by Hans Passant

How to: Define and Use Delegates (C++/CLI)

like image 162
t3chb0t Avatar answered May 30 '26 19:05

t3chb0t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!