在您的C++/CLI .NET代码中声明一个函数指针,它与您的本地std :: function中的参数匹配 - 并将.NET代理的指针转换为此代码。
//C++ CLI
IntPtr rahPtr = Marshal::GetFunctionPointerForDelegate(reAuthHandler);
//Declare a function pointer which matches the parameters of your native C++ std::function
typedef void(__stdcall * ThisIsMyCppFunctionDeclaration)(long);
//Then cast your delegate pointer to the c++ function pointer (which
auto callbackFunctionCpp = static_cast<ThisIsMyCppFunctionDeclaration>(rahPtr.ToPointer());
//And then finally invoke your native C++ method which takes a std::function
DoWork(callbackFunctionCpp);
-
//C++ Native
void DoWork(std::function<void(long theLongValue)> callback){
//Do something
callback(123);
}