问题
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++?
回答1:
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)
来源:https://stackoverflow.com/questions/26547215/how-to-pass-c-sharp-method-as-a-callback-to-cli-c-function