johnbrovi johnbrovi - 9 months ago 66
C Question

WinAPI. How to get Hard Drive free/busy space

I'm writing a program for getting some of the hard drive information. At the moment, i was able to get the full size of the hard disk using the DeviceIoControl function and corresponding IOCTL_DISK_GET_DRIVE_GEOMETRY flag .

I'm tried to use GetDiskFreeSpace function and send the "\\.\PhysicalDrive0" argument, but it doesn't work.

BOOL bResult = GetDiskFreeSpace("\\\\.\\PhysicalDrive0", &dwSectorsPerCluster,
&dwBytesPerSector, &dwNumberOfFreeClusters,

if (bResult == FALSE) {
std::cout << "Can't retrieve disk free space info." << std::endl;
return bResult;

Output: Can't retrieve disk free space info.
For example, "\\.\PhysicalDrive0" replaced on "C:" work fine.

The question is how to get the free or used space on hard drive using WinAPI functions?

P.S. Without using WMI.


GetDiskFreeSpace is "work" but need use correct first parameter.

lpRootPathName [in]

The root directory of the disk for which information is to be returned.

"\\\\.\\PhysicalDrive0" is incorrect because this is for whole disk drive and it not mounted by file system. however this request is handled by file system. you need use names like "\\\\?\\c:\\" or "\\\\?\\Harddisk0Partition<N>\\" or "\\\\?\\HarddiskVolume<N>\\" or "\\\\?\\Volume{guid}\\" - slash on the and is very important - without it will be no work. but in case "\\\\?\\PhysicalDrive0\\" - slash no help - will be no work - because no FS mount this device