开发者

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

开发者 https://www.devze.com 2023-04-12 16:58 出处:网络
I\'m writing a native wrapper around a managed component written in C++\\CLI. I have the following function in managed 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 unma开发者_开发知识库naged one.

gcroot<cli::array<System::Byte>^> managedArray = _managedObject->Function();


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消