Introduction to Volume Management with the win32 API

The GetLogicalDrives() function returns a DWORD bitmask that represents the currently available disk drives. If the function fails, the return value is zero.

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
	int iCounter = 0;
	int iASCIILetter = (int)'a';

	DWORD dwDrivesMask = GetLogicalDrives();

	if (dwDrivesMask == 0){
		printf("Failed to acquire mask of drives.\n");
		exit(EXIT_FAILURE);
	} 

	printf("Drives available:\n");
        //extract the drives from the mask using 
        //logical and-ing with & and bitshift operator <<
	while (iCounter < 24){
		if (dwDrivesMask & (1 << iCounter)){
			printf("%c:\\ \n", iASCIILetter + iCounter);
		}
		iCounter++;
	}

	return 0;
}

The GetLogicalDriveStrings() function returns the valid drives in a system by filling a buffer with a series of null-terminated strings, one for each drive on the system, plus an additional null character at the end of the list. The GetLogicalDriveStrings() function takes two arguments, the first is the length of the buffer in TCHARS, and the second is a pointer to the buffer. Note that the size does not include the final, terminating null character.

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    const int buffer_size = 512;
    TCHAR buffer[buffer_size];
    //we will use the pointer 
    //to iterate through the buffer
    TCHAR *tchPtr = buffer;

    if (GetLogicalDriveStrings(buffer_size - 1, buffer) == 0){
        printf("Unable to get drive strings.\n");
        exit(EXIT_FAILURE);
    }

    //go through buffer until we reach
    //the final double null char that indicates
    //end of the info
    while (*tchPtr != ''){
        printf("%s:\n", tchPtr);
        //_tcslen is the equiv. of strlen
        //does not include the null char, so we 
        //must add one
        tchPtr += _tcslen(tchPtr) + 1;
    }

    return 0;
}

The GetDriveType() function determines whether a disk drive is removable, fixed, CD-ROM, etc. The GetDriveType() function takes a single parameter, a string indicating the root directory for the drive; note that a trailing backslash is required. If the parameter is NULL, then the function uses the root of the current directory.

#include "stdafx.h"
#include <Windows.h>

void DisplayDriveType(int iParam);

int _tmain(int argc, _TCHAR* argv[])
{
    //null param (this drive)
    DisplayDriveType(GetDriveType(NULL));

    //nonexistant drive
    DisplayDriveType(GetDriveType(_T("x:\\")));

    //for all drives
    TCHAR szBuffer[256];
    TCHAR *tchPtr = szBuffer;
    GetLogicalDriveStrings(255, szBuffer);

    //print types for all drives on system
    for (tchPtr; *tchPtr != ''; tchPtr += _tcslen(tchPtr) +1)
    {
        printf("%s:\\ \t", tchPtr);
        DisplayDriveType(GetDriveType(tchPtr));
        printf("\n");
    }

    return 0;
}

void DisplayDriveType(int iParam){
    switch (iParam){
    case DRIVE_UNKNOWN:
        printf("Drive type unknown.\n");
        break;

    case DRIVE_NO_ROOT_DIR:
        printf("No drive for that root path.\n");
        break;

    case DRIVE_REMOVABLE:
        printf("Removable drive.\n");
        break;

    case DRIVE_FIXED:
        printf("Fixed drive.\n");
        break;

    case DRIVE_REMOTE:
        printf("Network drive.\n");
        break;

    case DRIVE_CDROM:
        printf("CD ROM drive.\n");
        break;

    }
}

The GetVolumeInformation() function retrieves file system metadata on a given volume. The GetVolumeInformation() function takes eight parameters. The first parameter is a string that contains the root directory of the volume for which we want information. The second argument is a pointer to a buffer that stores the name of the specified volume. The third argument is the length of the volume name buffer in TCHARS; the maximum buffer size is MAX_PATH + 1. The fourth argument is a pointer to a DWORD variable that receives the volume serial number. The fifth argument is a pointer to a DWORD variable that receives the maximum length, in TCHARs, of a file name component, where a file name component is a section of a filename between backslashes.

#include "stdafx.h"
#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    TCHAR tchVolumeNameBuff[MAX_PATH + 1];
    DWORD dwSerialNumber, dwMaxCompLength;

    GetVolumeInformation(_T("C:\\"), tchVolumeNameBuff, MAX_PATH + 1, &dwSerialNumber, &dwMaxCompLength, NULL, NULL, NULL);

    printf("Name: %s Serial Number: %d\n", tchVolumeNameBuff, dwSerialNumber);

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s