More on volume management functions - finding mount point
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: More on volume management functions
To show: Using ProcessVolume(), GetVolumeNameForVolumeMountPoint(), FindNextVolumeMountPoint(), GetVolumeInformation(),
FindVolumeMountPointClose(), FindNextVolume(), FindFirstVolume(), FindVolumeClose(), ProcessVolumeMountPoint() functions
// For Win Xp as a target, change accordingly
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdio.h>
#define Bufsize MAX_PATH
#define FILESYSNAMEBufsize MAX_PATH
// Process each mount point found here.
// The result indicates whether there is another mount point to be searched.
// This routine prints out the path to a mount point and its target.
BOOL ProcessVolumeMountPoint(HANDLE hPt, char *PtBuffer, DWORD dwPtBufsize, char *Buffer, DWORD dwBufsize)
{
BOOL bFlag; // Boolean result
char Path[Bufsize]; // construct a complete path here
char Target[Bufsize]; // target of mount at mount point
printf("Volume mount point found is \"%S\"\n", PtBuffer);
// Detect the volume mounted there. Build a unique path to the mount point strcpy(Path, Buffer);
strcpy_s(Path, Bufsize, Buffer);
// strcat(Path, PtBuffer);
strcat_s(Path, Bufsize, PtBuffer);
bFlag = GetVolumeNameForVolumeMountPoint(
(LPCWSTR)Path, // input volume mount point or directory
(LPWSTR)Target, // output volume name buffer
Bufsize // size of volume name buffer
);
if(!bFlag)
printf("Attempt to get volume name for %S failed.\n", Path);
else
printf("Target of the volume mount point is %S.\n\n", Target);
// Now, either get the next mount point and return it, or return a value indicating there are no more mount points.
bFlag = FindNextVolumeMountPoint(
hPt, // handle to scan
(LPWSTR)PtBuffer, // pointer to output string
dwPtBufsize // size of output buffer
);
return (bFlag);
}
// Process each volume. The Boolean result indicates whether there is another volume to be searched.
BOOL ProcessVolume(HANDLE hVol, char *Buffer, int iBufsize)
{
BOOL bFlag; // generic results flag for return
HANDLE hPt; // handle for mount point scan
char PtBuffer[Bufsize]; // string buffer for mount points
DWORD dwSysFlags; // flags that describe the file system
char FileSysNameBuffer[FILESYSNAMEBufsize];
printf("Volume found is \"%S\".\n", Buffer);
// Is this volume NTFS or other?
GetVolumeInformation((LPCWSTR)Buffer, NULL, 0, NULL, NULL, &dwSysFlags, (LPWSTR)FileSysNameBuffer, 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 file system is %S\n", FileSysNameBuffer);
// Detect support for reparse points, and therefore for volume
// mount points, which are implemented using reparse points.
if(!(dwSysFlags & FILE_SUPPORTS_REPARSE_POINTS))
{
printf("This file system does not support volume mount points.\n\n");
}
else
{
// Start processing mount points on this volume.
hPt = FindFirstVolumeMountPoint(
(LPCWSTR)Buffer, // root path of volume to be scanned
(LPWSTR)PtBuffer, // pointer to output string
Bufsize // size of output buffer
);
if(hPt == INVALID_HANDLE_VALUE)
{printf("No volume mount points found!\n\n");}
else
{
// Process the volume mount point.
bFlag = ProcessVolumeMountPoint(hPt, PtBuffer, Bufsize, Buffer, Bufsize);
// Do while we have volume mount points to process.
while (bFlag)
bFlag = ProcessVolumeMountPoint(hPt, PtBuffer, Bufsize, Buffer, Bufsize);
FindVolumeMountPointClose(hPt);
}
}
// Stop processing mount points on this volume.
bFlag = FindNextVolume(
hVol, // handle to scan being conducted
(LPWSTR)Buffer, // pointer to output
iBufsize // size of output buffer
);
return (bFlag);
}
// You may want to try wmain() version
int main(void)
{
char Buffer[Bufsize]; // Buffer for unique volume identifiers
HANDLE hVol; // handle for the volume scan
BOOL bFlag; // generic results flag
// Open a search for volumes.
hVol = FindFirstVolume((LPWSTR)Buffer, Bufsize);
if(hVol == INVALID_HANDLE_VALUE)
{
printf("No volumes found!\n");
return (-1);
}
bFlag = ProcessVolume(hVol, Buffer, Bufsize);
// Do while we have volumes to process.
while(bFlag)
{bFlag = ProcessVolume(hVol, Buffer, Bufsize);}
// Close out the volume scan.
// handle to be closed
bFlag = FindVolumeClose(hVol);
return (bFlag);
}
Output example:
Volume found is "\\?\Volume{4039899d-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{4039899e-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{4039899f-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{403989a0-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{4039899b-f63b-11d9-9648-806d6172696f}\".
The file system is CDFS
This file system does not support volume mount points.
Volume found is "\\?\Volume{4039899a-f63b-11d9-9648-806d6172696f}\".
The file system is FAT
This file system does not support volume mount points.
Press any key to continue . . .
Next, try mounting a floppy/CDROM/RW drive or a folder to a folder by using the next program example. Re-run the program.
The output with mounted CDRW at C:\mymount\ folder (the next program example) is shown below.
Volume found is "\\?\Volume{4039899d-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
Volume mount point found is "mymount\"
Attempt to get volume name for Volume found is "\\?\Volume{4039899e-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{4039899f-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{403989a0-f63b-11d9-9648-806d6172696f}\".
The file system is NTFS
No volume mount points found!
Volume found is "\\?\Volume{4039899b-f63b-11d9-9648-806d6172696f}\".
The file system is CDFS
This file system does not support volume mount points.
Volume found is "\\?\Volume{4039899a-f63b-11d9-9648-806d6172696f}\".
The file system is FAT
This file system does not support volume mount points.
Press any key to continue . . .