我无法弄清楚该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)
有人知道我是否将这种方法用于不是它的目的吗?