Win32 Windows Volume Program and Code Example 14

 

 

 

Creating Mounted Folders Programmatically

 

Creating a mounted folder is a two-step process. First, we call GetVolumeNameForVolumeMountPoint() with the mount point (drive letter, volume GUID path, or mounted folder) of the volume to be assigned to the mounted folder. Then use the SetVolumeMountPoint() function to associate the returned volume GUID path with the desired directory on another volume. Your application can designate any empty directory on a volume other than the root as a mounted folder. When you call the SetVolumeMountPoint() function, that directory becomes the mounted folder. You can assign the same volume to multiple mounted folders. After the mounted folder has been established, it is maintained through computer restarts automatically.

If a volume fails, any volumes that have been assigned to mounted folders on that volume can no longer be accessed through those mounted folders. For example, suppose you have two volumes, C: and D:, and that D: is associated with the mounted folder C:\MountD\. If volume C: fails, volume D: can no longer be accessed through the path C:\MountD\.

Only NTFS file system volumes can have mounted folders, but the target volumes for the mounted folders can be non-NTFS volumes. Mounted folders are implemented by using reparse points and are subject to their restrictions. It is not necessary to manipulate reparse points to use mounted folders; functions such as SetVolumeMountPoint() handle all the reparse point details for you. Because mounted folders are directories, you can rename, remove, move, and otherwise manipulate them, as you would other directories. (Note: The TechNet documentation uses the term mounted drives to refer to mounted folders.)

 

 

 

Enumerating Mounted Folders

 

The following functions are used to enumerate the mounted folders on a specified NTFS volume:

 

  1. FindFirstVolumeMountPoint()
  2. FindNextVolumeMountPoint()
  3. FindVolumeMountPointClose()

 

These functions operate in a manner very similar to the FindFirstFile(), FindNextFile(), and FindClose() functions. To enumerate mounted folders on a volume, first find out if the volume supports mounted folders. To do so, use the volume name returned by the FindFirstVolume() and FindNextVolume() functions to call the GetVolumeInformation() function. The names returned include a trailing backslash (\) to be compatible with the GetDriveType() function and related functions. When you call the GetVolumeInformation() function, if NTFS is returned in the lpFileSystemNameBuffer parameter, the volume is an NTFS volume. The NTFS file system supports mounted folders.

If the volume is an NTFS volume, begin a search for the mounted folders by calling FindFirstVolumeMountPoint(). If the search is successful, process the results according to your application's requirements. Then use FindNextVolumeMountPoint() in a loop to locate and process the mounted folders one at a time. When there are no more mounted folders to be enumerated, close the search handle by calling FindVolumeMountPointClose(). Note that the search will find only the mounted folders that are on the specified volume. You should not assume any correlation between the order of the mounted folders that are returned by these functions and the order of the mounted folders that are returned by other functions or tools.

 

Determining Whether a Directory Is a Mounted Folder

 

It is useful to determine whether a directory is a mounted folder when, for example, you are using a backup or search application that is limited to one volume. Such an application can reach information on multiple volumes if you use functions such as SetVolumeMountPoint() to create mounted folders for the other volumes on the volume that the application is limited to. To determine if a specified directory is a mounted folder, first call the GetFileAttributes() function and inspect the FILE_ATTRIBUTE_REPARSE_POINT flag in the return value to see if the directory has an associated reparse point. If it does, use the FindFirstFile() and FindNextFile() functions to obtain the reparse tag in the dwReserved0 member of the WIN32_FIND_DATA structure. To determine if the reparse point is a mounted folder (and not some other form of reparse point), test whether the tag value equals the value IO_REPARSE_TAG_MOUNT_POINT. To obtain the target volume of a mounted folder, use the GetVolumeNameForVolumeMountPoint() function. In a similar manner, you can determine if a reparse point is a symbolic link by testing whether the tag value is IO_REPARSE_TAG_SYMLINK.

 

Assigning a Drive Letter to a Volume

 

You can assign a drive letter (for example, x:\) to a local volume using SetVolumeMountPoint(), provided there is no volume already assigned to that drive letter. If the local volume already has a drive letter then SetVolumeMountPoint() will fail. To handle this, first delete the drive letter using DeleteVolumeMountPoint(). The system supports at most one drive letter per volume. Therefore, you cannot have C:\ and F:\ represent the same volume.

 

Caution 

 

Deleting an existing drive letter and assigning a new one may break existing paths, such as those in desktop shortcuts. It may also break the path to the program making the drive letter changes. With Windows virtual memory management, this may break the application, leaving the system in an unstable and possibly unusable state. It is the program designer's responsibility to avoid such potential catastrophes.

 

Mounted Folder Functions

 

The mounted folder functions can be divided into three groups:

 

  1. General-purpose functions
  2. Functions used to scan for volumes, and
  3. Functions used to scan a volume for mounted folders.

 

General-Purpose Mounted Folder Functions

 

The following Table lists the general-purpose mounted folder functions.

 

Function

Description

DeleteVolumeMountPoint()

Deletes a drive letter or mounted folder.

GetVolumeNameForVolumeMountPoint()

Retrieves the volume GUID path for the volume that is associated with the specified volume mount point (drive letter, volume GUID path, or mounted folder).

GetVolumePathName()

Retrieves the mounted folder that is associated with the specified volume.

SetVolumeMountPoint()

Associates a volume with a drive letter or a directory on another volume.

 

 

Volume-Scanning Functions

 

The following Table lists the volume-scanning functions.

 

Function

Description

FindFirstVolume()

Returns the name of a volume on a computer. FindFirstVolume() is used to begin enumerating the volumes of a computer.

FindNextVolume()

Continues a volume search started by a call to FindFirstVolume().

FindVolumeClose()

Closes a search for volumes.

 

Mounted Folder Scanning Functions

 

The following Table lists the mounted folder scanning functions.

 

Function

Description

FindFirstVolumeMountPoint()

Retrieves the name of a mounted folder on the specified volume. FindFirstVolumeMountPoint() is used to begin scanning the mounted folders on a volume.

FindNextVolumeMountPoint()

Continues a mounted folder search started by a call to FindFirstVolumeMountPoint().

FindVolumeMountPointClose()

Closes a search for mounted folders.

 

 

  < Windows Volume 13 | Win32 Programming Index | Windows Volume Index | Windows Volume 15 >