Getting device path for CreateFile while enumerating devices with SetupDiEnumDeviceInfo

本小妞迷上赌 提交于 2019-12-11 12:20:07

问题


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...

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.

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?


回答1:


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
)

In the descriptor, you could use the "BusType" and check for USB.



来源:https://stackoverflow.com/questions/21526233/getting-device-path-for-createfile-while-enumerating-devices-with-setupdienumdev

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!