array^ TO unsigned char* :: Marshall 类 - 互操作问题

发布于 2024-07-25 02:18:07 字数 754 浏览 5 评论 0原文

我想转换 array< 字节>^ 到无符号字符*。 我试图解释我所做的事情。 我不知道如何进一步进行。 请告诉我正确的方法。 我使用的是 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);

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

谢谢
拉吉

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.

Thank you
Raj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-08-01 02:18:07

与使用编组 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;

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;
淡忘如思 2024-08-01 02:18:07

尝试用以下内容替换最后两行:

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

您已经在非托管内存中分配了一个缓冲区来将密钥复制到其中,因此无需使用 AllocHGlobal 分配非托管内存。 您只需将非托管指针 (vUnmanagementPublicKey) 转换为托管指针 (IntPtr),以便 Marshal::Copy 可以使用它。 IntPtr 将本机指针作为其构造函数的参数之一来执行该转换。

所以你的完整代码可能看起来像这样:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文