Structures Using Unexpected FieldOffset Behavior

I am trying to understand the explicit structure of the structure and the superposition of the structure, and I do not see the behavior that I expect. Given the following code:

class Program
{

    static void Main(string[] args)
    {
        byte[] bytes = new byte[17];
        bytes[0] = 0x01; // Age is 1    //IntField1
        bytes[1] = 0x00;                //IntField1
        bytes[2] = 0x00;                //IntField1
        bytes[3] = 0x00;                //IntField1
        bytes[4] = 0x02;                //IntField2
        bytes[5] = 0x00;                //IntField2
        bytes[6] = 0x00;                //IntField2
        bytes[7] = 0x00;                //IntField2

        bytes[8] = 0x41;                //CharArray A
        bytes[9] = 0x42;                //CharArray B
        bytes[10] = 0x43;               //CharArray C
        bytes[11] = 0x44;               //CharArray D

        bytes[12] = 0x45;               //CharArray E

        bytes[13] = 0x46;               //CharArray F
        bytes[14] = 0x00; // \0 decimal 0
        bytes[15] = 0x00; // \0 decimal 0
        bytes[16] = 0x01; // 1 decimal 1
        Console.WriteLine(Marshal.SizeOf(typeof(TestStruct)));

        TestStruct testStruct2 = (TestStruct) RawDeserialize(bytes, 0, typeof (TestStruct));

        Console.WriteLine(testStruct2);
        Console.ReadLine();
    }
    public static object RawDeserialize( byte[] rawData, int position, Type anyType )
    {
        int rawsize = Marshal.SizeOf( anyType );
        if( rawsize > rawData.Length )
            return null;

        IntPtr buffer = Marshal.AllocHGlobal( rawsize );
        Marshal.Copy( rawData, position, buffer, rawsize );
        object retobj = Marshal.PtrToStructure( buffer, anyType );
        Marshal.FreeHGlobal( buffer );
        return retobj;
    }
}

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct TestStruct
{
    [FieldOffset(0)]
    public int IntField1;
    [FieldOffset(4)]
    public int IntField2;
    [FieldOffset(8)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public char[] CharArray;
    [FieldOffset(16)]
    public byte SomeByte;        

    [FieldOffset(8)]
    public TestStruct2 SubStruct;

    public override string ToString()
    {
        return string.Format("IntField1: {0}\nIntField2: {1}\nCharArray: {2}\nSomeByte: {3}\nSubStruct:\n{{{4}}}", 
            IntField1, IntField2,  new string(CharArray), SomeByte, SubStruct);
    }
}

[StructLayout(LayoutKind.Explicit, Pack = 1)]
public struct TestStruct2
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
    public char[] CharArray1;
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
    public char[] CharArray2;
    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
    public char[] CharArray3;

    public override string ToString()
    {
        return string.Format("CharArray1: {0}\nCharArray2: {1}\nCharArray3: {2}",
           new string(CharArray1), new string(CharArray2), new string(CharArray3));
    }
}

I expect the result to be something like:

IntField1: 1
IntField2: 2
CharArray: ABCDEF
SomeByte: 1
SubStruct:
{CharArray1: ABCDEF
CharArray2: ABCD
CharArray3: E}

but the result:

IntField1: 1
IntField2: 2
CharArray: ABCD
SomeByte: 1
SubStruct:
{CharArray1: ABCD
CharArray2: ABCD
CharArray3: EF}

CharArray TestStruct 4? , 6 ABCDEF, ABCD. TestStruct2.CharArray1.

+3
3

TestStruct2 CharArray, , TestStruct2 CharArray2 , chararray TestStruct.

TestStruct2 CharArray2, .

, , struct2, ..:

[StructLayout(LayoutKind.Explicit, Pack = 1)]
    public struct TestStruct
    {
        [FieldOffset(8)]
        public TestStruct2 SubStruct;

        [FieldOffset(0)]
        public int IntField1;
        [FieldOffset(4)]
        public int IntField2;
        [FieldOffset(8)]
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public char[] CharArray;
        [FieldOffset(16)]
        public byte SomeByte;        


        public override string ToString()
        {
            return string.Format("IntField1: {0}\nIntField2: {1}\nCharArray: {2}\nSomeByte: {3}\nSubStruct:\n{{{4}}}", 
                IntField1, IntField2,  new string(CharArray), SomeByte, SubStruct);
        }
    }

, TestStruct2 CharArray2 .

+2

char [] , - IntPtr, 4 8 - (x86 x64) .

MarshalAs , , , (, / ).

+2

Keep in mind that char in C # is equal to 2 unicode characters in bytes.

Although I still could not get the expected result, retaining the substructure there. It seems that the overlapping SizeConst arrays are shaking.

0
source

Source: https://habr.com/ru/post/1794613/


All Articles