Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert (managed to unmanaged) array<System::Byte ^> to byte*?

Tags:

c++-cli

I need help to make such conversion:

byte* bytes = Battle::Storm::GetBytes(0);

Now I get the error:

Error 3 error C2440: 'initializing' : cannot convert from 'cli::array ^' to 'byte *'

How can I do this?

like image 945
cnd Avatar asked Oct 31 '11 11:10

cnd


2 Answers

From the error message I understand that Battle::Storm::GetBytes(0); returns a multi dimensional array, which is in the form of cli::array<Byte,dimension> ^

To convert it to native unsigned char*

array<Byte,N> ^ byteMultiArray = Battle::Storm::GetBytes(0);
pin_ptr<unsigned char> array_pin = &byteArray[0, ... ,Nth 0]; 
unsigned char * nativeArray = array_pin;

Here the number N is the dimension of the array.

//for N = 2  
pin_ptr<unsigned char> array_pin = &byteArray[0,0];
//for N = 4  
pin_ptr<unsigned char> array_pin = &byteArray[0,0,0,0];
like image 108
ali_bahoo Avatar answered Nov 02 '22 05:11

ali_bahoo


You can use pin_ptr<> to get unmanaged array

array<Byte>^ arr =  gcnew array<Byte>(100) ;
pin_ptr<unsigned char> pUnmanagedArr = &arr[0];
like image 37
9 revs, 2 users 98% Avatar answered Nov 02 '22 06:11

9 revs, 2 users 98%