Tuesday, March 3, 2009

[VC++] Finding logical drive letters using GetLogicalDriveStrings()

Below code demonstrates how to find the logical drive letters in your machine using API GetLogicalDriveStrings(). If executed the below code on finding each drive letter it will show a message box displaying the drive letter.
TCHAR tszDriveList[512];
TCHAR tszDrive [3*sizeof(TCHAR)];
// Get the logical drive strings
int nDriveStrLen = GetLogicalDriveStrings( sizeof( tszDriveList ), tszDriveList );
if( 0 != nDriveStrLen )
{
// Parse the returned multi-string array.
int nIdx = 0;
while( 0 != tszDriveList[nIdx])
{
_tcscpy( tszDrive, &(tszDriveList[nIdx]));
// Show the drive obtained
AfxMessageBox( tszDrive );
// Skipping index for getting the whole :\ with drive letter
while( 0 != tszDriveList[nIdx] )
{
nIdx++;
}
nIdx++;
}
}

No comments: