PureBasic - форум

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » PureBasic - форум » PureBasic для Windows » Error when count partitions of the MBR and GPT Disk


Error when count partitions of the MBR and GPT Disk

Сообщений 1 страница 3 из 3

1

Today I try to count Partitions of the MBR and GPT Disk, but not success.

Код:
EnableExplicit

#IOCTL_DISK_GET_DRIVE_LAYOUT = $7400c    ; HEX
#IOCTL_DISK_GET_DRIVE_LAYOUT_EX = $70050 ; HEX

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

Structure PARTITION_INFORMATION_GPT Align #PB_Structure_AlignC
  TypeGuid.b[16] ;GUID
  IDGuid.b[16]   ;GUID
  Attributes.q
  Name.c[36]
EndStructure

Structure PARTITION_INFORMATION_EX Align #PB_Structure_AlignC
  PartitionStyle.w
  StartingOffset.d
  PartitionLength.d
  PartitionNumber.i
  RewritePartition.b
  IsServicePartition.b
  StructureUnion
    piMBR.PARTITION_INFORMATION_MBR
    piGPT.PARTITION_INFORMATION_GPT
  EndStructureUnion
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_MBR Align #PB_Structure_AlignC
  Signature.i
  CheckSum.i
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_GPT Align #PB_Structure_AlignC
  DiskId.b[16]
  StartingUsableOffset.q
  UsableLength.q
  MaxPartitionCount.l
EndStructure

Structure DRIVE_LAYOUT_INFORMATION Align #PB_Structure_AlignC
  PartitionCount.w
  Signature.w
  PartitionEntry.PARTITION_INFORMATION_EX[8192]
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_EX Align #PB_Structure_AlignC
  PartitionStyle.w
  PartitionCount.w
  StructureUnion
    dliMBR.DRIVE_LAYOUT_INFORMATION_MBR
    dliGPT.DRIVE_LAYOUT_INFORMATION_GPT
  EndStructureUnion
  PartitionEntry.PARTITION_INFORMATION_EX[8192]
EndStructure

Procedure _CreateFile_PHYSICALDRIVE_ONLY_READ(iDrive.i)
  Protected hDevice = CreateFile_("\\.\PHYSICALDRIVE" + iDrive, #GENERIC_READ, #FILE_SHARE_READ , 0, #OPEN_EXISTING, 0, 0)
  ProcedureReturn hDevice
EndProcedure

Procedure$ _WinAPI_count_Partitions(iDrive.i)
  Protected struc.DRIVE_LAYOUT_INFORMATION_EX
  Protected Ret$ = "Unknown", iBytesRet
  Protected hDevice = _CreateFile_PHYSICALDRIVE_ONLY_READ(iDrive)
  If hDevice <> #INVALID_HANDLE_VALUE ; <> 0
    If DeviceIoControl_(hDevice, #IOCTL_DISK_GET_DRIVE_LAYOUT_EX, 0, 0, @struc, SizeOf(DRIVE_LAYOUT_INFORMATION_EX), @iBytesRet, 0)
      Debug "Disk #" + iDrive
      Debug "PartitionStyle: " + struc\PartitionStyle
      Select struc\PartitionStyle
        Case 0 ; MBR
          Debug "Signature: " + struc\dliMBR\Signature
          Debug "Partition Count on MBR Disks: " + struc\PartitionCount
        Case 1 ; GPT
          Debug "Partition Count on GPT Disks: " + struc\PartitionCount
      EndSelect
    Else
      Debug "DeviceIoControl Failed on Disk #" + iDrive
    EndIf
  Else
    Debug "Have error when opening Disk #" + iDrive
  EndIf
  CloseHandle_(hDevice)
EndProcedure

Debug _WinAPI_count_Partitions(0); for my MBR Disk
Debug _WinAPI_count_Partitions(1); for my GPT Disk

Result:

Код:
[18:46:03] [error] Stack overflow.

Please someone help me.

Thanks.

0

2

Very large structure size (about 2 MB). Does not fit on the stack.

Код:
EnableExplicit

#IOCTL_DISK_GET_DRIVE_LAYOUT = $7400c    ; HEX
#IOCTL_DISK_GET_DRIVE_LAYOUT_EX = $70050 ; HEX

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

Structure PARTITION_INFORMATION_GPT Align #PB_Structure_AlignC
  TypeGuid.b[16] ;GUID
  IDGuid.b[16]   ;GUID
  Attributes.q
  Name.c[36]
EndStructure

Structure PARTITION_INFORMATION_EX Align #PB_Structure_AlignC
  PartitionStyle.w
  StartingOffset.d
  PartitionLength.d
  PartitionNumber.i
  RewritePartition.b
  IsServicePartition.b
  StructureUnion
    piMBR.PARTITION_INFORMATION_MBR
    piGPT.PARTITION_INFORMATION_GPT
  EndStructureUnion
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_MBR Align #PB_Structure_AlignC
  Signature.i
  CheckSum.i
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_GPT Align #PB_Structure_AlignC
  DiskId.b[16]
  StartingUsableOffset.q
  UsableLength.q
  MaxPartitionCount.l
EndStructure

Structure DRIVE_LAYOUT_INFORMATION Align #PB_Structure_AlignC
  PartitionCount.w
  Signature.w
  PartitionEntry.PARTITION_INFORMATION_EX[8192]
EndStructure

Structure DRIVE_LAYOUT_INFORMATION_EX Align #PB_Structure_AlignC
  PartitionStyle.w
  PartitionCount.w
  StructureUnion
    dliMBR.DRIVE_LAYOUT_INFORMATION_MBR
    dliGPT.DRIVE_LAYOUT_INFORMATION_GPT
  EndStructureUnion
  PartitionEntry.PARTITION_INFORMATION_EX[8192]
EndStructure

Procedure _CreateFile_PHYSICALDRIVE_ONLY_READ(iDrive.i)
  Protected hDevice = CreateFile_("\\.\PHYSICALDRIVE" + iDrive, #GENERIC_READ, #FILE_SHARE_READ , 0, #OPEN_EXISTING, 0, 0)
  ProcedureReturn hDevice
EndProcedure

Procedure$ _WinAPI_count_Partitions(iDrive.i)
  Protected *struc.DRIVE_LAYOUT_INFORMATION_EX
  Protected Ret$ = "Unknown", iBytesRet
  Protected hDevice = _CreateFile_PHYSICALDRIVE_ONLY_READ(iDrive)
  If hDevice <> #INVALID_HANDLE_VALUE ; <> 0
    *struc = AllocateStructure(DRIVE_LAYOUT_INFORMATION_EX)
    If *struc
      If DeviceIoControl_(hDevice, #IOCTL_DISK_GET_DRIVE_LAYOUT_EX, 0, 0, *struc, SizeOf(DRIVE_LAYOUT_INFORMATION_EX), @iBytesRet, 0)
        Debug "Disk #" + iDrive
        Debug "PartitionStyle: " + *struc\PartitionStyle
        Select *struc\PartitionStyle
          Case 0 ; MBR
            Debug "Signature: " + *struc\dliMBR\Signature
            Debug "Partition Count on MBR Disks: " + *struc\PartitionCount
          Case 1 ; GPT
            Debug "Partition Count on GPT Disks: " + *struc\PartitionCount
        EndSelect
      Else
        Debug "DeviceIoControl Failed on Disk #" + iDrive
      EndIf
      
      FreeStructure(*struc)
    EndIf
  Else
    Debug "Have error when opening Disk #" + iDrive
  EndIf
  CloseHandle_(hDevice)
EndProcedure

Debug _WinAPI_count_Partitions(0); for my MBR Disk
Debug _WinAPI_count_Partitions(1); for my GPT Disk

0

3

Thank you.
Stack overflow: GONE!.

My line of code is probably wrong somewhere. So the result for Partition Count is 0.

Can you please help me with Partition Count (counting how many partitions the hard drive has, which partitions windows can recognize) ?

0


Вы здесь » PureBasic - форум » PureBasic для Windows » Error when count partitions of the MBR and GPT Disk