Nathanael
Nathanael

Reputation: 1772

How do I convert a cli::array to a native array from native code?

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();

Upvotes: 4

Views: 8654

Answers (2)

ildjarn
ildjarn

Reputation: 63005

There are two usual approaches:

  1. Perform the marshaling with native code, which requires use of pin_ptr<>:

     boost::shared_array<unsigned char> convert(array<unsigned char>^ arr)
     {
         boost::shared_array<unsigned char> dest(new unsigned char[arr->Length]);
         pin_ptr<unsigned char> pinned = &arr[0];
         unsigned char* src = pinned;
         std::copy(src, src + arr->Length, dest.get());
         return dest;
     }
    
  2. Perform the marshaling with managed code, which requires use of the Marshal class:

     boost::shared_array<unsigned char> convert(array<unsigned char>^ arr)
     {
         using System::Runtime::InteropServices::Marshal;
    
         boost::shared_array<unsigned char> dest(new unsigned char[arr->Length]);
         Marshal::Copy(arr, 0, IntPtr(dest.get()), arr->Length);
         return dest;
     }
    

Generally I would prefer the latter approach, as the former can hinder the GC's effectiveness if the array is large.

Upvotes: 8

user405725
user405725

Reputation:

Take a look at pin_ptr, it lets you pass address of a managed class to an unmanaged function.

Upvotes: 1

Related Questions