Do you need an absolutely specific layout or is it acceptable to just make size 8?
I ask about it because it is laid out as follows
[StructLayout(LayoutKind.Explicit, Size=8)] public struct SomeStruct { [FieldOffset(0)] public byte SomeByte; [FieldOffset(1)] public int SomeInt; [FieldOffset(5)] public short SomeShort; [FieldOffset(7)] public byte SomeByte2; }
Has non-aligned word fields that may be causing your problem.
If you can reorder things, this may work for you:
[StructLayout(LayoutKind.Explicit, Size=8)] public struct SomeStruct { [FieldOffset(0)] public byte SomeByte; [FieldOffset(1)] public byte SomeByte2; [FieldOffset(2)] public short SomeShort; [FieldOffset(4)] public int SomeInt; }
When I test with this on an emulator, it works great.
Obviously, if you are not ready to allow regrouping, you cannot do anything.
This answer and this old article will strongly indicate that you should align your structures to the minimum multiple of their size (I tried with int aligned at offset 2, and this also caused an error)
Given your need to interact with external data, the most likely solution is to:
[StructLayout(LayoutKind.Explicit, Size=8)] public struct SomeStruct { [FieldOffset(0)] private byte b0; [FieldOffset(1)] private byte b1; [FieldOffset(2)] private byte b2; [FieldOffset(3)] private byte b3; [FieldOffset(4)] private byte b4; [FieldOffset(5)] private byte b5; [FieldOffset(6)] private byte b6; [FieldOffset(7)] private byte b7;
ShuggyCoUk
source share