小编mai*_*oid的帖子

Marshal.Copy,将一个 IntPtr 数组复制到一个 IntPtr 中

我无法弄清楚该Copy(IntPtr[], Int32, IntPtr, Int32)方法是如何工作的。我虽然它可以将包含在多个 IntPtr 中的数据复制到单个 IntPtr(如 MSDN 所述)但显然它不像我预期的那样工作:

IntPtr[] ptrArray = new IntPtr[]
{
    Marshal.AllocHGlobal(1),
    Marshal.AllocHGlobal(2)
 };

 Marshal.WriteByte(ptrArray[0], 0, 0xC1);

 // Allocate the total size.
 IntPtr ptr = Marshal.AllocHGlobal(3);

 Marshal.Copy(ptrArray, 0, ptr, ptrArray.Length);

 // I expect to read 0xC1 but Value is always random!!
 byte value = Marshal.ReadByte(ptr, 0);
Run Code Online (Sandbox Code Playgroud)

有人知道我是否将这种方法用于不是它的目的吗?

c# arrays copy marshalling intptr

6
推荐指数
1
解决办法
9743
查看次数

标签 统计

arrays ×1

c# ×1

copy ×1

intptr ×1

marshalling ×1