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?