[英]Identify USB Mass storage Device
Context : My application writes data in USB ,CD & DVD. 上下文:我的应用程序将数据写入USB,CD和DVD。 I am using RegisterDeviceNotification for detect device changes.To ensure that connected device is USB based storage device i am using DeviceIoControl api.
我正在使用RegisterDeviceNotification来检测设备更改。为确保连接的设备是基于USB的存储设备,我正在使用DeviceIoControl api。
Problem : Now i need to identify Storage devices in USB devices. 问题 :现在我需要识别USB设备中的存储设备。 During testing i found USB based CD/DVD were also detected by logic as USB mass storage device.
在测试期间,我发现基于逻辑的USB USB CD / DVD也被检测为USB大容量存储设备。 I added check for Device Type.
我添加了“设备类型”检查。 But i don't see any device type in SCSI for USB mass storage.
但是我在SCSI中看不到USB大容量存储的任何设备类型。
Please suggest me a good solution to uniquely identify a USB Mass storage device. 请为我提供一个很好的解决方案,用于唯一标识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;
}
You must use L"\\\\\\\\.\\\\PhysicalDriveN" instead of L"\\\\\\\\.\\\\X:" 您必须使用L“ \\\\\\\\。\\\\ PhysicalDriveN”而不是L“ \\\\\\\\。\\\\ X:”
where you change N from 0 to 29: 将N从0更改为29:
PhysicalDrive0 物理驱动器0
PhysicalDrive1 物理驱动器1
... ...
PhysicalDrive29 物理驱动器29
GetDriveType(root) delivers the drive type, as removeable, fixed, cdrom and some others: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.