.net 封送委托到std::函数

.net 封送委托到std::函数,.net,c++-cli,.net,C++ Cli,这能做到吗 可以使用以下代码编组到C样式函数指针: GCHandle rahHandlle = GCHandle::Alloc(reAuthHandler); IntPtr rahPtr = Marshal::GetFunctionPointerForDelegate(reAuthHandler); auto rahUnman = static_cast<SomeFuncPtr>(rahPtr.ToPointer()); GCHandle-rahandle=GCHandle::Al

这能做到吗

可以使用以下代码编组到C样式函数指针:

GCHandle rahHandlle = GCHandle::Alloc(reAuthHandler);
IntPtr rahPtr = Marshal::GetFunctionPointerForDelegate(reAuthHandler);
auto rahUnman = static_cast<SomeFuncPtr>(rahPtr.ToPointer());
GCHandle-rahandle=GCHandle::Alloc(reAuthHandler);
IntPtr rahPtr=Marshal::GetFunctionPointerForDelegate(reAuthHandler);
auto rahUnman=static_cast(rahPtr.ToPointer());

但是如何将一个封送到std::函数?如果可以的话。

在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++CLI
IntPtr rahPtr=Marshal::GetFunctionPointerForDelegate(reAuthHandler);
//声明一个匹配您的本机C++ STD::
typedef void(u stdcall*thismycppfunctiondeclaration)(长);
//然后将委托指针转换为C++函数指针
autocallbackfunctioncpp=static_cast(rahPtr.ToPointer());
//然后最后调用您的本地C++方法,该方法采用STD::
DoWork(callbackFunctionCpp);
-

//C++本机
voidDoWork(std::函数回调){
//做点什么
回调(123);
}

非常好用!谢谢!
//C++ Native
void DoWork(std::function<void(long theLongValue)> callback){
    //Do something
    callback(123);
}