I have a C++/CLI method, ManagedMethod
, with one output argument that will be modified by a native method as such:
// 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
}
Once ManagedMethod
returns, the value of n
has been modified as I would expect. So far, the only way I've been able to get this to compile is to use a pin_ptr
inside ManagedMethod
, so is pinning in fact the correct/only way to do this? Or is there a more elegant way of passing n
to NativeMethod
?