Getting device path for CreateFile while enumerating devices with SetupDiEnumDeviceInfo

StackOverflow https://stackoverflow.com/questions/21526233

  •  06-10-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top