GetVolumeInformation(), ProcessVolume(), FindNextVolume(), FindVolumeClose()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Windows volume manipulation

To show: Using GetVolumeInformation(), ProcessVolume(), FindNextVolume(), FindVolumeClose() functions

 

 

// Target for Win Xp

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

#define BUFSIZE MAX_PATH

#define FILESYSNAMEBUFSIZE MAX_PATH

 

BOOL ProcessVolume(HANDLE hVol, char *Buf, int iBufSize)

{

DWORD lpMaximumComponentLength;

// flags that describe the file system

DWORD dwSysFlags;

char FileSysNameBuf[FILESYSNAMEBUFSIZE];

// generic results flag

BOOL bFlag;

 

GetVolumeInformation(

(LPCWSTR)Buf,

NULL,

BUFSIZE,

NULL,

&lpMaximumComponentLength,

&dwSysFlags,

(LPWSTR)FileSysNameBuf,

FILESYSNAMEBUFSIZE

);

 

////////----------caution--------------///////

// For file system, in order the removal drives such as floppy and CD-ROM to be recognized, you must insert the media...

printf("The volume found: %S\n", Buf);

printf("The buffer for volume name: %d\n", BUFSIZE);

printf("The max component length: %d\n", lpMaximumComponentLength);

printf("The file system flag: %d\n", dwSysFlags);

printf("The file system: %S\n", FileSysNameBuf);

printf("The buffer for file system name: %d\n\n", FILESYSNAMEBUFSIZE);

bFlag = FindNextVolume(

hVol, // handle to search being conducted

(LPWSTR)Buf, // pointer to output

iBufSize // size of output buffer

);

 

return (bFlag);

}

 

// You may want to try the wmain() version

int main(void)

{

// buffer for unique volume identifiers

char buf[BUFSIZE];

// handle for the volume scan

HANDLE hVol;

BOOL bFlag;

 

// Open a search for volumes.

hVol = FindFirstVolume((LPWSTR)buf, BUFSIZE);

 

if(hVol == INVALID_HANDLE_VALUE)

{

printf("No volumes found!\n");

return (1);

}

bFlag = ProcessVolume(hVol, buf, BUFSIZE);

// Do the job while we have volumes to process.

while(bFlag)

{

bFlag = ProcessVolume(hVol, buf, BUFSIZE);

}

// Close out the volume search and close the handle

bFlag = FindVolumeClose(hVol);

return 0;

}

 

Output example:

 

The volume found: \\?\Volume{4039899d-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 255

The file system flag: 459007

The file system: NTFS

The buffer for file system name: 260

The volume found: \\?\Volume{4039899e-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 255

The file system flag: 459007

The file system: NTFS

The buffer for file system name: 260

The volume found: \\?\Volume{4039899f-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 255

The file system flag: 459007

The file system: NTFS

The buffer for file system name: 260

The volume found: \\?\Volume{403989a0-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 255

The file system flag: 459007

The file system: NTFS

The buffer for file system name: 260

The volume found: \\?\Volume{4039899b-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 110

The file system flag: 524293

The file system: CDFS

The buffer for file system name: 260

The volume found: \\?\Volume{4039899a-f63b-11d9-9648-806d6172696f}\

The buffer for volume name: 260

The max component length: 255

The file system flag: 6

The file system: FAT

The buffer for file system name: 260

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index