Error when count partitions of the MBR and GPT Disk

Just starting out? Need help? Post your questions and find answers here.
hoangdiemtinh
User
User
Posts: 29
Joined: Wed Nov 16, 2022 1:51 pm

Error when count partitions of the MBR and GPT Disk

Post by hoangdiemtinh »

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

Code: Select all

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:

Code: Select all

[18:46:03] [error] Stack overflow.
Please someone help me.

Thanks.
hoangdiemtinh
User
User
Posts: 29
Joined: Wed Nov 16, 2022 1:51 pm

Re: Error when count partitions of the MBR and GPT Disk

Post by hoangdiemtinh »

someone help me.
User avatar
HeX0R
Addict
Addict
Posts: 973
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Error when count partitions of the MBR and GPT Disk

Post by HeX0R »

How many bytes do you think that structure will need?

Code: Select all

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
[Edit]
A little more hints, I guess that might be not enough
Since you are using that structure like this:

Code: Select all

Protected struc.DRIVE_LAYOUT_INFORMATION_EX
Anything will be put on the stack, but there is a limit (can't remember the real limit).
Huge data should be better not put on the stack, better use memory, like this:

Code: Select all

Protected *struc.DRIVE_LAYOUT_INFORMATION_EX = AllocateMemory(SizeOf(DRIVE_LAYOUT_INFORMATION_EX))
hoangdiemtinh
User
User
Posts: 29
Joined: Wed Nov 16, 2022 1:51 pm

Re: Error when count partitions of the MBR and GPT Disk

Post by hoangdiemtinh »

I changed to 512. Error: (122) The data area passed to a system call is too small.

EDIT:

I changed to 1024. Code run fine, but result count partitions is 0.
User avatar
HeX0R
Addict
Addict
Posts: 973
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Error when count partitions of the MBR and GPT Disk

Post by HeX0R »

Most of your structure definitions are wrong, you should better use those from breeze4me here:
viewtopic.php?p=591984#p591984

And take note on my edit above also.

Unfortunately I can't help any further, I don't need anything like this and have not the time to dive into it.
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

Re: Error when count partitions of the MBR and GPT Disk

Post by AZJIO »

hoangdiemtinh
User
User
Posts: 29
Joined: Wed Nov 16, 2022 1:51 pm

Re: Error when count partitions of the MBR and GPT Disk

Post by hoangdiemtinh »

AZJIO wrote: Mon Jul 17, 2023 4:55 pm Get_MBR_GPT
Not work on GPT Disks. (I am also use in WinPE)
AZJIO
Addict
Addict
Posts: 1298
Joined: Sun May 14, 2017 1:48 am

Re: Error when count partitions of the MBR and GPT Disk

Post by AZJIO »

hoangdiemtinh wrote: Mon Jul 17, 2023 11:07 pm Not work on GPT Disks. (I am also use in WinPE)
I added this code to ChkDskGui and was told that it works well.
Show me a screenshot in "AOMEI Partition Assistant" and also in my program so I can see that it doesn't work.
hoangdiemtinh
User
User
Posts: 29
Joined: Wed Nov 16, 2022 1:51 pm

Re: Error when count partitions of the MBR and GPT Disk

Post by hoangdiemtinh »

AZJIO wrote: Mon Jul 17, 2023 11:20 pm
hoangdiemtinh wrote: Mon Jul 17, 2023 11:07 pm Not work on GPT Disks. (I am also use in WinPE)
I added this code to ChkDskGui and was told that it works well.
It not detect the Initialized Drive (unallocated). And it not detect (count) the Hidden Patitions.
see more: viewtopic.php?t=80153
Post Reply