码迷,mamicode.com
首页 > Windows程序 > 详细

学习API使用FindFirstVolume系列函数遍历驱动器,获取驱动器信息

时间:2016-05-03 20:28:54      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#define  BUFSIZE MAX_PATH

BOOL GetDirverInfo(LPSTR szDrive);

int main(void)
{
TCHAR buf[BUFSIZE];
HANDLE hVol;
BOOL bFlag;
/**********************************
* FindFirstVolume(buf,BUFSIZE)
* 功能:查找主机中的第一个驱动器
* 参数:buf,BUFSIZE
* buf 指向驱动器名的内存缓存区
* BUFSIZE 参数buf所指向的缓存区大小
* 返回值 HANDLE :驱动器设备名
**********************************/
hVol = FindFirstVolume(buf,BUFSIZE);

if (hVol == INVALID_HANDLE_VALUE)
{
printf(TEXT("No volmes found!\n"));
return (-1);
}
GetDirverInfo(buf);
while(FindNextVolume(hVol,buf,BUFSIZE))
{
GetDirverInfo(buf);
}
bFlag = FindVolumeClose(hVol);
char ch;
ch = getchar();
return (bFlag);
}

//GetDirverInfo函数

BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDiveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
CHAR szFileSystemNameBuffer[BUFSIZE];
CHAR szDirverName[MAX_PATH];
printf("\n%s\n",szDrive);
/*********************
* UINT WINAPI GetDriveType(LPSTR szDrive);
* 功能:判断一个磁盘驱动器的类型
* 参数:LPSTR szDrive
* 指明要获取属性的驱动器的根目录,如C:\
* 返回值 UINT:表示磁盘类型
**********************/
uDiveType = GetDriveType(szDrive);
switch(uDiveType)
{
case DRIVE_UNKNOWN://未知磁盘类型
printf("The drive type cannot be determined.");
break;
case DRIVE_NO_ROOT_DIR://说明参数是无效
printf("The root path is invalid,for example,no volume is mounted at the path.");
break;
case DRIVE_REMOVABLE://可移动磁盘
printf("The drive is a type that has removable media,for example,\
a floppy drive or removable hard disk.");
break;
case DRIVE_FIXED://固定磁盘
printf("The drive is a type that cannot be removed,for example,a fixed\
hard drive.");
break;
case DRIVE_REMOTE://网络磁盘
printf("The drive is a remote (network) drive");
break;
case DRIVE_CDROM://光驱
printf("The drive is a CD-ROM drive.");
break;
case DRIVE_RAMDISK://RAM
printf("The drive is a RAM disk.");
break;
default:
break;
}
/***********************************
*GetVolumeInformation(szDrive,szDirverName,MAX_PATH,
&dwVolumeSerialNumber,&dwMaximumComponentLength,
&dwFileSystemFlags,szFileSystemNameBuffer,BUFSIZE)
* 功能:获取磁盘驱动器的信息
* 参数:如下
* 返回值:非零即成功
***********************************/
if (!GetVolumeInformation(szDrive,
szDirverName,//卷标(字符串)
MAX_PATH,//szDirveName的长度
&dwVolumeSerialNumber,//用于装载磁盘卷序列号的变量
&dwMaximumComponentLength,//用于装载文件名每一部分的长度
&dwFileSystemFlags,//用于装载一个或多个二进制位标志的变量
szFileSystemNameBuffer,//系统类型
BUFSIZE))
{
return FALSE;
}
if (0!=lstrlen(szDirverName))
{
printf("\nDrive Name is %s\n",szDirverName);
}
printf("\nVolume Serial Number is %u",dwVolumeSerialNumber);
printf("\nMaximun Component Length is %u",dwMaximumComponentLength);
printf("\nSystem Type is %s\n",szFileSystemNameBuffer);
if (dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf("The file system does not support volume mount points.\n");
}
if (dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf("The file system supports disk quotas.\n");
}
if (dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf("The file system supports case-sensitive file names.\n");
}
printf("...\n");
return TRUE;
}

学习API使用FindFirstVolume系列函数遍历驱动器,获取驱动器信息

标签:

原文地址:http://www.cnblogs.com/zjf94/p/5456284.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!