说明:
有时候我们在做设备监控的时候,要识别一些链接设备,在使用函数GetDriveType的时候,U盘可以返回DRIVE_REMOVABLE,而本地硬盘硬盘和移动硬盘DRIVE_FIXED,因此还需要进一步来识别。识别方法为判断设备的总线类型即可:需要用到的数据类型以及函数有:

//将所有的卷存储在buffer中,格式为:C:/NULLD:/NULLE:/NULL
DWORD GetLogicalDriveStrings( 
      DWORD nBufferLength,  // size of buffer
      LPTSTR lpBuffer       // drive strings buffer);

//判断设备类型:
UINT GetDriveType( LPCTSTR lpRootPathName // root directory);
返回值有:
#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 //DeviceIoControl要查询的类型
typedef struct _STORAGE_PROPERTY_QUERY { // ID of the property being retrieved
STORAGE_PROPERTY_ID PropertyId;//使用前要对此字段赋值 // Flags indicating the type of query being performed
STORAGE_QUERY_TYPE QueryType; //使用前要对此字段赋值 // Space for additional parameters if necessary
BYTE AdditionalParameters[1]; } STORAGE_PROPERTY_QUERY, *PSTORAGE_PROPERTY_QUERY; // STORAGE_PROPERTY_ID枚举类型
typedef enum _STORAGE_PROPERTY_ID {
StorageDeviceProperty = 0,
StorageAdapterProperty,
StorageDeviceIdProperty,
StorageDeviceUniqueIdProperty, // See storduid.h for details
StorageDeviceWriteCacheProperty,
StorageMiniportProperty,
StorageAccessAlignmentProperty,
StorageDeviceSeekPenaltyProperty,
StorageDeviceTrimProperty,
StorageDeviceWriteAggregationProperty,
StorageDeviceDeviceTelemetryProperty,
StorageDeviceLBProvisioningProperty,
StorageDevicePowerProperty,
StorageDeviceCopyOffloadProperty,
StorageDeviceResiliencyProperty,
StorageDeviceMediumProductType,
} STORAGE_PROPERTY_ID, *PSTORAGE_PROPERTY_ID; //STORAGE_QUERY_TYPE枚举
typedef enum _STORAGE_QUERY_TYPE {
PropertyStandardQuery = 0, // Retrieves the descriptor
PropertyExistsQuery, // Used to test whether the descriptor is supported
PropertyMaskQuery, // Used to retrieve a mask of writeable fields in the descriptor
PropertyQueryMaxDefined // use to validate the value
} STORAGE_QUERY_TYPE, *PSTORAGE_QUERY_TYPE; //我们的输出结构: typedef _Struct_size_bytes_(Size) struct _STORAGE_DEVICE_DESCRIPTOR { // Sizeof(STORAGE_DEVICE_DESCRIPTOR)
DWORD Version;
// Total size of the descriptor, including the space for additional data and id strings
DWORD Size;
// The SCSI-2 device type
BYTE DeviceType;
// The SCSI-2 device type modifier (if any) - this may be zero
BYTE DeviceTypeModifier;
// Flag indicating whether the device's media (if any) is removable. This
// field should be ignored for media-less devices
BOOLEAN RemovableMedia;
// Flag indicating whether the device can support mulitple outstanding
// commands. The actual synchronization in this case is the responsibility of the port driver.
BOOLEAN CommandQueueing;
// Byte offset to the zero-terminated ascii string containing the device's
// vendor id string. For devices with no such ID this will be zero
DWORD VendorIdOffset;
// Byte offset to the zero-terminated ascii string containing the device's
// product id string. For devices with no such ID this will be zero
DWORD ProductIdOffset;
// Byte offset to the zero-terminated ascii string containing the device's
// product revision string. For devices with no such string this will be zero
DWORD ProductRevisionOffset;
// Byte offset to the zero-terminated ascii string containing the device's
// serial number. For devices with no serial number this will be zero
DWORD SerialNumberOffset;
// Contains the bus type (as defined above) of the device. It should be
// used to interpret the raw device properties at the end of this structure
// (if any)
STORAGE_BUS_TYPE BusType;
// The number of bytes of bus-specific data which have been appended to this descriptor
DWORD RawPropertiesLength;
// Place holder for the first byte of the bus specific property data
BYTE RawDeviceProperties[1];
} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR; //最后要根据BusType字段来判断 看一些这个类型:
typedef enum _STORAGE_BUS_TYPE {
BusTypeUnknown = 0x00,
BusTypeScsi,
BusTypeAtapi,
BusTypeAta,
BusType1394,
BusTypeSsa,
BusTypeFibre,
BusTypeUsb,//这个就是移动硬盘了
BusTypeRAID,
BusTypeiScsi,
BusTypeSas,
BusTypeSata,
BusTypeSd,
BusTypeMmc,
BusTypeVirtual,
BusTypeFileBackedVirtual,
BusTypeSpaces,
BusTypeNvme,
BusTypeMax,
BusTypeMaxReserved = 0x7F
} STORAGE_BUS_TYPE, *PSTORAGE_BUS_TYPE; //下面附一段代码测试: int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwReturnLen = 0;
PWCHAR pDrive = NULL;
WCHAR wszDrive[MAX_PATH] = { 0 };
dwReturnLen = GetLogicalDriveStringsW(MAX_PATH, wszDrive);
//WCHAR wszName[MAX_PATH] = { 0 };
HANDLE hDevice = NULL; if (dwReturnLen < MAX_PATH)
{
pDrive = wszDrive; while (*pDrive)
{
UINT uType = 0; uType = GetDriveTypeW(pDrive);
if (uType == DRIVE_REMOVABLE)
{
printf("%ws:U盘.\n", pDrive);
}
else if (uType == DRIVE_CDROM)
{
printf("%ws:光驱.\n", pDrive);
} else if (uType == DRIVE_FIXED)
{
//进一步判断移动硬盘或者本地硬盘 wstring wstrName(L"\\\\.\\");
wstrName.append(pDrive, 2); /*经测试\\.\C:\这种格式打不开, *如换成\\.\C:就可以打开了 */ hDevice = CreateFileW(
wstrName.c_str(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == hDevice)
{
printf("createFile error code:(0x%08x).\n", GetLastError());
getchar();
return 0;
}
//输入查询参数
STORAGE_PROPERTY_QUERY PropertyQuery;
//初始化PropertyQuery
PropertyQuery.PropertyId = StorageDeviceProperty;
PropertyQuery.QueryType = PropertyStandardQuery; BOOL bResult;
DWORD dwBytesReturned = 0;
//查询结果结构
STORAGE_DEVICE_DESCRIPTOR deviceDescriptor = { 0 };
deviceDescriptor.Size = sizeof(STORAGE_DEVICE_DESCRIPTOR);
//STORAGE_BUS_TYPE
bResult = DeviceIoControl(
hDevice,
IOCTL_STORAGE_QUERY_PROPERTY, //系统内置控制码
&PropertyQuery,
sizeof(STORAGE_PROPERTY_QUERY),
&deviceDescriptor,
deviceDescriptor.Size,
&dwBytesReturned,
NULL);
if (bResult)
{
//依据总线类型来判断
//printf("device:%ws storage bus type:%d \n", pDrive, deviceDescriptor.BusType);
if (BusTypeUsb == deviceDescriptor.BusType)
printf("%ws:移动硬盘 \n", pDrive);
else //这里还可以再进一步详细划分 printf("%ws:本地硬盘 \n", pDrive);
}
CloseHandle(hDevice);
hDevice = NULL;
}
else //if (uType == DRIVE_UNKNOWN)
{
printf("%ws:未知.\n", pDrive);
}
pDrive += 4;
}
} getchar();
return 0;
}

测试结果:

说明:这里的结果是我自己的电脑上插了两个U盘,分别是8G和16G的,还有一个移动硬盘,自带一个光驱,安装了一个虚拟光驱。可以都能正确的识别出来

参考链接:

http://blog.sina.com.cn/s/blog_56f9f160010009gp.html

http://blog.csdn.net/sloan6/article/details/7874554

vc 识别移动硬盘 U盘,本地硬盘的更多相关文章

  1. (免量产,免格式化)手动将PE安装到移动硬盘/U盘或无系统硬盘!

    在一台没有装系统的电脑上,只要把XP系统启动文件,及引导菜单文件(ntldr,boot.ini,”bootfont.bin这个可有可无,主要作用是显示中文菜单”)和GRUB引导文件和PE系统文件复制到 ...

  2. 使用MBROSTool 工具制作本地硬盘多启动盘的方法总结

    前段时间写了一个自用五合一多启动盘分享--分别用来维护娱乐,wifi密码破解,win&mac登陆密码绕过/清除,反馈的同学还是挺多,觉得大家都有这方面的需求,于是再把自己的使用经验总结一下. ...

  3. 移动硬盘/U盘装Windows 7旗舰版(VHD版)

    真正的移动版WIN7,在移动硬盘/U盘上运行的WIN7 工具准备 - 联想Y450本本,已安装Windows 7旗舰版(或者WINPE3.0版),用来给移动WIN7做引导 -Win7.vhd,15G, ...

  4. 使用MBROSTool 工具制作本地硬盘F3救急模式的方法总结

    前面写了一篇使用MBROSTool 工具制作本地硬盘多启动盘的方法总结.里面就是可以把一些系统安装到硬盘上面方便使用,比如安装PE到硬盘,不过启动的时候会先进入多UDm菜单,然后选择[启动本地系统]后 ...

  5. Mac通过type-c接口无法识别移动硬盘

      最近买了一块移动硬盘以及硬盘盒连接到Mac上来使用,最近发现了一个问题,就是当我使用完后将硬盘推出第二天再次连接上的时候硬盘不能被识别了,连硬盘和上的数据指示灯也不会亮,但是连接的鼠标键盘却没问题 ...

  6. 移动硬盘/U盘上装Windows 7旗舰版(VHD版)

    真正的移动版WIN7,在移动硬盘/U盘上运行的WIN7 工具准备 - 联想Y450本本,已安装Windows 7旗舰版(或者WINPE3.0版),用来给移动WIN7做引导 -Win7.vhd,15G, ...

  7. Linux下挂载NTFS格式的U盘或硬盘

    我们知道在Linux下挂载fat32的U盘非常容易,使用mount /dev/drive_name /mnt/指定目录这样就可以挂载了,但是如果U盘或者硬盘的格式是NTFS的话,那么Linux是不能识 ...

  8. 如何将win7安装到 移动硬盘/U盘 及 VHD、BCD等相关知识 链接汇总

    如何将win7安装到 移动硬盘/U盘? 从u盘启动win7/2008 R2的方法,更新:换机器免sysprep的方法:http://www.360doc.com/content/11/1103/14/ ...

  9. Openwrt 安装软件到U盘或硬盘

    http://blog.licess.org/openwrt-install-software-to-udisk-harddisk/ 运行一个多月的DDNAS被结婚来玩的小孩给关了,于是趁机更新了一下 ...

随机推荐

  1. NX二次开发-UFUN获取对象的显示属性(图层,颜色,空白状态,线宽,字体,高亮状态)UF_OBJ_ask_display_properties

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> #include <uf_obj.h> UF_initialize( ...

  2. NX二次开发-UFUN工程图表格注释设置单元格首选项UF_TABNOT_set_cell_prefs

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  3. go beego

    一. 引入 go get github.com/astaxie/beego go get gitgub.com/beego/bee go get -u gitxxx.... 更新框架 编写 packa ...

  4. Day 13 : 函数递归,

    从前有有座山,山里有座庙,庙里有个老和尚给小和尚们讲故事,讲的什么呀,讲的是,从前有有座山,山里有座庙,庙里有个老和尚给小和尚们讲故事,讲的什么呀?讲的是?...... 递归:1.一个函数再内部调用了 ...

  5. USACO 2011 November Cow Lineup /// map set 尺取法 oj25279

    题目大意: 输入n 接下来n行描述n头牛的编号num和品种id 得到包含所有id的最短段 输出最短段的编号差 Sample Input 625 726 115 122 320 130 1 Sample ...

  6. neo4j 基本概念和Cypher语句总结

    下面是一个介绍基本概念的例子,参考链接Graph database concepts: (1) Nodes(节点) 图谱的基本单位主要是节点和关系,他们都可以包含属性,一个节点就是一行数据,一个关系也 ...

  7. Asp.net MVC使用EasyNetQ操作RabbitMQ

    Demo下载地址:https://download.csdn.net/download/u010312811/11259742 .Net下操作RabbitMQ最常用的SDK是RabbitMQ.Clie ...

  8. asprise-ocr-api-sample 高价收破解版64 32位

    asprise-ocr-api-sample验证码 高价收破解版64 32位 Reflector 8.5 打开自己的C#代码  完全100%的反编译了

  9. AOP与IOC的概念(即spring的核心)

    a) IOC:Spring是开源框架,使用框架可以使我们减少工作量,提高工作效率并且它是分层结构,即相对应的层处理对应的业务逻辑,减少代码的耦合度.而spring的核心是IOC控制反转和AOP面向切面 ...

  10. java String,StringBuilder和StringBuffer类的区别

    对String而言: String是只读字符串,所引用的字符串不能被改变,一经定义,无法再增删改. String 定义的字符串保存在常量池里面,进行+操作时不能直接在原有基础上拼接. 每次+操作 : ...