0

Given a drive letter (F:), I need to get some device information on the USB device like the serial number.

From the drive letter I can get the volume device path

char path[1024];
auto size = QueryDosDevice("F:", path, 1024);
printf("path: %s\n", path);

outputs:

path: \Device\HarddiskVolume6

Working the other way, I can enumerate connected USB devices and get their path, device ids, and serial number

HDEVINFO handle = INVALID_HANDLE_VALUE;

handle = SetupDiGetClassDevs(&GUID_DEVINTERFACE_USB_DEVICE, NULL, 0,
    DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
if (handle == INVALID_HANDLE_VALUE)
{
    printf("error\n");
    return -1;
}


// Each iteration of this loop attempts to set up a new device.
DWORD index = 0;
while (true)
{
    // See if we have reached the end of the list.
    SP_DEVINFO_DATA info;
    info.cbSize = sizeof(info);
    BOOL success = SetupDiEnumDeviceInfo(handle, index, &info);
    if (!success)
    {
        if (GetLastError() == ERROR_NO_MORE_ITEMS)
        {
            // We have reached the end of the list.
            break;
        }

        // An unexpected error happened.
        printf("Failed to test for the end of the USB device list.\n");
        break;
    }


    DWORD data_type = 0;
    char path[1024];
    success = SetupDiGetDeviceRegistryProperty(handle, &info, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, &data_type, (unsigned char*)path, 1024, nullptr);
    if (!success)
    {
        printf("Failed to get device object name: %d\n", GetLastError());
        return -3;
    }
    printf("path: %s\n", path);

    char id[200];
    success = SetupDiGetDeviceInstanceId(handle, &info, id, 200, NULL);
    printf("id: %s\n", id);

    index++;
}
if (handle != INVALID_HANDLE_VALUE) { SetupDiDestroyDeviceInfoList(handle); }

outputs:

path: \Device\USBPDO-3
id: USB\VID_203A&PID_FFF9\0X8020000005AC8514
path: \Device\USBPDO-6
id: USB\VID_203A&PID_FFFC\PW3.0
path: \Device\USBPDO-4
id: USB\VID_203A&PID_FFFA\TAG21C7A8AD5
path: \Device\USBPDO-5
id: USB\VID_203A&PID_FFFA\TAG21D87ACA0
path: \Device\USBPDO-7
id: USB\VID_0781&PID_5530\20043515400CF1202B25

Is there any way to bridge the two? I haven't been able to find a way to determine which volume devices are on which physical USB device. Is this even possible?

Ken White
  • 123,280
  • 14
  • 225
  • 444
minnymauer
  • 374
  • 1
  • 4
  • 17
  • 1
    Looks like this is related question: https://stackoverflow.com/questions/3098696/get-information-about-disk-drives-result-on-windows7-32-bit-system – Renat Jan 16 '20 at 17:49
  • 1
    @Renat It looks like the example program found there has the exact information I'm looking for, thank you. – minnymauer Jan 16 '20 at 18:03
  • @Suma Yes, that's the same link Renant posted. – minnymauer Jan 16 '20 at 18:30
  • Can you accept is as a duplicate, then, so that this question is closed? – Suma Jan 16 '20 at 21:19
  • The answer very much depends on what you have at the input and what you want to receive. need more accurate statement of the problem. – RbMm Jan 16 '20 at 22:24

0 Answers0