Microsoft Learn

Volume Management API Reference

Manage and query information about storage volumes in Windows.

Introduction

The Volume Management API provides functions for interacting with storage volumes, including querying volume properties, formatting, mounting, and unmounting. These operations are crucial for managing storage devices and ensuring data integrity.

This API is primarily used by system administrators, developers building storage management tools, and applications that require fine-grained control over disk storage.

Key Functions

Here are some of the most commonly used functions for volume management:

GetVolumeInformation

Retrieves detailed information about a specified volume, such as its serial number, file system name, and flags indicating its capabilities and state.

BOOL GetVolumeInformation(
  _In_opt_  LPCTSTR lpRootPathName,
  _Out_opt_ LPTSTR  lpVolumeNameBuffer,
  _In_      DWORD   nVolumeNameSize,
  _Out_opt_ LPDWORD lpVolumeSerialNumber,
  _Out_opt_ LPDWORD lpMaximumComponentLength,
  _Out_opt_ LPDWORD lpFileSystemFlags,
  _Out_opt_ LPTSTR  lpFileSystemNameBuffer,
  _In_      DWORD   nFileSystemNameSize
);
                

Parameters:

Name Type Description
lpRootPathName LPCTSTR The root directory of the volume to be queried. This parameter can be NULL.
lpVolumeNameBuffer LPTSTR A buffer that receives the name of the volume.
nVolumeNameSize DWORD The size, in characters, of the buffer pointed to by lpVolumeNameBuffer.
lpVolumeSerialNumber LPDWORD A pointer to a variable that receives the volume's serial number.
lpMaximumComponentLength LPDWORD A pointer to a variable that receives the maximum length, in characters, of a file name component on the specified file system.
lpFileSystemFlags LPDWORD A pointer to a variable that receives bit flags that describe the specified file system.
lpFileSystemNameBuffer LPTSTR A buffer that receives the name of the file system.
nFileSystemNameSize DWORD The size, in characters, of the buffer pointed to by lpFileSystemNameBuffer.

FormatVolume (Conceptual - typically achieved via utilities or lower-level APIs)

While there isn't a direct Win32 API named FormatVolume for general-purpose formatting, the functionality is achieved through lower-level APIs or command-line utilities like format.exe which interact with the storage stack.

Note: Direct programmatic formatting is complex and carries significant risk. It's usually handled by dedicated tools.

MountVol (Utility Equivalent)

The MountVol command-line utility is used to manage volume mount points. Programmatically, this functionality is often exposed via WMI or specific Storage Management APIs.

Common operations include:

  • Listing volumes and their mount points.
  • Adding mount points.
  • Deleting mount points.
// Example of conceptual WMI interaction (not direct API call)
// You would typically use scripting or C++ with WMI providers.
SELECT * FROM Win32_Volume
                

Common Tasks

Getting a List of Volumes

To get a list of all mounted volumes, you can iterate through drive letters (C:, D:, etc.) and call GetVolumeInformation for each, or use WMI to query the Win32_Volume class.

Retrieving Disk Space Information

Use the GetDiskFreeSpaceEx function to obtain information about the amount of free and total disk space on a specified volume.

BOOL GetDiskFreeSpaceEx(
  _In_opt_  LPCTSTR lpDirectoryName,
  _Out_opt_ULARGE_INTEGER lpFreeBytesAvailableToCaller,
  _Out_opt_ ULARGE_INTEGER lpTotalNumberOfBytes,
  _Out_opt_ ULARGE_INTEGER lpTotalNumberOfFreeBytes
);
                

Managing Mount Points

Mount points allow you to attach a volume to an empty directory on another NTFS volume. This can be achieved using the SetVolumeMountPoint and DeleteVolumeMountPoint functions.

BOOL SetVolumeMountPoint(
  _In_ LPCTSTR MountPoint,
  _In_ LPCTSTR VolumePathName
);

BOOL DeleteVolumeMountPoint(
  _In_ LPCTSTR MountPoint
);
                

Error Handling

Always check the return values of API functions. If a function fails, call GetLastError to retrieve a specific error code that can help diagnose the problem.

Important: Operations like formatting or modifying mount points can lead to data loss if not performed correctly. Ensure you have proper error handling and user confirmation in place.