简体   繁体   中英

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,
                   &dwTotalNumberOfClusters);

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?

PS Without using WMI.

GetDiskFreeSpace does "work", but you need to use the 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 the whole disk drive and it is not mounted by file system. However, this request is handled by file system. You need to use names like "\\\\\\\\?\\\\c:\\\\" or "\\\\\\\\?\\\\Harddisk0Partition<N>\\\\" or "\\\\\\\\?\\\\HarddiskVolume<N>\\\\" or "\\\\\\\\?\\\\Volume{guid}\\\\" - slash on the end is very important - without it will not work. But in case of "\\\\\\\\?\\\\PhysicalDrive0\\\\" - slash will not help. It will not work, because no FS is mounted on this device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM