如何将从非托管代码检索的System :: IntPtr转换为C ++ / CLI中定义的委托类型?

时间:2012-11-02 05:22:21

标签: interop c++-cli marshalling void-pointers intptr

目前我正在使用两个图层。一层是C ++ / CLI,另一层是非托管的C ++图形库,我没有源代码。当用户在非托管代码中移动操纵器时,我将数据传递给非托管代码并在回调函数'intermediateCallback'中接收数据。

下面是我如何定义名为'tempFunc'的委托类型。它没有任何东西,也没有任何回报。 'intermediateCallback'的'void * userData'参数是我想要转回委托实例的内容。此参数作为转换为IntPtr转换为void *的委托传递给unamanged代码。我可以将void *转回IntPtr,但是如何将其转换为委托类型'tempFunc'?

delegate void tempFunc();

//the function that the delegate instance actually points to
void libCoin3D::CenterballDragger::memberCallback()
{
....
}

//the function that the unmanaged code will call and pass data to
void intermediateCallback( void * userData, SoDragger *dragger)
{
    System::IntPtr ptr=(System::IntPtr)userData;
    tempFunc^ tF=(tempFunc^)ptr;//this line does not work, I want to do something like it
}

最初,当我将代理传递给非托管代码时,这就是我将代理转换为void *的方式。

如果tF是由以下人员定义的代表:

tempFunc^ tF=gcnew tempFunc(this,&libCoin3D::CenterballDragger::memberCallback);

我将其转换为:

(System::Void*)(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(tF))

那么可以将void *转换为tempFunc吗?

1 个答案:

答案 0 :(得分:3)

我解决了类似的问题:

delegate void tempFunc();

//the function that the delegate instance actually points to
void libCoin3D::CenterballDragger::memberCallback()
{
  ....
}

//the function that the unmanaged code will call and pass data to
void __stdcall intermediateCallback( void * userData, SoDragger *dragger)
{
  using System::Runtime::InteropServices::GCHandle;
  GCHandle gch = GCHandle::FromIntPtr((System::IntPtr)userData);
  tempFunc^ tF = (tempFunc^)gch.Target;

  tF();

  gch.Free(); // If the call only once, otherwise it is necessary to keep the pointer and delete it later     

}

tempFunc^ tF = gcnew tempFunc(this, &libCoin3D::CenterballDragger::memberCallback);

 // pass to unmanaged code
setCallback( &intermediateCallback, (void*)GCHandle::ToIntPtr(GCHandle::Alloc(tF)) );