上下文:我的应用程序用USB、CD和DVD写数据。我使用RegisterDeviceNotification来检测设备changes.To,确保连接的设备是基于DeviceIoControl的存储设备,我使用的是DeviceIoControl api。
问题:现在我需要识别USB设备中的存储设备。在测试过程中,我发现基于USB的CD/DVD也被逻辑检测为USB海量存储设备。我增加了设备类型的检查。但是我没有看到任何用于USB大容量存储的SCSI设备类型。
请建议我一个很好的解决方案,以唯一确定USB海量存储设备。
bool IsUsbStorageDevice( wchar_t letter )
{
wchar_t volumeAccessPath[] = L"\\\\.\\X:";
volumeAccessPath[4] = letter;
HANDLE deviceHandle = CreateFileW(
volumeAccessPath,
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
// setup query
STORAGE_PROPERTY_QUERY query;
memset(&query, 0, sizeof(query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
// issue query
DWORD bytes;
STORAGE_DEVICE_DESCRIPTOR devd;
STORAGE_BUS_TYPE busType = BusTypeUnknown;
bool usbcdrom = false;
if (DeviceIoControl(deviceHandle,
IOCTL_STORAGE_QUERY_PROPERTY,
&query, sizeof(query),
&devd, sizeof(devd),
&bytes, NULL))
{
busType = devd.BusType;
usbcdrom = devd.DeviceType == 0x005;
}
CloseHandle(deviceHandle);
return (BusTypeUsb == busType) && !usbcdrom;
}发布于 2014-06-03 12:36:48
您必须使用L"\\.\PhysicalDriveN"而不是L"\\.\X:"
其中,将N从0更改为29:
PhysicalDrive0
PhysicalDrive1
..。
PhysicalDrive29
发布于 2014-06-26 14:17:07
GetDriveType(root)提供驱动器类型,作为可移动的、固定的、cdrom和其他一些类型:
wchar_t rootPath[] = L"X:\\";
rootPath[0] = letter;
DWORD DriveType = GetDriveType( rootPath );
switch ( DriveType ) {
case DRIVE_CDROM:
// CD/DVD/BR drive
break;
case DRIVE_REMOVABLE:
// most flash drives, card readers
break;
case DRIVE_FIXED:
// some flash drives, hard drives
break;
default:
// never seen for USB drives
break;
}https://stackoverflow.com/questions/23988310
复制相似问题