Описание функции


Get_MBR_GPT

Получить стркутуру раздела "MBR" или "GPT"

Get_MBR_GPT(NumDrive$)

Параметры

NumDrive$ Номер диска в виде строки, например "0"

Возвращаемое значение

Успех:Возвращает строку "MBR", "GPT" или "RAW"

Примечания

Используйте DriveGetNumber().

Пример


EnableExplicit

; константы и структуры MBR/GPT
; https://www.purebasic.fr/english/viewtopic.php?t=25663&p=220673

#PARTITION_STYLE_MBR = 0
#PARTITION_STYLE_GPT = 1
#PARTITION_STYLE_RAW = 2
#IOCTL_DISK_GET_DRIVE_LAYOUT_EX = $70050

Structure PARTITION_INFORMATION_GPT Align #PB_Structure_AlignC
    Partitiontype.GUID
    PartitionId.GUID
    Attributes.q
    Name.b[36]
EndStructure

Structure PARTITION_INFORMATION_MBR Align #PB_Structure_AlignC
    PartitionType.b
    BootIndicator.b
    RecognizedPartition.b
    HiddenSectors.l
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_GPT Align #PB_Structure_AlignC
    PartitionStyle.GUID
    StartingUsableOffset.LARGE_INTEGER
    UsableLength.LARGE_INTEGER
    MaxPartitionCount.l
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_MBR Align #PB_Structure_AlignC
    DiskId.l
    PartitionCount.l
EndStructure

Structure PARTITION_INFORMATION_EX Align #PB_Structure_AlignC
    PartitionStyle.l
    StartingOffset.LARGE_INTEGER
    PartitionLength.LARGE_INTEGER
    PartitionNumber.l
    RewritePartition.b
    StructureUnion
        ppmbr.PARTITION_INFORMATION_MBR
        ppgpt.PARTITION_INFORMATION_GPT
    EndStructureUnion
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_EX Align #PB_Structure_AlignC
    PartitionStyle.l
    PartitionCount.l
    StructureUnion
        pdmbr.DRIVE_LAYOUT_INFORMATION_MBR
        pdgpt.DRIVE_LAYOUT_INFORMATION_GPT
    EndStructureUnion
    PartitionEntry.PARTITION_INFORMATION_EX[255]
EndStructure
; конец => константы и структуры MBR/GPT

;Получение номера диска и раздела, из буквы раздела
Procedure.s Get_MBR_GPT(NumDrive$)
    Protected pdl.DRIVE_LAYOUT_INFORMATION_EX, Bytes.l, hDrive, res$ = "---"
    hDrive = CreateFile_("\\.\PhysicalDrive" + NumDrive$, 0, #FILE_SHARE_READ | #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0)
;     hDrive = CreateFile_("\\?\C:\", 0, #FILE_SHARE_READ | #FILE_SHARE_WRITE, 0, #OPEN_EXISTING, 0, 0) ; буква вместо номера
    If hDrive <> #INVALID_HANDLE_VALUE
        If DeviceIoControl_(hDrive, #IOCTL_DISK_GET_DRIVE_LAYOUT_EX, 0, 0, @pdl, SizeOf(pdl), @Bytes, 0)
            Select pdl\PartitionStyle
                Case #PARTITION_STYLE_MBR
                    res$="MBR"
                Case #PARTITION_STYLE_GPT
                    res$="GPT"
                Case #PARTITION_STYLE_RAW
                    res$="RAW"
            EndSelect
        EndIf
        CloseHandle_(hDrive)
    EndIf
    ProcedureReturn res$
EndProcedure

Debug Get_MBR_GPT("0")