Unable to sort structure containing union

I have a C ++ structure that looks like this:

struct unmanagedstruct
{
    int             flags;
    union
    {
        int             offset[6];
        struct
        {
            float           pos[3];
            float           q[4];
        } posedesc;
    } u;
};

And I'm trying to marshal it like this in C #:

[StructLayout(LayoutKind.Explicit)]
public class managedstruct {
    [FieldOffset(0)]
    public int flags;

    [FieldOffset(4), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6)]
    public int[] offset;

    [StructLayout(LayoutKind.Explicit)]
    public struct posedesc {
        [FieldOffset(0), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3)]
        public float[] pos;

        [FieldOffset(12), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)]
        public float[] q;
    }

    [FieldOffset(4)]
    public posedesc pose;
}

However, when I try to load data into my structure, there are only the first 3 elements of the offset array (the length of the array is 3). I can confirm that their values ​​are correct, but I still need the other 3 elements. Am I doing something obviously wrong?

I use these functions to load a struct:

private static IntPtr addOffset(IntPtr baseAddress, int byteOffset) {
    switch (IntPtr.Size) {
        case 4:
            return new IntPtr(baseAddress.ToInt32() + byteOffset);
        case 8:
            return new IntPtr(baseAddress.ToInt64() + byteOffset);
        default:
            throw new NotImplementedException();
    }
}

public static T loadStructData<T>(byte[] data, int byteOffset) {
    GCHandle pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned);
    T output = (T)Marshal.PtrToStructure(addOffset(pinnedData.AddrOfPinnedObject(), byteOffset), typeof(T));
    pinnedData.Free();
    return output;
}

Download example:

managedstruct mystruct = loadStructData<managedstruct>(buffer, 9000);

Let me know if you need more information.

+5
source share
3 answers

100%, , , . ++ int [] posedesc. , sizeof (int) + sizeof (posedisc). : , int [], posedisc, , ++, .

, , , , , , posedisc. LoadStruct. byte [] , .

+1

, ++ int , .

, offset ++ 64- .

, #, , MarshalAs, ArraySubType.

, ArraySubType, , , , , , offset ++ accros 48 24, , .

int long ++, , , ArraySubType MarshalAs .

0

, , offset pos, , , , . offset 3 ( GetType , Single[]). , .).

, q pos ' 7, fixed ( , ).

0

All Articles