array^ TO unsigned char* :: Marshall 类 - 互操作问题
我想转换 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与使用编组 API 相比,固定托管数组更容易:
Instead of using the marshalling-API it is easier to just pin the managed array:
尝试用以下内容替换最后两行:
您已经在非托管内存中分配了一个缓冲区来将密钥复制到其中,因此无需使用 AllocHGlobal 分配非托管内存。 您只需将非托管指针 (vUnmanagementPublicKey) 转换为托管指针 (IntPtr),以便 Marshal::Copy 可以使用它。 IntPtr 将本机指针作为其构造函数的参数之一来执行该转换。
所以你的完整代码可能看起来像这样:
Try replacing your last two lines with this:
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: