Reading Programming

Jan 22, 2006 13:10 # 41430

null *** wants to know...

Determine drive size

So I've got this code (it's Delphi but should easily be understood by non-Delphi people) which obtains a physical drive's geometry and attempts to compute the total drive size:

DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,nil,0,
                    @Geo,SizeOf(Geo),Len1,nil) ;
    With Geo Do Size := Cylinders.QuadPart * Int64(TracksPerCylinder)
                        * Int64(SectorsPerTrack) * Int64(BytesPerSector) ;

It's the only Win32 function that actually gives me a result - GetFileSize(), GetFileInformationByHandle() and friends either return 0 or fail; GetDiskFreeSpace() works on logical drives but requires that they be formatted and it reports the size minus the file system structures.

The above code works fine, however, it computes a size that is slightly off (the reported size is a few MB too small for my USB flash disk and two different 80GB HDDs) and doesn't work for logical drives (volumes).

Am I doing something wrong? Is there any other way to determine the size of a physical (and maybe logical) drive? Google can't help me, nor can intercepting system calls from recovery software that delivers the correct drive size.
Any help would be greatly appreciated!

"God is dead." - Nietzsche, 1882 "Nietzsche is dead." - God, 1900

This post was edited by null on Jan 22, 2006.

Jan 23, 2006 11:09 # 41439

null *** has all the information you need...

Partial solution

I think I've at least partially solved the problem. DeviceIoControl(IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS) on a volume returns a list of disk slices belonging to the volume, including their correct size. In the most basic case the volume consists of one partition and that partition's size is also the disk size.
Still no success determining a physical drive's size tho.

"God is dead." - Nietzsche, 1882 "Nietzsche is dead." - God, 1900


Favorites (edit)

Small text Large text

Netalive Amp (Skin for Winamp)