Marshal.SizeOf structure returns excessive number

后端 未结 2 1969
囚心锁ツ
囚心锁ツ 2021-01-18 21:02

I have the following structure

[StructLayout(LayoutKind.Sequential)]
public struct SFHeader
{

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    pu         


        
2条回答
  •  耶瑟儿~
    2021-01-18 21:34

    You could use a fixed size buffer instead of a string.

    [StructLayout(LayoutKind.Sequential)]
    public unsafe struct SFHeader
    {
        public fixed char FileName[5];
        public int Offset;
        public short Size;
        public byte Flags;
        public byte Source;
        public long LastWriteTime;
    
        public byte[] GetBytes()
        {
            //omitted
        }
    
        public static SFHeader FromBytes(byte[] buffer)
        {
            //omitted
        }
    }
    

提交回复
热议问题