I'm writing a native wrapper around a managed component written in C++\CLI.
I have the following function in managed code:
array<Byte>^ Class::Function();
I want to expose this function from a native C++ class with the following signature:
shared_array<unsigned char> Class::Function();
I've gotten as far as calling the managed function from native code, but I'm not sure how to safely copy the managed array into an unmanaged one.
gcroot<cli::array<System::Byte>^> managedArray = _managedObject->Function();
Take a look at
pin_ptr
, it lets you pass address of a managed class to an unmanaged function.There are two usual approaches:
Perform the marshaling with native code, which requires use of
pin_ptr<>
:Perform the marshaling with managed code, which requires use of the Marshal class:
Generally I would prefer the latter approach, as the former can hinder the GC's effectiveness if the array is large.