array <Byte> ^ to unsigned char*:: Marshall class - Interop Issue

Raj*_*Raj 1 interop cryptography c++-cli type-conversion

我想将数组<Byte> ^转换为unsigned char*.我试图解释我做了什么.我不知道如何继续前进.请告诉我正确的方法.我正在使用MS VC 2005.

//Managed array  
array<Byte>^ vPublicKey = vX509->GetPublicKey();

//Unmanaged array
unsigned char        vUnmanagedPublicKey[MAX_PUBLIC_KEY_SIZE]; 
ZeroMemory(vUnmanagedPublicKey,MAX_PUBLIC_KEY_SIZE);

//MANAGED ARRAY to UNMANAGED ARRAY  

// Initialize unmanged memory to hold the array.  
vPublicKeySize = Marshal::SizeOf(vPublicKey[0]) * vPublicKey->Length;  
IntPtr vPnt = Marshal::AllocHGlobal(vPublicKeySize);

// Copy the Managed array to unmanaged memory.  
Marshal::Copy(vPublicKey,0,vPnt,vPublicKeySize);
Run Code Online (Sandbox Code Playgroud)

这里vPnt是一个数字.但是如何将数据从vPublicKey复制到vUnmanagedPublicKey.

谢谢
Raj

Ras*_*ber 8

而不是使用编组API,更容易固定托管数组:

array<Byte>^ vPublicKey = vX509->GetPublicKey();
cli::pin_ptr<unsigned char> pPublicKey = &vPublicKey[0];

// You can now use pPublicKey directly as a pointer to the data.

// If you really want to move the data to unmanaged memory, you can just memcpy it:
unsigned char * unmanagedPublicKey = new unsigned char[vPublicKey->Length];
memcpy(unmanagedPublicKey, pPublicKey, vPublicKey->Length);
// .. use unmanagedPublicKey
delete[] unmanagedPublicKey;
Run Code Online (Sandbox Code Playgroud)