PowerCoder
PowerCoder

Reputation: 57

GetLogicalDrives plus additional information C++

I need help with one of my programs that pulls out available drives on a system and prints various information about the drives. I am using VC++ and am fairly new to C++ and need some high level inputs or example code from experienced programmers.

Here is my current source code:

#include "stdafx.h"
#include Windows.h 
#include stdio.h 
#include iostream 

using namespace std; 

int main()
{
    // Initial Dummy drive
    WCHAR myDrives[] = L" A";  

    // Get the logical drive bitmask (1st drive at bit position 0, 2nd drive at bit position 1... so on)  
    DWORD myDrivesBitMask = GetLogicalDrives();  

    // Verifying the returned drive mask  
    if(myDrivesBitMask == 0)   
        wprintf(L"GetLogicalDrives() failed with error code: %d\n", GetLastError());  
    else  { 
        wprintf(L"This machine has the following logical drives:\n");   
        while(myDrivesBitMask)     {      
            // Use the bitwise AND with 1 to identify 
            // whether there is a drive present or not. 
            if(myDrivesBitMask & 1)    {     
                // Printing out the available drives     
                wprintf(L"drive %s\n", myDrives);    
            }    
            // increment counter for the next available drive.   
            myDrives[1]++;    
            // shift the bitmask binary right    
            myDrivesBitMask >>= 1;   
        }   
        wprintf(L"\n");  
    }  
    system("pause");   
}

` -Here is the output-

This machine has the following logical drives:
drive  C
drive  D
drive  E
drive  F
drive  G
drive  H
drive  I

I need to output additional information about each drive (perhaps an example will tell the story in a shorter amount of time):

Drive – C:\ Drive Type: Fixed Drive Ready Status: True Volume Label: Boot Drive File System Type : NTFS Free Space: 30021926912 Total Drive Size: 240055742464

Drive – D:\ Drive Type: Fixed Drive Ready Status: True Volume Label: Application Data File System Type : NTFS Free Space: 42462507008 Total Drive Size: 240054693888

Which methods, libs api, etc. can I use to pull out drive type, drive status, volume label, file system type, free space, and total drive size?

*Side note, I noticed a defect with my pre-processor directives, specifically within the standard I/O header files. I know that is not the recommended way using printf and cout is type safe and the proper route to go but I couldn't figure out how to format output in cout as you would do in wprintf(L"drive %s\n", myDrives);.... so how would you do this with cout??

Thanks in advance.

Upvotes: 1

Views: 4487

Answers (2)

torno
torno

Reputation: 476

To check whether a drive is ready you may also use GetDiskFreeSpaceEx. If this fails, the drive is not ready/usable.

Here is some example code: http://pinvoke.net/default.aspx/coredll/GetDiskFreeSpaceEx.html

Upvotes: 0

Steve
Steve

Reputation: 7271

You want to look at functions such as GetVolumeInformation to retrieve file system information such as free space and volume name.

GetDriveType will give you some basic information about the drive type, but USB thumb sticks and flash readers can give surprising results.

I'm not sure what you mean by "ready status". If you mean whether there is a valid volume in the drive, then you can try CreateFile with a path of "\\.\C:" to try and open the volume. If it fails then there is no volume (disk) present. This will be of use for SD card readers. To do this without an error dialog appearing you will need to call SetErrorMode(SEM_NOOPENFILEERRORBOX) first.

Upvotes: 2

Related Questions