Windows does not mount drives; he mounts the volume. However, the size of a USBSTOR device is not specified as a child of a node in the device tree. Thus, you need to list all volumes and perform a bunch of manipulations and string comparisons to combine the STOP / VOLUME storage nodes with the USBSTOR nodes.
All volume GUIDs are enumerated using the FindFirstVolume function set. Leading characters "\. \" And trailing "\" can be removed, and the resulting string is then passed to QueryDosDevice . This gives the name of the device.
Next, you need to list all volumes using GUID_DEVINTERFACE_VOLUME with SetupDiGetClassDevs and friends. Compare the device type and number of each volume with the USBSTOR device you are looking for using IOCTL_STORAGE_GET_DEVICE_NUMBER . After reconciliation, you can get the device name from the volume and compare it with another list of device names to find the GUID of the volume.
Finally, the volume GUID can be used successfully with SetVolumeMountPoint .
Thanks to Gabe for his very helpful help in commenting on my question.
Code snippets
Get the type and number of the device from the device path:
STORAGE_DEVICE_NUMBER sdn; HANDLE handle = CreateFile(devInterfaceDetail->DevicePath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, NULL); DWORD len = 0; DeviceIoControl(h, IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &sdn, sizeof (sdn), &len, NULL);
Find the device name for the corresponding USBSTOR instance by iterating over all volume interfaces and comparing the disk number with the above snippet:
std::string deviceName; HDEVINFO devInfoSet = SetupDiGetClassDevs(&GUID_DEVINTERFACE_VOLUME, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); SP_DEVICE_INTERFACE_DATA devInterface = { 0 }; devInterface.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA); for (int i = 0; SetupDiEnumDeviceInterfaces(devInfoSet, NULL, &GUID_DEVINTERFACE_VOLUME, i, &devInterface); ++i) { SP_DEVINFO_DATA devInfoData = { 0 }; devInfoData.cbSize = sizeof (SP_DEVINFO_DATA); DWORD len; SetupDiGetDeviceInterfaceDetail(devInfoSet, &devInterface, NULL, 0, &len, &devInfoData); std::vector<char> buf(len); SP_DEVICE_INTERFACE_DETAIL_DATA *devInterfaceDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA *) &buf[0]; devInterfaceDetail->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA); if (SetupDiGetDeviceInterfaceDetail(devInfoSet, &devInterface, devInterfaceDetail, len, NULL, &devInfoData)) { if (DEVICE_NUMBER == this->getDeviceNumber(devInterfaceDetail->DevicePath)) { std::vector<BYTE> buf(MAX_PATH + 1); DWORD type, len; if (SetupDiGetDeviceRegistryProperty(devInfoSet, &devInfoData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, &type, &buf[0], buf.size(), &len)) { deviceName.assign(buf.begin(), buf.begin() + len); break; } } } }