将非托管数据复制到托管数组 [英] Copy unmanaged data into managed array

查看:130
本文介绍了将非托管数据复制到托管数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用C ++ / CLI(数组)将本地(即非托管)数据(字节*)复制到托管字节数组。

I need to copy native (i.e. unmanaged) data (byte*) to managed byte array with C++/CLI (array).

我尝试了Marshal :: Copy (数据由const void *数据指向,并且是dataSize字节)

I tried Marshal::Copy (data is pointed to by const void* data and is dataSize bytes)

array<byte>^ _Data=gcnew array<byte>(dataSize);
System::Runtime::InteropServices::Marshal::Copy((byte*)data, _Data, 0, dataSize);

这会产生错误C2665:16个重载都不能转换所有参数。然后我尝试了

This gives error C2665: none of the 16 overloads can convert all parameters. Then I tried

System::Runtime::InteropServices::Marshal::Copy(new IntPtr(data), _Data, 0, dataSize);

会产生错误C2664:参数1无法从 const void *转换为 __w64 int

which produces error C2664: parameter 1 cannot be converted from "const void*" to "__w64 int".

那么该怎么做,是否确实是元帅::抄写这样做的最佳(最简单/最快)方法?

So how can it be done and is Marshal::Copy indeed the "best" (simplest/fastest) way to do so?

推荐答案

IntPtr 只是围绕 void *的包装。您不需要新语法,只需使用显式转换运算符即可。

"IntPtr" is just a wrapper around a "void *". You shouldn't need the new syntax, just use of the explicit conversion operator.

System::Runtime::InteropServices::Marshal::Copy( IntPtr( ( void * ) data ), _Data, 0, dataSize );

应该工作。

这篇关于将非托管数据复制到托管数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆