1

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[wcslen(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

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • Did you check GetVolumePathName succeeded? Does it return an error or success code? – Useless Sep 29 '11 at 16:30
  • Because `\Device\HarddiskVolume2` likely gets resolved to `C:\Device\HarddiskVolume2`, based on your current directory. – avakar Sep 29 '11 at 16:30
  • It is in fact written in the documentation: 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. – avakar Sep 29 '11 at 16:32
  • @Useless: Y I checked. It returns 1. I just omitted it to brief the code. – Neel Basu Sep 29 '11 at 16:33
  • @avakar: Then What I need to do To prevent it from Playing that way (prepending ` C:\ ` ) ? – Neel Basu Sep 29 '11 at 16:33
  • @NeelBasu, you can't. No prefix of that path is a volume. – avakar Sep 29 '11 at 16:35

3 Answers3

3

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.

avakar
  • 32,009
  • 9
  • 68
  • 103
2

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Then What I need to do such that it doesn't return the current Volume always ? – Neel Basu Sep 29 '11 at 16:38
  • 1
    @NeelBasu, use GetVolumePathNamesForVolumeName instead using the wVolName as the parameter. – Mark Ransom Sep 29 '11 at 16:46
  • Yep Man !! Its Working. But One more question . It says GetVolumePathName`s` So How it behaves If there are Multiple Places It has been mounted on ? – Neel Basu Sep 29 '11 at 17:01
  • Oh! I got it "The list is an array of null-terminated strings terminated by an additional NULL character." But I must say Its a real bad design – Neel Basu Sep 29 '11 at 17:04
1

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.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Yes I modeled it after http://stackoverflow.com/questions/7584627/list-of-physical-storage-devices-win32-c/7584873#7584873 and the link you provided.But If It kind of value not flushed kind of problem why the device name is changing – Neel Basu Sep 29 '11 at 16:37