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

本文介绍了一种将托管的 C# Byte 数组转换为非托管 unsigned char 的方法,并提供了使用 Marshal.Copy 或 pin_ptr 进行转换的具体步骤。
部署运行你感兴趣的模型镜像

I wanted to convert array< Byte>^ to unsigned char*. I have tried to explain what i have done. I donot know how to proceed further. Please show me the right approach. I am using 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);

Here vPnt is a number. But how can copy the data from vPublicKey in to vUnmanagedPublicKey.

Answers

//-------------------------------------

Try replacing your last two lines with this:

Marshal::Copy(vPublicKey, 0, IntPtr(vUnmanagedPublicKey), vPublicKeySize);

You have already allocated a buffer in unmanaged memory to copy the key to, so there is no need to allocate unmanaged memory using AllocHGlobal. You just needed to convert your unmanaged pointer (vUnmanagedPublicKey) to a managed pointer (IntPtr) so that Marshal::Copy could use it. IntPtr takes a native pointer as one of the arguments to its constructor to perform that conversion.

So your full code could look something like this:

array<Byte>^ vPublicKey = vX509->GetPublicKey();
unsigned char        vUnmanagedPublicKey[MAX_PUBLIC_KEY_SIZE]; 
ZeroMemory(vUnmanagedPublicKey, MAX_PUBLIC_KEY_SIZE);

Marshal::Copy(vPublicKey, 0, IntPtr(vUnmanagedPublicKey), vPublicKey->Length);
//-------------------------------------

Instead of using the marshalling-API it is easier to just pin the managed array:

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;
转自:http://stackoverflow.com/questions/1012396/arraybyte-to-unsigned-char-marshall-class-interop-issue

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

在 CLI/C++ 中使用 `array<System::Byte>^` 类型需要满足以下条件: 1. `array<System::Byte>^` 是一种托管数组类型,用于存储字节数据(即 `System::Byte` 类型),适用于 .NET Framework 中的 CLI/C++ 项目。它通常用于处理二进制数据、网络传输或文件读写操作中的原始字节流。 2. 在使用 `array<System::Byte>^` 时,必须包含相关的命名空间,例如 `System` 和 `System::IO`,以确保编译器能够识别 `System::Byte` 和数组类型。以下代码片段展示了如何声明和初始化一个 `array<System::Byte>^` 实例: ```cpp #include <iostream> using namespace System; using namespace System::IO; int main() { array<System::Byte>^ buffer = gcnew array<System::Byte>(4); // 初始化一个长度为4的字节数组 FileStream^ fs = gcnew FileStream("test.bin", FileMode::Open); fs->Read(buffer, 0, buffer->Length); // 从文件中读取字节到数组中 return 0; } ``` 3. `array<System::Byte>^` 可以与原生 C++ 类型(如 `unsigned char*` 或 `char*`)进行相互转换,但需要使用 `pin_ptr<System::Byte>` 来固定托管数组在内存中的位置,以避免垃圾回收器移动它。例如: ```cpp void TestByteArray(array<System::Byte>^ byteArray) { pin_ptr<System::Byte> p = &byteArray[0]; unsigned char* pby = p; char* pch = reinterpret_cast<char*>(pby); // 使用 pch 或 pby 进行操作 } ``` 该方法确保了托管数组的地址可以安全地传递给原生 C++ 代码,适用于需要与非托管代码交互的场景。 4. `array<System::Byte>^` 可以通过 `System::Text::Encoding` 类转换为字符串,或者将字符串转换为字节数组。例如,将字节数组转换为十六进制字符串: ```cpp array<System::Byte>^ buffer = gcnew array<System::Byte>(4); // 假设 buffer 已被填充 String^ str = gcnew String(""); for (int i = 0; i < buffer->Length; ++i) { Int32 temp = Int32::Parse(buffer[i]->ToString()); String^ strTemp = temp.ToString("x2"); str += strTemp; if (i < (buffer->Length - 1)) str += " "; } Console::WriteLine(str); ``` 这种方法适用于需要将二进制数据以可读格式(如十六进制)显示或传输的场景。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值