Win32 Windows Volume Program and Code Example 5

 

 

 

 

 

Obtaining Volume Information Program Example

 

The GetVolumeInformation() function retrieves information about the file system on a given volume. This information includes the volume name, volume serial number, file system name, file system flags, maximum length of a file name, and so on. Before you access files and directories on a given volume, you should determine the capabilities of the file system by using the GetVolumeInformation() function. This function returns values that you can use to adapt your application to work effectively with the file system. In general, you should avoid using static buffers for file names and paths. Instead, use the values returned by GetVolumeInformation() to allocate buffers as you need them. If you must use static buffers, reserve 256 characters for file names and 260 characters for paths.

The GetSystemDirectory() and GetWindowsDirectory() functions retrieve the paths to the system directory and the Windows directory, respectively. The GetDiskFreeSpace() function retrieves organizational information about a volume, including the number of bytes per sector, the number of sectors per cluster, the number of free clusters, and the total number of clusters. However, GetDiskFreeSpace() cannot report volume sizes that are greater than 2 GB. To ensure that your application works with large capacity hard drives, use the GetDiskFreeSpaceEx() function. The GetDriveType() function indicates whether the volume referenced by the specified drive letter is a removable, fixed, CD-ROM, RAM, or network drive. The GetLogicalDrives() function identifies the volumes present. The GetLogicalDriveStrings() function retrieves a null-terminated string for each volume present. Use these strings whenever a root directory is required.

Create a new Win32 console application project and give a suitable project name.

 

Obtaining Volume Information Program Example - Creating a new Win32 empty console application project

 

Add the source file and give a suitable name.

 

Obtaining Volume Information Program Example - adding a new C++ source file

 

Add the following source code.

 

#include <stdio.h>

#include <Windows.h>

 

int wmain()

{

      // + 1 is for NULL

      WCHAR volumeName[MAX_PATH + 1] = { 0 };

      WCHAR fileSystemName[MAX_PATH + 1] = { 0 };

      DWORD serialNumber = 0;

      DWORD maxComponentLen = 0;

      DWORD fileSystemFlags = 0;

     

      if (GetVolumeInformation(

            L"C:\\", // L"\\MyServer\MyShare\"

        volumeName,

        sizeof(volumeName),

        &serialNumber,

        &maxComponentLen,

        &fileSystemFlags,

        fileSystemName,

        sizeof(fileSystemName)) == TRUE)

     

      {

            wprintf(L"GetVolumeInformation() should be fine!\n");

            wprintf(L"Volume Name: %s\n", volumeName);

            wprintf(L"Serial Number: %lu\n", serialNumber);

            wprintf(L"File System Name: %s\n", fileSystemName);

            wprintf(L"Max Component Length: %lu\n", maxComponentLen);

            wprintf(L"File system flags: 0X%.08X\n", fileSystemFlags);

      }

      else

      {

            wprintf(L"GetVolumeInformation() failed, error %u\n", GetLastError());

      }

 

      return 0;

}

 

Build and run the project. The following screenshot is an output sample.

 

Obtaining Volume Information Program Example - a sample console output

 

The following sample output is querying the DVD/CD drive with CD inside the drive.

 

Obtaining Volume Information Program Example - a sample output querying the CD/DVD drives

 

 

 

  < Windows Volume 4 | Win32 Programming Index | Windows Volume Index | Windows Volume 6 >