How to list drive volume names?

How can I list all the logical volumes on disk? I want the volume name to be suitable for opening with CreateFile .

What have you tried?

I used the FindFirstVolume / FindNextVolume to list the volumes. It returns a list of names, such as:

  • \\?\Volume{0b777018-3313-11e2-8ccd-806e6f6e6963}\
  • \\?\Volume{0b777019-3313-11e2-8ccd-806e6f6e6963}\
  • \\?\Volume{758a2cf2-cf3a-11e4-8dce-c86000d0b92a}\
  • \\?\Volume{4f81d34b-34f4-11e2-9f6e-c86000d0b92a}\

But none of these volume names are valid volume names. That is, none of these names can be passed to CreateFile to open the volume:

 0x00000003 (The system cannot find the path specified) 

The question is, how can I convert the thing returned by FindFirstVolume to a volume name?

But the real question is: how do I list volumes in the first place?

Why not just use \\.\C: :?

I did not ask how hard to encode the volume name; I asked how to list the names of volumes.

In addition, not every volume has a drive letter, for example:

  • \\?\Volume{0b777019-3313-11e2-8ccd-806e6f6e6963}\ ==> \\.\C:
  • \\?\Volume{758a2cf2-cf3a-11e4-8dce-c86000d0b92a}\ ==> \\.\D:
  • \\?\Volume{0b777018-3313-11e2-8ccd-806e6f6e6963}\ ==> reserved volume of the system without a drive letter
  • \\?\Volume{4f81d34b-34f4-11e2-9f6e-c86000d0b92a}\ ==> the CD installed in the folder

I swear there is an API for listing volumes.

GetLogicalDriveStrings

The problem with GetLogicalDriveStrings is that it only returns logical drives:

  • C:\
  • D:\

not volumes . In my case, it skips two volumes:

  • System reserved
  • D: \ CDROM

for FindFirstVolume do the right thing.

Reading bonuses

+6
source share
1 answer

The answer to your question is hidden inside Volume Naming . When using the GUID path of a volume, the rules are slightly different:

All functions of the volume and installed folder that take the GUID path of the volume as an input parameter require a backslash. [...] but this does not apply to the CreateFile function. You can open a volume by calling CreateFile and omit the trailing backslash with the volume name you specify. CreateFile treats the GUID path of the volume with a backslash as the root directory of the volume.

The solution is easy: split the backslash using the GUID volume to open the volume using CreateFile .

In other words, although volume control features such as:

  • GetVolumeInformation
  • GetVolumePathNamesForVolumeName

enter the full volume name returned by FindFirstVolume / FindNextVolume , CreateFile requires the returned backslash to be removed:

  • \\?\Volume{0b777018-3313-11e2-8ccd-806e6f6e6963}
  • \\?\Volume{0b777019-3313-11e2-8ccd-806e6f6e6963}
  • \\?\Volume{758a2cf2-cf3a-11e4-8dce-c86000d0b92a}
  • \\?\Volume{4f81d34b-34f4-11e2-9f6e-c86000d0b92a}
+11
source

Source: https://habr.com/ru/post/984135/


All Articles