Need help with a program that shows hard-disk informations

I have a school project that requires to do a c++ program that shows the informations on the hard-disk. Informations like: the number of partitions and their names, partitions capacity, etc. My teacher said that can be done in about 15 lines. I have tried some things but nothing worked. Can you help me with this one, please? I am new in C++.
C++ itself knows nothing about hard-drives. Functions for fetching information on that sort of thing may be provided by your operating system. What operating system are you using?
I am using Windows XP.
Why are you being asked to do this? (It doesn't have very much to do with C++.)

Might as well learn/practice some C++ features while we are at it.

std::string https://cal-linux.com/tutorials/strings.html
std::bitset<> http://stdcxx.apache.org/doc/stdlibug/8-4.html
Initializer lists http://www.stroustrup.com/C++11FAQ.html#init-list
std::numeric_limits<> http://en.cppreference.com/w/cpp/types/numeric_limits
value initialisation http://en.cppreference.com/w/cpp/language/value_initialization

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <bitset>
#include <limits>
#include <string>
#include <windows.h>
#include <iomanip>

int main()
{
    const std::string drive_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364972(v=vs.85).aspx
    std::bitset< std::numeric_limits<DWORD>::digits > drive_bitset( GetLogicalDrives() ) ;

    for( std::size_t i = 0 ; i < drive_letters.size() ; ++i ) if( drive_bitset[i] ) // if bit i is set
    {
        const std::string root_dir { drive_letters[i], ':' } ; // eg. C: (initializer_list)
        
        //  https://msdn.microsoft.com/en-us/library/windows/desktop/aa364939(v=vs.85).aspx
        if( GetDriveTypeA( root_dir.c_str() ) == DRIVE_FIXED ) // if it is a fixed disk drive
        {
            ULARGE_INTEGER total_bytes {} ; // value initialisation
            ULARGE_INTEGER free_bytes {} ;
            // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
            if( GetDiskFreeSpaceExA( root_dir.c_str(), nullptr, std::addressof(total_bytes), std::addressof(free_bytes) ) )
            {
                char vol_name[ MAX_PATH+1 ] {} ; // value initialisation
                // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364994(v=vs.85).aspx
                GetVolumeNameForVolumeMountPointA( ( root_dir + "\\" ).c_str(), vol_name, MAX_PATH ) ; // volume name (if any)

                char dev_name[ MAX_PATH+1 ] {} ;
                // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365461(v=vs.85).aspx
                QueryDosDeviceA( root_dir.c_str(), dev_name, MAX_PATH ) ; 

                std::cout << "drive letter: " << drive_letters[i]
                          << "\n  mount name: " << vol_name
                          << "\n device name: " << dev_name
                          << "\n total bytes:" << std::setw(14) << total_bytes.QuadPart
                          << "\n  used bytes:" << std::setw(14) << total_bytes.QuadPart - free_bytes.QuadPart
                          << "\n  free bytes:" << std::setw(14) << free_bytes.QuadPart << "\n\n" ;
            }
        }
    }
}

Typical output for a drive:
drive letter: C
  mount name: \\?\Volume{eac8793c-84d9-11e5-80b4-806e6f6e6963}\
 device name: \Device\HarddiskVolume2
 total bytes:  214379261952
  used bytes:   29090553856
  free bytes:  185288708096

http://rextester.com/YMW83370
Topic archived. No new replies allowed.