Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate disk volume names?

Tags:

windows

winapi

How can i enumerate a list of all logical volumes on a disk? I want the name of the volume that is suitable for opening with CreateFile.

What have you tried?

I've used the FindFirstVolume/FindNextVolume API to enumerate a list of 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 those volume names are valid volume names. That is, none of those names can be passed to CreateFile to open the volume:

0x00000003 (The system cannot find the path specified)

The question might be how do i convert the thing returned by FindFirstVolume into a volume name?

But the real question is how do i enumerate volumes in the first place?

Why not just use \\.\C:?

I wasn't asking how to hard-code a volume name; i was asking how to enumerate volume names.

Besides, not every volume has a drive letter, e.g.:

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

I swear there is an API to enumerate volumes.

GetLogicalDriveStrings

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

  • C:\
  • D:\

and not volumes. In my case it misses two volumes:

  • System Reserved
  • D:\CDROM

that FindFirstVolume does correctly return.

Bonus Reading

  • MSDN: Enumerating Volumes
  • How to explicitly lock a mounted file system?
like image 906
Ian Boyd Avatar asked Mar 23 '15 14:03

Ian Boyd


People also ask

How do you name a volume?

Volume names must begin with the letter “d” followed by a number (for example, d0 ). Instead of specifying the full volume name, such as /dev/md/dsk/d1 , you can often use an abbreviated volume name, such as d1 , with any meta* command. Like physical slices, volumes have logical names that appear in the file system.

How do I find the volume GUID?

The easiest way to view all volume GUIDs on your system, is to run the "mountvol" command in command prompt. * Note: "mountvol" command is used to create, delete, or list a volume mount point. But if you give the command with any arguments lists all the Volume GUIDs.

What is a volume GUID?

Volume{GUID}\" where GUID is a globally unique identifier (GUID) that identifies the volume. A volume GUID path is sometimes referred to as a unique volume name, because a volume GUID path can refer only to one volume. However, this term is misleading, because a volume can have more than one volume GUID path.


1 Answers

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

All volume and mounted folder functions that take a volume GUID path as an input parameter require the trailing backslash. [...] but this is not the case with the CreateFile function. You can open a volume by calling CreateFile and omit the trailing backslash from the volume name you specify. CreateFile processes a volume GUID path with an appended backslash as the root directory of the volume.

The solution is easy: Strip the trailing backslash from a volume GUID path to open the volume using CreateFile.

In other words, while volume management functions such as:

  • GetVolumeInformation
  • GetVolumePathNamesForVolumeName

do take the full volume name returned by FindFirstVolume/FindNextVolume, CreateFile requires the returned trailing backslash 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}
like image 98
IInspectable Avatar answered Oct 07 '22 07:10

IInspectable