且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用C#在USB上创建多个分区

更新时间:2023-02-12 21:58:56

我找到了解决方案.

这是因为:

  1. Marshal.AllocHGlobal分配的内存不是零填充.
  2. PARTITION_INFORMATION_GPT结构名称成员填写的只有8个 分配的内存字节数相反,它需要72个字节.
  1. Marshal.AllocHGlobal allocated memory is not zero filled.
  2. PARTITION_INFORMATION_GPT structure name member filled is only has 8 bytes of memory allocated instead it requires 72 bytes.


[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct PARTITION_INFORMATION_GPT
{
    /// <summary>
    /// A GUID that identifies the partition type.
    /// 
    /// Each partition type that the EFI specification supports is identified by its own GUID, which is 
    /// published by the developer of the partition.
    /// </summary>
    [FieldOffset(0)]
    public Guid PartitionType;

    /// <summary>
    /// The GUID of the partition.
    /// </summary>
    [FieldOffset(16)]
    public Guid PartitionId;

    /// <summary>
    /// The Extensible Firmware Interface (EFI) attributes of the partition.
    /// 
    /// </summary>
    [FieldOffset(32)]
    public UInt64 Attributes;

    /// <summary>
    /// A wide-character string that describes the partition.
    /// </summary>
    [FieldOffset(40)]
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 36)]
    public string Name;
}