How do I convert a cli::array to a native array fr

2019-05-06 17:38发布

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

2条回答
Luminary・发光体
2楼-- · 2019-05-06 18:16

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

查看更多
姐就是有狂的资本
3楼-- · 2019-05-06 18:20

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.

查看更多
登录 后发表回答