2015-05-29 65 views
1

这可以做到吗?元帅代表std :: function

编组到C风格的函数指针可以用下面的代码来完成:

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

但是,如何将一个元帅到一个std ::功能?如果可以做到的话。

回答

0

在您的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); 
}