【发布时间】:2015-02-18 23:46:31
【问题描述】:
如何取消引用 void* 在 C++/CLI 中指向的值,特别是,我想将其分配给 int。
int Callback(void* returnValue)
{
int lookUpValue = *returnValue; // How to do this??
if(lookUpValue==1)
DoSomething();
if(lookUpValue==2)
DoSomethingElse();
return CALLBACK_SUCCESS; // Defined elsewhere.
}
我试过了:
{
GCHandle h = GCHandle::FromIntPtr(IntPtr(voidPtr));
Object^ result = h.Target;
lookUpValue = (int)result;
h.Free();
}
来自示例: CLI/C++: void* to System::Object
但发现在某些情况下 h.Target 未定义,我会崩溃。所以不要认为我已经能够正确地做到这一点。
提前致谢。
【问题讨论】: