Copy unmanaged data into managed array
Posted
by
JeffRSon
on Stack Overflow
See other posts from Stack Overflow
or by JeffRSon
Published on 2011-06-19T14:34:19Z
Indexed on
2012/06/09
4:40 UTC
Read the original article
Hit count: 135
c++-cli
I need to copy native (i.e. unmanaged) data (byte*) to managed byte array with C++/CLI (array).
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);
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);
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?
© Stack Overflow or respective owner