Search code examples
c++wmi

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

  • 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.