和楼主遇到同样的问题,郁闷中。有篇帖子里给出了VC的代码解决这个问题。
试着改了下,不过一直报错,等待高人。
另外楼主读取硬盘数,如果及机器上挂的不止一块硬盘的话不依然有问题吗
4.区分移动硬盘和硬盘
我说了,移动硬盘也是DRIVE_FIXED ,真够bt的,这个没做过的话,很难想像的,太bt了
那怎么区分
用DeviceIoControl对卷下IOCTL_STORAGE_QUERY_PROPERTY进行获取信息
取返回STORAGE_DEVICE_DESCRIPTOR结构里面的STORAGE_BUS_TYPE
代码
#include <dbt.h>
#include <winioctl.h>
// IOCTL control code
#define IOCTL_STORAGE_QUERY_PROPERTY CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)
typedef enum _STORAGE_PROPERTY_ID {
StorageDeviceProperty = 0,
StorageAdapterProperty,
StorageDeviceIdProperty
} STORAGE_PROPERTY_ID, *PSTORAGE_PROPERTY_ID;
typedef enum _STORAGE_QUERY_TYPE {
PropertyStandardQuery = 0,
PropertyExistsQuery,
PropertyMaskQuery,
PropertyQueryMaxDefined
} STORAGE_QUERY_TYPE, *PSTORAGE_QUERY_TYPE;
typedef struct _STORAGE_PROPERTY_QUERY {
STORAGE_PROPERTY_ID PropertyId;
STORAGE_QUERY_TYPE QueryType;
UCHAR AdditionalParameters[1];
} STORAGE_PROPERTY_QUERY, *PSTORAGE_PROPERTY_QUERY;
typedef struct _STORAGE_DEVICE_DESCRIPTOR {
ULONG Version;
ULONG Size;
UCHAR DeviceType;
UCHAR DeviceTypeModifier;
BOOLEAN RemovableMedia;
BOOLEAN CommandQueueing;
ULONG VendorIdOffset;
ULONG ProductIdOffset;
ULONG ProductRevisionOffset;
ULONG SerialNumberOffset;
STORAGE_BUS_TYPE BusType;
ULONG RawPropertiesLength;
UCHAR RawDeviceProperties[1];
} STORAGE_DEVICE_DESCRIPTOR, *PSTORAGE_DEVICE_DESCRIPTOR;
HANDLE hDevice; // 设备句柄
BOOL bResult; // DeviceIoControl的返回结果
// 打开设备
hDevice = ::CreateFile(szPath, // 文件名
GENERIC_READ, // 软驱需要读盘
FILE_SHARE_READ | FILE_SHARE_WRITE, // 共享方式
NULL, // 默认的安全描述符
OPEN_EXISTING, // 创建方式
0, // 不需设置文件属性
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
return FALSE;
}
STORAGE_PROPERTY_QUERY Query; // input param for query
DWORD dwOutBytes; // IOCTL output length
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
STORAGE_DEVICE_DESCRIPTOR pDevDesc;
pDevDesc.Size = sizeof(STORAGE_DEVICE_DESCRIPTOR);
// 用 IOCTL_STORAGE_QUERY_PROPERTY
bResult = :
eviceIoControl(hDevice, // device handle
IOCTL_STORAGE_QUERY_PROPERTY, // info of device property
&Query, sizeof(STORAGE_PROPERTY_QUERY), // input data buffer
&pDevDesc, pDevDesc.Size, // output data buffer
&dwOutBytes, // out's length
(LPOVERLAPPED)NULL);
UINT Type = pDevDesc.BusType;
// Unknown 0x00
// SCSI 0x01
// ATAPI 0x02
// ATA 0x03
// IEEE1394 0x04
// SSA(Serial storage architecture) 0x05
// Fibre Channel, 0x06
// USB, 0x07
// RAID, 0x08
这样,就能区分USB硬盘和普通硬盘了