Storage Devices
This section provides comprehensive documentation for the Windows API functions related to managing and interacting with storage devices. This includes disk drives, removable media, and other storage peripherals.
Core Concepts
Understanding the fundamental concepts is crucial for effective storage device programming. Key concepts include:
- Device Types: Differentiating between various storage device interfaces like ATA, SCSI, USB mass storage, and NVMe.
- Volume Management: Working with logical disks, partitions, and file systems.
- Raw Disk Access: Interacting directly with the storage device at a sector level for low-level operations.
- Hardware Abstraction Layer (HAL): How Windows abstracts hardware differences for storage devices.
Key API Groups
Disk Management Functions
These functions allow you to query, manage, and manipulate disk drives and their partitions.
GetDiskFreeSpaceEx: Retrieves information about the available disk space on a specified disk.CreateHardLink: Creates a hard link to an existing file.DeviceIoControl: Performs control operations on devices. This is a versatile function used for many low-level storage operations.GetDriveType: Determines the type of a disk drive.
Common Parameters
lpRootPathName: A pointer to a null-terminated string that specifies the root directory of the drive to be queried.lpFreeBytesAvailableToCaller: A pointer to a variable that receives the number of free bytes on the disk that are available to the caller.
Return Value
A non-zero value indicates success. Zero indicates failure. Use GetLastError to get extended error information.
Volume and File System APIs
Interact with logical volumes and the file systems they contain.
GetVolumeInformation: Retrieves information about the file system and volume.SetVolumeLabel: Sets the label of a specified file system volume.FormatEx: Formats a volume with a specified file system, allocation unit size, and label.
Raw Disk Access and Device Control
For advanced scenarios, direct access to device controls is necessary.
ReadFile/WriteFile: Used for reading from and writing to device objects.StorageDeviceProperty: A property set for theSetupDiGetDevicePropertyfunction, used to retrieve storage device properties.- IOCTL Codes: A wide range of IOCTL codes exist for specific operations like retrieving partition information, sending SCSI commands, and managing SMART data. Examples include
IOCTL_DISK_GET_DRIVE_GEOMETRYandIOCTL_STORAGE_QUERY_PROPERTY.
Example Usage (Conceptual)
// Example of checking free space on C: drive
DWORDLONG freeBytes;
TCHAR szDrive[] = TEXT("C:\\");
if (GetDiskFreeSpaceEx(
szDrive,
(LPDWORDLONG)&freeBytes,
NULL,
NULL))
{
wprintf(TEXT("Free space on %s: %I64u bytes.\n"), szDrive, freeBytes);
}
else
{
wprintf(TEXT("Error getting disk space: %lu\n"), GetLastError());
}