我有一个C++/CLI方法,ManagedMethod
,与一个输出参数,将通过一个本地方法这样进行修改:的n
值已被修改,我期望C++/CLI方法调用本地方法修改int - 需要pin_ptr?
// file: test.cpp
#pragma unmanaged
void NativeMethod(int& n)
{
n = 123;
}
#pragma managed
void ManagedMethod([System::Runtime::InteropServices::Out] int% n)
{
pin_ptr<int> pinned = &n;
NativeMethod(*pinned);
}
void main()
{
int n = 0;
ManagedMethod(n);
// n is now modified
}
一旦ManagedMethod
回报。到目前为止,我已经能够编译的唯一方法是在ManagedMethod
中使用pin_ptr
,所以实际上只有这样做才是正确的方法?还是有一种更优雅的方式将n
传递给NativeMethod
?