GCHandles的数组

时间:2019-01-18 22:06:02

标签: c++-cli

我在c ++ / cli中有一个字节数组数组:

array<array<System::Byte>^>^ data;

我需要将其转换为指针的c ++样式矢量

vector<uint8_t*> cData;

然后我将cData发送给一个函数,并在完成后释放固定的内存。

代码如下:

void ProcessImages(const std::vector<const uint8_t*> srcImages);

void MyCLIFunc(array<array<System::Byte>^>^ data)
{
     vector<uint8_t*> cData;

     //Does not compile
     std::vector<pin_ptr<Byte>> pinnedVector; // error C3239: 'cli::pin_ptr<unsigned char> *': pointer to interior/pin pointer is disallowed by the common language runtime`

     //Does not compile
     std::vector<GCHandle> gchandles; //>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.16.27023\include\vector(935): error C3699: '&&': cannot use this indirection on type '_Ty' with   [ _Ty=System::Runtime::InteropServices::GCHandle  ]

     //Here I want to call ProcessImages, converting data to srcImages without copying mem
     ProcessImages(cData);
}

我缺少什么,如何存储GCHandles / pin_ptr的集合?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果有人感兴趣: 我遇到的问题实际上是固定锯齿状数组。

我需要使用CLI数组来存储句柄, 以下代码对我有用:

auto handles = gcnew cli::array<GCHandle>(dataSize);
for (UInt32 i = 0; i < dataSize; i++)
    {
        GCHandle handle = GCHandle::Alloc(frame[i], GCHandleType::Pinned);
        handles[i] = handle;
        cData.push_back((const uint8_t*)handle.AddrOfPinnedObject().ToPointer());
    }
ProcessImages(cData);
for (UInt32 i = 0; i < dataSize; i++)
    handles[i].Free();