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

别说谁变了你拦得住时间么 提交于 2019-12-10 18:15:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!