将字节数组封送到C#结构

将字节数组封送到C#结构,c#,unmanaged,marshalling,C#,Unmanaged,Marshalling,我正在做一个C#项目,用于读取FAT32引导扇区和BPB,问题是我正在使用封送机制将字节数组转换为自定义FAT32数据结构。 我收到一条错误消息,上面说: 无法从程序集“FAT32Management,版本=1.0.0.0,区域性=中性,PublicKeyToken=null”加载类型“FAT32Management.Fat32BootSector”,因为它在偏移量3处包含一个对象字段,该字段未正确对齐或与非对象字段重叠 我无法解决这个问题 这是密码 using System; using Sy

我正在做一个C#项目,用于读取FAT32引导扇区和BPB,问题是我正在使用封送机制将字节数组转换为自定义FAT32数据结构。 我收到一条错误消息,上面说: 无法从程序集“FAT32Management,版本=1.0.0.0,区域性=中性,PublicKeyToken=null”加载类型“FAT32Management.Fat32BootSector”,因为它在偏移量3处包含一个对象字段,该字段未正确对齐或与非对象字段重叠

我无法解决这个问题

这是密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FAT32Management
{
    [StructLayout(LayoutKind.Explicit)]//, CharSet = CharSet.Ansi, Size = 96, Pack = 1)]
    public struct Fat32BootSector
    {
        #region Common Region With all FAT systems
        /// <summary>
        /// First 3 Bytes of the Jump insctructions.
        /// Offset 0x00  
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        [FieldOffset(0x00)]
        public byte[] JumpBootInstructions;

        /// <summary>
        /// 8 Bytes of the OemName
        /// Offset 0x03
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
        [FieldOffset(0x03)]
        public string OemName;

        #region BIOS Parameter Block (BPB)
        /// <summary>
        /// 2 Bytes of the BytesPerSector parameter. The BIOS Paramter Block Starts here
        /// Offset 0x0b  
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x0b)]
        public ushort BpbBytesPerSector;

        /// <summary>
        /// 1 Byte containing the number of sectors per cluster. This must be a power of 2 from 1 to 128
        /// Offset 0x0d
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x0d)]
        public byte BpbSectorsPerCluster;

        /// <summary>
        /// 2 Bytes for the Bpb reserved sectors count, Usually 32 for FAT32.
        /// Offset 0x0e  
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x0e)]
        public ushort BpbReservedSectorsCount;

        /// <summary>
        /// 1 Byte Number of file allocation tables. Almost always 2.
        /// Offset 0x10
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x10)]
        public byte BpbFatCount;

        /// <summary>
        /// 2 Bytes, Maximum number of root directory entries. Only used on FAT12 and FAT16
        /// Offset 0x11
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x11)]
        public ushort BpbMaxRootDirectoriesCount;

        /// <summary>
        /// 2 Bytes, Total sectors (if zero, use 4 byte value at offset 0x20) used only for FAT12 AND FAT16 Systems
        /// Offset 0x13  
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x13)]
        public ushort BpbTotalSectors16;

        /// <summary>
        /// 1 Byte, the media descriptor 
        /// Offset 0x15
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x15)]
        public MediaDescriptor BpbMediaDescriptor;

        /// <summary>
        /// 2 Bytes, Sectors per File Allocation Table for FAT12/FAT16
        /// Offset 0x16
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x16)]
        public ushort BpbSectorsPerFat16;

        /// <summary>
        /// 2 Bytes, Sectors per track
        /// Offset 0x18
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x18)]
        public ushort BpbSectorsPerTrack;

        /// <summary>
        /// 2 Bytes Number of heads.
        /// Offset 0x1a
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x1a)]
        public ushort BpbNumberOfHeads;

        /// <summary>
        /// 4 Bytes Hidden sectors.
        /// Offset 0x1c
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x1c)]
        public uint BpbHiddenSectors;

        /// <summary>
        /// 4 Bytes, Total sectors (if greater than 65535; otherwise, see offset 0x13)
        /// Offset 0x20
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x20)]
        public uint BpbTotalSectors32;

        #endregion

        #endregion

        #region Extended BIOS Parameter Block: FAT32 Specific 

        /// <summary>
        /// 4 Bytes for the number of sectors occupied by ONE FAT. 
        /// Offset 0x24
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x24)]
        public uint FatSize32;

        /// <summary>
        /// 2 Bytes
        /// This field is only defined for FAT32 media and does not exist on FAT12 and FAT16 media.
        /// Bits 0-3 -- Zero-based number of active FAT. Only valid if mirroring is disabled.
        /// Bits 4-6 -- Reserved.
        /// Bit  7     -- 0 means the FAT is mirrored at runtime into all FATs.
        ///             -- 1 means only one FAT is active; it is the one referenced in bits 0-3.
        /// Bits 8-15  -- Reserved.
        /// Offset 0x28
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x28)]
        public ushort ExtendedFlags;

        /// <summary>
        /// 2 Bytes for the file system version. The high byte is major revision number. Low byte is minor revision number. 
        /// Offset 0x2a
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        [FieldOffset(0x24)]
        public byte[] FileSystemVersion;

        /// <summary>
        /// 4 Bytes for the first cluster number of the root directory
        /// Offset 0x2c
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x2c)]
        public uint RootDirFirstClusterNumber;

        /// <summary>
        /// 2 Bytes for the Sector number of FS Information Sector.
        /// Offset 0x30
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x30)]
        public ushort FSInfoSectorNumber;

        /// <summary>
        /// 2 Bytes. If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
        /// Offset 0x32
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x32)]
        public ushort BackupBootSectorNumber;

        /// <summary>
        /// 12 Reserved Bytes.
        /// Offset 0x34
        /// </summary>
        [MarshalAs(UnmanagedType.ByValArray,SizeConst = 12)]
        [FieldOffset(0x34)]
        public byte[] Reserved;

        /// <summary>
        /// 1 Byte for the physical drive number.
        /// Offset 0x40
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x40)]
        public byte PhysicalDriveNumber;

        /// <summary>
        /// 1 Reserved byte.
        /// Offset 0x41
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x41)]
        public byte Reserved1;

        /// <summary>
        /// 1 Byte. The boot signature
        /// Offset 0x42
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x42)]
        public byte ExtendedBootSignature;

        /// <summary>
        /// 4 Bytes for the volume serial number.
        /// Offset 0x43
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x43)]
        public uint VolumeID;

        /// <summary>
        /// 11 Byte for the volume label.
        /// Offset 0x47
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        [FieldOffset(0x47)]
        public string VolumeLabel;

        /// <summary>
        /// 8 Bytes for the file system type string. 
        /// Offset 0x52
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
        [FieldOffset(0x52)]
        public string FileSystemType;

        #endregion

        /// <summary>
        /// Gets the boot sector for the specified drive.
        /// <remarks>The drive letter must have this pattern X: </remarks>
        /// </summary>
        /// <param name="driveLetter">The </param>
        /// <returns>The boot sector for the specified drive.</returns>
        public static Fat32BootSector GetBootSectorForDrive(string driveLetter)
        {
            byte[] bootSector = new byte[Marshal.SizeOf(typeof(Fat32BootSector))];
            string drive = @"\\.\" + driveLetter;
            IntPtr hardDiskPointer = SystemIOCalls.OpenFile(drive);
            // Seeks to the start of the partition
            SystemIOCalls.SeekAbsolute(hardDiskPointer, 0, 0);
            // Read the first reserved sector of the drive data (Boot Sector)
            // The data should be read with a chunk of 512 X byte.
            SystemIOCalls.ReadBytes(hardDiskPointer, bootSector, 512);
            SystemIOCalls.CloseHandle(hardDiskPointer);

            // Marshaling the bytes array to a valid struct
            GCHandle pinnedInfos = GCHandle.Alloc(bootSector, GCHandleType.Pinned);
            var infos = (Fat32BootSector)Marshal.PtrToStructure(pinnedInfos.AddrOfPinnedObject(), typeof(Fat32BootSector));
            pinnedInfos.Free();
            return infos;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.InteropServices;
使用系统文本;
名称空间管理
{
[StructLayout(LayoutKind.Explicit)]/,CharSet=CharSet.Ansi,Size=96,Pack=1]
公共结构FAT32B扇区
{
#区域所有FAT系统的公共区域
/// 
///跳转指令的前3个字节。
///偏移量0x00
/// 
[Marshallas(UnmanagedType.ByValArray,SizeConst=3)]
[字段偏移量(0x00)]
公共字节[]指令;
/// 
///OemName的8个字节
///偏移量0x03
/// 
[Marshallas(UnmanagedType.ByValTStr,SizeConst=8)]
[字段偏移量(0x03)]
公共字符串OemName;
#区域BIOS参数块(BPB)
/// 
///BytePersector参数的2字节。BIOS参数块从这里开始
///偏移量0x0b
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x0b)]
公共ushort BpbBytesPerSector;
/// 
///1字节,包含每个群集的扇区数。这必须是1到128之间的2的幂
///偏移量0x0d
/// 
[Marshallas(UnmanagedType.U1)]
[字段偏移量(0x0d)]
公共字节BpbSectorsPerCluster;
/// 
///Bpb保留扇区计数为2字节,FAT32通常为32字节。
///偏移量0x0e
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x0e)]
公共ushort BpbReservedSectorsCount;
/// 
///文件分配表的1字节数。几乎总是2字节。
///偏移量0x10
/// 
[Marshallas(UnmanagedType.U1)]
[字段偏移量(0x10)]
公共字节BpbFatCount;
/// 
///2字节,最大根目录条目数。仅用于FAT12和FAT16
///偏移量0x11
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x11)]
公共ushort BpbMaxRootDirectoriesCount;
/// 
///2字节,总扇区(如果为零,则在偏移量0x20处使用4字节值),仅用于FAT12和FAT16系统
///偏移量0x13
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x13)]
公共卫生服务部门BPBtotalSector 16;
/// 
///1字节,媒体描述符
///偏移量0x15
/// 
[Marshallas(UnmanagedType.U1)]
[字段偏移量(0x15)]
公共媒体描述符BpbMediaDescriptor;
/// 
///FAT12/FAT16的每个文件分配表2字节、扇区
///偏移量0x16
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x16)]
公共ushort BPBSectorPerfat16;
/// 
///2字节,每个磁道扇区
///偏移量0x18
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x18)]
公共ushort BpbSectorsPerTrack;
/// 
///2字节的头数。
///偏移量0x1a
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x1a)]
公共ushort BPB头数;
/// 
///4字节隐藏扇区。
///偏移量0x1c
/// 
[Marshallas(UnmanagedType.U4)]
[字段偏移量(0x1c)]
公共部门;
/// 
///4字节,总扇区(如果大于65535;否则,请参阅偏移量0x13)
///偏移量0x20
/// 
[Marshallas(UnmanagedType.U4)]
[字段偏移量(0x20)]
公共单位BPB总公司32;
#端区
#端区
#区域扩展BIOS参数块:FAT32特定
/// 
///4字节表示一个FAT占用的扇区数。
///偏移量0x24
/// 
[Marshallas(UnmanagedType.U4)]
[字段偏移量(0x24)]
公共单位FatSize32;
/// 
///2字节
///此字段仅为FAT32介质定义,在FAT12和FAT16介质上不存在。
///位0-3—从零开始的活动FAT数。仅在禁用镜像时有效。
///位4-6——保留。
///位7--0表示FAT在运行时镜像到所有FAT中。
///--1表示只有一个FAT处于活动状态;它是位0-3中引用的FAT。
///位8-15——保留。
///偏移量0x28
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x28)]
美国国旗;
/// 
///文件系统版本为2字节。高字节为主要版本号。低字节为次要版本号。
///偏移量0x2a
/// 
[Marshallas(UnmanagedType.ByValArray,SizeConst=2)]
[字段偏移量(0x24)]
公共字节[]文件系统版本;
/// 
///根目录的第一个群集编号为4字节
///偏移量0x2c
/// 
[Marshallas(UnmanagedType.U4)]
[字段偏移量(0x2c)]
公共uint RootDirFirstClusterNumber;
/// 
///FS信息扇区的扇区号为2字节。
///偏移量0x30
/// 
[Marshallas(UnmanagedType.U2)]
[字段偏移量(0x30)]
公共ushort FSInfoSectorNumber;
/// 
///2字节。如果非零,则表示引导记录副本卷的保留区域中的扇区号。
[FieldOffset(0x03)]
public fixed byte OemName[8]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace FAT32Management
{
    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size = 96, Pack = 1)]
    public unsafe struct Fat32BootSector
    {
        #region Common Region With all FAT systems
        /// <summary>
        /// First 3 Bytes of the Jump insctructions.
        /// Offset 0x00     
        /// </summary>
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        [FieldOffset(0x00)]
        public fixed byte JumpBootInstructions[3];

        /// <summary>
        /// 8 Bytes of the OemName
        /// Offset 0x03
        /// </summary>
        [FieldOffset(0x03)]
        public fixed char OemName[8];

        #region BIOS Parameter Block (BPB)
        /// <summary>
        /// 2 Bytes of the BytesPerSector parameter. The BIOS Paramter Block Starts here
        /// Offset 0x0b     
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x0b)]
        public ushort BpbBytesPerSector;

        /// <summary>
        /// 1 Byte containing the number of sectors per cluster. This must be a power of 2 from 1 to 128
        /// Offset 0x0d
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x0d)]
        public byte BpbSectorsPerCluster;

        /// <summary>
        /// 2 Bytes for the Bpb reserved sectors count, Usually 32 for FAT32.
        /// Offset 0x0e     
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x0e)]
        public ushort BpbReservedSectorsCount;

        /// <summary>
        /// 1 Byte Number of file allocation tables. Almost always 2.
        /// Offset 0x10
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x10)]
        public byte BpbFatCount;

        /// <summary>
        /// 2 Bytes, Maximum number of root directory entries. Only used on FAT12 and FAT16
        /// Offset 0x11
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x11)]
        public ushort BpbMaxRootDirectoriesCount;

        /// <summary>
        /// 2 Bytes, Total sectors (if zero, use 4 byte value at offset 0x20) used only for FAT12 AND FAT16 Systems
        /// Offset 0x13     
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x13)]
        public ushort BpbTotalSectors16;

        /// <summary>
        /// 1 Byte, the media descriptor 
        /// Offset 0x15
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x15)]
        public MediaDescriptor BpbMediaDescriptor;

        /// <summary>
        /// 2 Bytes, Sectors per File Allocation Table for FAT12/FAT16
        /// Offset 0x16
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x16)]
        public ushort BpbSectorsPerFat16;

        /// <summary>
        /// 2 Bytes, Sectors per track
        /// Offset 0x18
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x18)]
        public ushort BpbSectorsPerTrack;

        /// <summary>
        /// 2 Bytes Number of heads.
        /// Offset 0x1a
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x1a)]
        public ushort BpbNumberOfHeads;

        /// <summary>
        /// 4 Bytes Hidden sectors.
        /// Offset 0x1c
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x1c)]
        public uint BpbHiddenSectors;

        /// <summary>
        /// 4 Bytes, Total sectors (if greater than 65535; otherwise, see offset 0x13)
        /// Offset 0x20
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x20)]
        public uint BpbTotalSectors32;

        #endregion

        #endregion

        #region Extended BIOS Parameter Block: FAT32 Specific

        /// <summary>
        /// 4 Bytes for the number of sectors occupied by ONE FAT. 
        /// Offset 0x24
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x24)]
        public uint FatSize32;

        /// <summary>
        /// 2 Bytes
        /// This field is only defined for FAT32 media and does not exist on FAT12 and FAT16 media.
        /// Bits 0-3    -- Zero-based number of active FAT. Only valid if mirroring is disabled.
        /// Bits 4-6    -- Reserved.
        /// Bit  7      -- 0 means the FAT is mirrored at runtime into all FATs.
        ///             -- 1 means only one FAT is active; it is the one referenced in bits 0-3.
        /// Bits 8-15   -- Reserved.
        /// Offset 0x28
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x28)]
        public ushort ExtendedFlags;

        /// <summary>
        /// 2 Bytes for the file system version. The high byte is major revision number. Low byte is minor revision number. 
        /// Offset 0x2a
        /// </summary>
        [FieldOffset(0x2a)]
        public fixed byte FileSystemVersion[2];

        /// <summary>
        /// 4 Bytes for the first cluster number of the root directory
        /// Offset 0x2c
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x2c)]
        public uint RootDirFirstClusterNumber;

        /// <summary>
        /// 2 Bytes for the Sector number of FS Information Sector.
        /// Offset 0x30
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x30)]
        public ushort FSInfoSectorNumber;

        /// <summary>
        /// 2 Bytes. If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
        /// Offset 0x32
        /// </summary>
        [MarshalAs(UnmanagedType.U2)]
        [FieldOffset(0x32)]
        public ushort BackupBootSectorNumber;

        /// <summary>
        /// 12 Reserved Bytes.
        /// Offset 0x34
        /// </summary>
        [FieldOffset(0x34)]
        public fixed byte Reserved[12];

        /// <summary>
        /// 1 Byte for the physical drive number.
        /// Offset 0x40
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x40)]
        public byte PhysicalDriveNumber;

        /// <summary>
        /// 1 Reserved byte.
        /// Offset 0x41
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x41)]
        public byte Reserved1;

        /// <summary>
        /// 1 Byte. The boot signature
        /// Offset 0x42
        /// </summary>
        [MarshalAs(UnmanagedType.U1)]
        [FieldOffset(0x42)]
        public byte ExtendedBootSignature;

        /// <summary>
        /// 4 Bytes for the volume serial number.
        /// Offset 0x43
        /// </summary>
        [MarshalAs(UnmanagedType.U4)]
        [FieldOffset(0x43)]
        public uint VolumeID;

        /// <summary>
        /// 11 Byte for the volume label.
        /// Offset 0x47
        /// </summary>
        [FieldOffset(0x47)]
        public fixed char VolumeLabel[11];

        /// <summary>
        /// 8 Bytes for the file system type string. 
        /// Offset 0x52
        /// </summary>
        [FieldOffset(0x52)]
        public fixed char FileSystemType[8];

        #endregion

        /// <summary>
        /// Gets the boot sector for the specified drive.
        /// <remarks>The drive letter must have this pattern X: </remarks>
        /// </summary>
        /// <param name="driveLetter">The </param>
        /// <returns>The boot sector for the specified drive.</returns>
        public static Fat32BootSector GetBootSectorForDrive(string driveLetter)
        {
            byte[] bootSector = new byte[Marshal.SizeOf(typeof(Fat32BootSector))];
            string drive = @"\\.\" + driveLetter;
            IntPtr hardDiskPointer = SystemIOCalls.OpenFile(drive);
            // Seeks to the start of the partition
            SystemIOCalls.SeekAbsolute(hardDiskPointer, 0, 0);
            // Read the first reserved sector of the drive data (Boot Sector)
            // The data should be read with a chunk of 512 X byte.
            SystemIOCalls.ReadBytes(hardDiskPointer, bootSector, 512);
            SystemIOCalls.CloseHandle(hardDiskPointer);

            // Marshaling the bytes array to a valid struct
            GCHandle pinnedInfos = GCHandle.Alloc(bootSector, GCHandleType.Pinned);
            var infos = (Fat32BootSector)Marshal.PtrToStructure(pinnedInfos.AddrOfPinnedObject(), typeof(Fat32BootSector));
            pinnedInfos.Free();
            return infos;
        }
    }
}