Getting device path for CreateFile while enumerating devices using SetupDiEnumDeviceInfo

I need to get information about all connected usb storage devices. So far I am 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 am creating a list of devices using SetupDiGetClassDevs with the "USBSTOR" enumerator and then enumerating it using SetupDiEnumDeviceInfo. The question is, is there some way to get the device path to call CreateFile inside my enum? As I can see, you can get the correct path using SetupDiGetDeviceInterfaceDetail, but for that I have to enumerate devices using the SetupDiEnumDeviceInterfaces function. I tried to list devices this way, but with no success. It seems to me that when enumerating devices using SetupDiEnumDeviceInterfaces, you should pass the device interface GUID to SetupDiGetClassDevs, but I cannot find a specific device interface for usb mass storage devices. I have read the msdn docs about"Device Information" and really didn't understand what "device interface" is.

Final question: can I get the device path when enumerating devices using SetupDiEnumDeviceInfo? If not, how can I list all connected usb storage devices using SetupDiEnumDeviceInterfaces?

+1


source to share


1 answer


You can enumerate physical disk devices using GUID_DEVINTERFACE_DISK. Using:

SetupDiGetClassDevs
(
    &GUID_DEVINTERFACE_DISK,
    NULL,
    NULL,
    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
)

      

Then, query for the storage adapter handle.



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 can use "BusType" and check USB.

+1


source







All Articles