函数定义:
Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the volumes of a computer.
HANDLE WINAPI FindFirstVolume( _Out_ LPTSTR lpszVolumeName, _In_ DWORD cchBufferLength );
说明:
检索本机卷的名字,但这个名字是GUID(全局唯一标志符)路径的,可见卷的表示方法有好多种,不仅仅是我们平时所看到的C盘,D盘,
返回值:
成功返回一个有效的句柄,最后需要用FindVolumeClose函数关掉该句柄。失败返回INVALID_HANDLE_VALUE。
接着又一个函数:
BOOL WINAPI FindNextVolume( _In_ HANDLE hFindVolume, _Out_ LPTSTR lpszVolumeName, _In_ DWORD cchBufferLength );
说明:
第一个参数就是用调用FindFirstVolume的返回句柄, 其它两个参数不多介绍了,
用这两个函数循环的遍历所有的可用卷。
下面写一个例子来输出这个GUID名称:
void main() { CHAR szVolume[MAX_PATH] = { 0 }; HANDLE hVolume = FindFirstVolumeA(szVolume,MAX_PATH); if (INVALID_HANDLE_VALUE == hVolume) return 0; //string strVolume = szVolume; printf("%s \n", szVolume); while (FindNextVolumeA(hVolume, szVolume, MAX_PATH)) { printf("%s \n", szVolume); } FindVolumeClose(hVolume);//别忘了关闭句柄 }
我的输出结果是这样的:
分析:
可见这些也代表着卷标的唯一标志,我的笔记本上是有五个驱动器,分别是C盘,D盘,E盘,F盘和CD ROM光驱H。这样也证实了这一说法。
接下来我们用GetDriveType函数拿上面的返回值测试一下看正确不。
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or
network drive. //判断卷的类型
UINT GetDriveType(
LPCTSTR lpRootPathName // root directory
);
扫描二维码关注公众号,回复:
3479425 查看本文章
void main() { UINT uGUIDType[5]; UINT uNameType[5]; int i = 0; CHAR szVolume[MAX_PATH] = { 0 }; HANDLE hVolume = FindFirstVolumeA(szVolume,MAX_PATH); if (INVALID_HANDLE_VALUE == hVolume) return 0; //string strVolume = szVolume; //printf("%s \n", szVolume); uGUIDType[i] = GetDriveTypeA(szVolume); uNameType[i] = GetDriveTypeA("C:\\"); i++; while (FindNextVolumeA(hVolume, szVolume, MAX_PATH)) { //printf("%s \n", szVolume); uGUIDType[i] = GetDriveTypeA(szVolume); i++; } uNameType[1] = GetDriveTypeA("D:\\"); uNameType[2] = GetDriveTypeA("E:\\"); uNameType[3] = GetDriveTypeA("F:\\"); uNameType[4] = GetDriveTypeA("H:\\"); CHAR szResult[MAX_PATH] = { 0 }; sprintf_s(szResult, "uGUIDType:\t%d %d %d %d %d \nuNameType:\t%d %d %d %d %d\n", uGUIDType[0], uGUIDType[1], uGUIDType[2], uGUIDType[3], uGUIDType[4], uNameType[0], uNameType[1], uNameType[2], uNameType[3], uNameType[4]); printf("%s", szResult); }
这次查看结果:
可知那些奇怪的字符 \\?\Volume{fe04a021-a8fc-11e4-824b-806e6f6e6963}\和"C:\\"是一样的。
而这些数字代表什么呢?查看MSDN可得:
#define DRIVE_UNKNOWN 0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE 2
#define DRIVE_FIXED 3
#define DRIVE_REMOTE 4
#define DRIVE_CDROM 5
#define DRIVE_RAMDISK 6