Question

How can I get the exact size of the hard disk in bytes on the old Windows 2000?

IOCTL_DISK_GET_LENGTH_INFO works fine for volumes, but not on physical disk handles (stuff at //./PHYSICALDISKx) on Windows 2000. (Works fine for physical drives from XP or later)

We still have to support this ancient OS...

UPDATE:

The code is written in C, so I would use plain Windows API if possible.

Was it helpful?

Solution 3

It seems there is no IOCTL that give you the exact hard drive size on Windows 2000.

But there is a workaround, that worked:

I used IOCTL_DISK_GET_DRIVE_GEOMETRY to get cylinders, tracks/cyl, sectors/track, bytes/sector.

Multiplying them gave me a rough size of the HDD but not the exact size (little smaller). I can calculate the almost last sector id from this.

To get the exact size I used IOCTL_SCSI_PASS_THROUGH and sent SCSI read commands to attempt to read beyond the end of disk, so I had a sector number for a non-existing sector, then used binary search to get highest sector id where the read succeeds.

Multiplying it with the bytes/sector, I have the exact size of hard disk in bytes.

OTHER TIPS

You could get the free space in C (without any API or etc)

If you run "dir c:\", the last line will give you the free disk space.

Better solution: "fsutil volume diskfree c:"

Or try the below code...

void main (int argc, wchar_t **argv)
       {
          BOOL  fResult;
          unsigned __int64 i64FreeBytesToCaller,
                           i64TotalBytes,
                           i64FreeBytes;
             fResult = GetDiskFreeSpaceEx (L"C:",
                                     (PULARGE_INTEGER)&i64FreeBytesToCaller,
                                     (PULARGE_INTEGER)&i64TotalBytes,
                                     (PULARGE_INTEGER)&i64FreeBytes);
             if (fResult)
             {
                printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
                printf ("Available space to caller = %I64u MB\n",
                        i64FreeBytesToCaller / (1024*1024));
                printf ("Total space               = %I64u MB\n",
                        i64TotalBytes / (1024*1024));
                printf ("Free space on drive       = %I64u MB\n",
                        i64FreeBytes / (1024*1024));
             }
       }

Whenever I need to get this type of info, I typically use WMI as it provides a wealth of data on just about anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top