Getting List of Volumes always Says C:\
Why The Following Code always reports C:\
although It reports different Device Name
handle = FindFirstVolumeW(volName, sizeof(volName));
do{
wchar_t wVolName[MAX_PATH];
QString::fromWCharArray(volName).toWCharArray(wVolName);//make a copy of volName on wVolName
wVolName[wcsle开发者_开发知识库n(volName)-1] = L'\0';
wchar_t wDeviceName[MAX_PATH];
int charCount = 0;
charCount = QueryDosDeviceW(&wVolName[4], wDeviceName, ARRAYSIZE(wDeviceName));
qDebug() << QString::fromWCharArray(wVolName) << "Device: " << QString::fromWCharArray(wDeviceName);//print wVolName and wDeviceName
wchar_t driveName[MAX_PATH];
GetVolumePathName(wDeviceName, driveName, MAX_PATH);
CloseHandle(handle);
qDebug() << QString::fromWCharArray(driveName);
}while(FindNextVolume(handle, volName, sizeof(volName)));
FindVolumeClose(handle);
Output:
"\\?\Volume{5c77cc58-d5ab-11e0-a0ec-806d6172696f}" Device: "\Device\HarddiskVolume2"
"C:\"
"\\?\Volume{5c77cc59-d5ab-11e0-a0ec-806d6172696f}" Device: "\Device\HarddiskVolume3"
"C:\"
"\\?\Volume{5c77cc57-d5ab-11e0-a0ec-806d6172696f}" Device: "\Device\CdRom0"
"C:\"
"\\?\Volume{5c77cc56-d5ab-11e0-a0ec-806d6172696f}" Device: "\Device\Floppy0"
"C:\"
"\\?\Volume{8d974f2c-e9a1-11e0-b7da-0013d407432f}" Device: "\Device\Harddisk1\DP(1)0- 0+8"
"C:\"
Why doesn't it report D
, E
, etc ..
EDIT
and How can I derive the Drive Letter assigned to the Volume
The documentation for the function says it all:
You must specify a valid Win32 namespace path. If you specify an NT namespace path, for example, "\DosDevices\H:" or "\Device\HardDiskVolume6", the function returns the drive letter of the current volume, not the drive letter of that NT namespace path.
By the way, a volume can be mounted to multiple drive letters (a drive name like C:
is nothing more than a symlink in the NT namespace), so it doesn't really make sense to translate in this manner.
From the GetVolumePathName documentation:
If you specify a relative directory or file name without a volume qualifier, GetVolumePathName returns the drive letter of the current volume.
Perhaps because you are calling CloseHandle
while in the loop: don't do that.
It looks like you modeled your code after http://msdn.microsoft.com/en-us/library/cc542456%28v=vs.85%29.aspx: you'll notice the only time they call CloseHandle
is AFTER the entire loop is done.
精彩评论