'How to get free space of all logical drives of hard disk using WMI in C++?

I would like to calculate free space of entire hard drive using c++ and WMI.

For example. if HDD contains 3 logical drives say C: , D: , E: and each logical drive has below configuration.

drive Total Space Free Space

C: 10GB 5 GB D: 20GB 8 GB E: 15GB 7 GB

So I need to fetch the free hard drive space i.e free space of all drives C,D and E.

SO it should return 5+8+7 = 20 GB.

Also I don't know what all logical drives exists for that Hard drive.



Solution 1:[1]

The non WMI ay is much easier.

You can use GetLogicalDriveStrings (https://msdn.microsoft.com/en-us/library/windows/desktop/aa364975(v=vs.85).aspx) to get all the drives in the system.

Next use GetDiskFreeSpace (https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx) to find the free space of a specific drive.

If yo really want to (have to) stick to WMI, the page https://msdn.microsoft.com/en-us/library/windows/desktop/aa393244(v=vs.85).aspx will give some directions on how to instantiate WMI classes, and you will need to work with the Win32_LogicalDisk (https://msdn.microsoft.com/en-us/library/windows/desktop/aa394173(v=vs.85).aspx) class which has a FreeSpace member.

Solution 2:[2]

Here is a fully working function which will get you the free space in all drives. It generates the result as a CString but of course you can return a number (of bytes).

CString GetFreeDiskSpace()
{
    CString str_result{ L"" };
    DWORD cchBuffer;
    WCHAR stddriveStrings[2048];
    WCHAR *driveSetings = &stddriveStrings[0];
    UINT driveType;
    PWSTR driveTypeString;
    ULARGE_INTEGER freeSpace;

    // Find out the required buffer size that we need
    cchBuffer = GetLogicalDriveStrings(0, NULL);


    // Fetch all drive letters as strings 
    GetLogicalDriveStrings(cchBuffer, driveSetings);

    // Loop until we reach the end (by the '\0' char)
    // driveStrings is a double null terminated list of null terminated strings)
    while (*driveSetings)
    {
        // Dump drive information
        driveType = GetDriveType(driveSetings);
        GetDiskFreeSpaceEx(driveSetings, &freeSpace, NULL, NULL);

        switch (driveType)
        {
            case DRIVE_FIXED:
                driveTypeString = L"Hard Drive";
                break;

            case DRIVE_CDROM:
                driveTypeString = L"CD/DVD";
                break;

            case DRIVE_REMOVABLE:
                driveTypeString = L"Removable";
                break;

            case DRIVE_REMOTE:
                driveTypeString = L"Network";
                break;

            default:
                driveTypeString = L"Unknown";
                break;
        }

        str_result.Format(L"%s\n%s - %s - %I64u GB free", 
            str_result, driveSetings, driveTypeString,
            freeSpace.QuadPart / 1024 / 1024 / 1024);

  // Move to next drive string
  // +1 is to move past the null at the end of the string.
        driveSetings += _tcsclen(driveSetings) + 1;
    }


    return str_result;

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ferenc Deak
Solution 2 Michael Haephrati