C# Union Structure Marshalling

前端 未结 1 1743
耶瑟儿~
耶瑟儿~ 2021-01-21 01:34

I\'m trying to integrate Video4Linux in my managed application. Indeed I\'ve declared all required structures and relative ioctl. In this question I present two i

相关标签:
1条回答
  • 2021-01-21 02:14

    The problem has been solved by forcing the structure size to the actual one (204, in my case), and remove the array field RawData (as suggested by David Heffernan).

    Indeed:

    [StructLayout(LayoutKind.Explicit, Pack = 4, CharSet = CharSet.Ansi, Size = 204)]
    public struct Format
    {
        public Format(BufferType bufferType)
        {
            Type = bufferType;
            Pixmap = new PixmapFormat();
        }
    
        public Format(BufferType bufferType, PixmapFormat pixmapFormat)
        {
            Type = bufferType;
            Pixmap = pixmapFormat;
        }
    
        [FieldOffset(0)]
        public BufferType Type;
    
        [FieldOffset(4)]
        public PixmapFormat Pixmap;
    }
    

    Of course, Marshal.SizeOf(typeof(Format)) == 204.

    0 讨论(0)
提交回复
热议问题