How to get drive name from drive letter using c++

To find drive name from drive letter in c++ is very easy. The Windows.h header file provides many methods to find the volume name or label from the drive letter.
The method GetVolumeInformationA() or GetVolumeInformationW() can be used to derive the volume name from the drive letter.
Find drive name from drive letter in c++

Syntax of GetVolumeInformationA()

bool GetVolumeInformationA
(
LPCSTR drive,
LPSTR VolumeName,
DWORD VolumeNameSize,
LPDWORD VolumeSerialNumber,
LPDWORD MaximumComponentLength,
LPDWORD FileSystemFlags,
LPSTR FileSystemNameBuffer,
DWORD FileSystemNameSize
)

Syntax of GetVolumeInformationW()

bool GetVolumeInformationW
(
LPCWSTR drive,
LPWSTR VolumeName,
DWORD VolumeNameSize,
LPDWORD VolumeSerialNumber,
LPDWORD MaximumComponentLength,
LPDWORD FileSystemFlags,
LPWSTR FileSystemNameBuffer,
DWORD FileSystemNameSize
)
The method return type is bool which returns true if the method was successful and false if the method failed for some reason.
Any one on the above methods can be used. The only difference is the data type of the parameters. For finding drive label only the first three parameters (LPCSTR  drive,  LPSTR   VolumeName,  DWORD   VolumeNameSize) are important .

  • The first parameter "LPCSTR drive", takes the drive letter as argument in the format 'drive:\\' . Example: 'e:\\'
  • The second parameter "LPSTR   VolumeName", will return the volume name of the drive if user has assigned any custom name to it. If no name has been assigned to it then it returns blank.
  • The third parameter "DWORD   VolumeNameSize", specifies the size of VolumeName assigned in the second parameter. The size of the VolumeName should be large enough so that the name of the volume fits in it.
  • Rest of the parameter can be set to NULL and the last parameter is set to zero
The rest of the parameters are not important for our goal but if you are interested in knowing them as well then visit Microsoft's site.

Lets look at an example.

First we have to include the Windows.h header and other necessary headers
#include<iostream>
#include<string>
#include<Windows.h>
using namespace std;
int main()
{
char drive[4] = { ' D', ':', '\\'};//Assigning the drive letter in the format(drive:\\)
char VolumeName[MAX_PATH];//MAX_PATH is the size of the char array.
if (!GetVolumeInformationA(
(LPCSTR)drive,
(LPSTR)VolumeName,
sizeof(VolumeName),
NULL,
NULL,
NULL,
NULL,
0)
)
{
// If method was not successful
cout << "Error";
}
else
{
//displays the volume name
cout << VolumeName;
}
}
Output Example
 My Drive
The above C++ program displays the volume name of drive D if user has used any custom name. If no name has been assigned then VolumeName is empty.

Let us look at another example where drive letter is not hard coded.

#include<iostream>
#include<string>
#include<Windows.h>
void GetVolumeLabel(char volume)
{
char drive[4] = { ' ',':','\\'};
drive[0] = volume;
char VolumeName[MAX_PATH];
if (!GetVolumeInformationA(
(LPCSTR)drive,
(LPSTR)VolumeName,
sizeof(VolumeName),
NULL,
NULL,
NULL,
NULL,
0)
)
{
cout << "Error";
}
else
{
cout << VolumeName;
}
}
int main()
{
GetVolumeLabel('D');
return 0;
}

Comments