问题描述
我需要获取有关所有连接的 USB 大容量存储设备的信息.现在我正在使用此代码来完成这项工作
I need to get info about all connected usb mass storage devices. For now I'm using this code to do the job
HDEVINFO deviceInfoList;
deviceInfoList = SetupDiGetClassDevs(NULL, _T("USBSTOR"), NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (deviceInfoList != INVALID_HANDLE_VALUE)
{
SP_DEVINFO_DATA deviceInfoData;
deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(deviceInfoList, i, &deviceInfoData); i++)
{
LPTSTR buffer = NULL;
DWORD buffersize = 0;
while (!SetupDiGetDeviceInstanceId(deviceInfoList, &deviceInfoData, buffer, buffersize, &buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
if (buffer) delete buffer;
buffer = new TCHAR[buffersize];
}
else
{
_tprintf(_T("%ls\n"), _T("error"));
break;
}
}
_tprintf(_T("%ls\n"), buffer);
if (buffer) { delete buffer; buffer = NULL; }
etc...
如您所见,我正在使用带有USBSTOR"枚举器的 SetupDiGetClassDevs 创建设备列表,然后使用 SetupDiEnumDeviceInfo 枚举它.问题是我可以在枚举中以某种方式获取 CreateFile 调用的设备路径吗?正如我所看到的,您可以使用 SetupDiGetDeviceInterfaceDetail 获得正确的路径,但为此我应该使用 SetupDiEnumDeviceInterfaces 函数枚举设备.我试图以这种方式枚举设备,但没有任何成功.在我看来,当您使用 SetupDiEnumDeviceInterfaces 枚举设备时,您应该将设备接口 GUID 传递给 SetupDiGetClassDevs,但我找不到 USB 大容量存储设备的特定设备接口.我阅读了有关 设备的 msdn 文档信息集,真的不明白什么是设备接口".
So as you can see I'm creating device list using SetupDiGetClassDevs with "USBSTOR" enumerator and then enumerating it with SetupDiEnumDeviceInfo. The question is can I somehow get device path for CreateFile call inside my enumerating? As I can see you can obtain correct path using SetupDiGetDeviceInterfaceDetail but for this I should enumerate devices using SetupDiEnumDeviceInterfaces function. I was tried to enumerate devices this way, but without any success. It seems for me that when you enumerate devices with SetupDiEnumDeviceInterfaces you should pass device interface GUID to SetupDiGetClassDevs but I can't find specific device interface for usb mass storage devices. I was read msdn docs about Device Information Sets and really don't get what "device interface" is.
最后一个问题是:我可以在使用 SetupDiEnumDeviceInfo 枚举设备时获取设备路径吗?如果没有,如何使用 SetupDiEnumDeviceInterfaces 枚举所有连接的 USB 大容量存储设备?
Final question is: can I get device path while enumerating devices using SetupDiEnumDeviceInfo? If no, how can I enumerate all connected usb mass storage devices with SetupDiEnumDeviceInterfaces?
推荐答案
您可以使用 GUID_DEVINTERFACE_DISK 枚举物理磁盘设备.使用:
You could enumerate physical disk devices using the GUID_DEVINTERFACE_DISK. Using:
SetupDiGetClassDevs
(
&GUID_DEVINTERFACE_DISK,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
)
然后,查询存储适配器描述符.
Then, query the storage adapter descriptor.
STORAGE_PROPERTY_QUERY storageProperty;
//...setup
PSTORAGE_ADAPTER_DESCRIPTOR pstorageAdapterDesc;
pstorageAdapterDesc = (PSTORAGE_ADAPTER_DESCRIPTOR)LocallAlloc( LPTR, storageDescHeader.Size );
DeviceIoControl
(
handle,
IOCTL_STORAGE_QUERY_PROPERTY,
&storageProperty,
sizeof( STORAGE_PROPERTY_QUERY ),
pstorageAdapterDesc,
storageDescHeader.Size,
bytesReturned,
NULL
)
在描述符中,您可以使用BusType"并检查 USB.
In the descriptor, you could use the "BusType" and check for USB.
这篇关于使用 SetupDiEnumDeviceInfo 枚举设备时获取 CreateFile 的设备路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!