简单地将C ++ / CLI int ^转换为非托管int *

魔鬼

这是如此基础,应该很容易找到。在搜索中,我得到的只是更复杂的解决方案。转换字符串,封送处理,固定对象。您如何简单地从c ++ / CLI int ^指针转换为C ++ / CLI中的本机int *。

我的身体是

void Open(int ^Hndl)
{
    void Unmanaged_Open(Hndl); // How do you pass the pointer to this
}

其中void Unmanaged_Open(int * handle);

本·沃格特

这是在C ++ / CLI中实现输出参数的方式,例如C#的void func(out int x)请注意,没有int^

void Open([OutAttribute] int% retval)
{
    int result;
    if (!UnmanagedOpen(&result))
         throw gcnew Exception("Open failed!");
    retval = result;
}

注意,简单地返回值可能会更好。当使用返回值进行错误检查时,Out参数大多数出现在本机函数中。您可以使用.NET中的异常进行错误检查,如下所示:

int Open()
{
    int result;
    if (!UnmanagedOpen(&result))
         throw gcnew Exception("Open failed!");
    return result;
}

或者如果预期会失败(例如,不受信任的输入),请实现TryXYZ模式(在MSDN上进行描述):

bool TryOpen([OutAttribute] int% retval)
{
    retval = 0;
    int result;
    if (!UnmanagedOpen(&result)) return false;
    retval = result;
    return true;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章