When using Marshal to copy data from a byte array to the structure
below, only the first byte of the "other" array is getting copied from
the original byte array. What do I need to specify to get
Marshal.PtrToSt ructure to copy the all the data into the "other"
array?
[StructLayout(La youtKind.Explic it, Size = 40)]
unsafe public struct DeadReckoning
{
[FieldOffset(0)]
public byte algorithm; // 1 byte
[FieldOffset(1)]
public fixed byte other[15]; // 15 bytes
[FieldOffset(16)]
public Vector linearAccelerat ion; // 12 bytes
[FieldOffset(28)]
public Vector angularVelocity ; // 12 bytes
}
}
// Given rawData as a byte array
DeadReckoning dr = new DeadReckoning() ;
IntPtr pData = Marshal.AllocHG lobal(40);
Marshal.Copy(ra wData, 28, pData, 40);
Marshal.PtrToSt ructure(pData, dr);
Marshal.FreeHGl obal(pData);
At this point, only dr.other[0] has the correct value; the values at
1-14 indexes of dr.other are not set.
below, only the first byte of the "other" array is getting copied from
the original byte array. What do I need to specify to get
Marshal.PtrToSt ructure to copy the all the data into the "other"
array?
[StructLayout(La youtKind.Explic it, Size = 40)]
unsafe public struct DeadReckoning
{
[FieldOffset(0)]
public byte algorithm; // 1 byte
[FieldOffset(1)]
public fixed byte other[15]; // 15 bytes
[FieldOffset(16)]
public Vector linearAccelerat ion; // 12 bytes
[FieldOffset(28)]
public Vector angularVelocity ; // 12 bytes
}
}
// Given rawData as a byte array
DeadReckoning dr = new DeadReckoning() ;
IntPtr pData = Marshal.AllocHG lobal(40);
Marshal.Copy(ra wData, 28, pData, 40);
Marshal.PtrToSt ructure(pData, dr);
Marshal.FreeHGl obal(pData);
At this point, only dr.other[0] has the correct value; the values at
1-14 indexes of dr.other are not set.
Comment