How to get drive letter from USB VID and PID

Hello
I have written a C++ application which is able to detect multiple USB VID and PID devices upon insertion and removal by matching "USB\VID_%04X&PID_%04X". From this how am I able to find which drive letter the USB storage has mounted on?
The drive, letter D:// is a UF2, fat filesystem which I am using to flash upgrade an embedded device. On my Windows system this shows up under "Portable Devices" in "Device Manager".
Device SWD\WPDBUSENUM_??_USBSTOR#Disk&Ven_Logger&Prod_&Rev_1.00#9&319384a2&0&24842CB953324D5946202020FF170720&0#{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
If I view the properties of the D:// drive I can get the following information under properties, events:
Device USBSTOR\Disk&Ven_Logger&Prod_&Rev_1.00\9&319384a2&0&24842CB953324D5946202020FF170720&0 was configured.
Driver Name: disk.inf
Class Guid: {4d36e967-e325-11ce-bfc1-08002be10318}
Driver Date: 06/21/2006
Driver Version: 10.0.19041.789
Driver Provider: Microsoft
Driver Section: disk_install.NT
Driver Rank: 0xFF0006
Matching Device Id: GenDisk
Outranked Drivers: disk.inf:GenDisk:00FF2002
Device Updated: false
Parent Device: USB\VID_17DE&PID_00FF&MI_02\8&1d8b0748&0&0002
Any direction how I can achieve the above in C++ would grateful.
Thanks
J
PS C:\Users\jonathan.woodward> Get-WmiObject Win32_UsbHub | Select-Object DeviceID
DeviceID
USB\VID_413C&PID_2113\6&3B66B33D&0&12
USB\ROOT_HUB30\5&381F2DE&0&0
USB\VID_2516&PID_0051\6&3B66B33D&0&7
USB\ROOT_HUB30\5&3B7A03C3&0&0
USB\VID_0BDA&PID_5411\6&28B29415&0&2
USB\VID_0BDA&PID_5411\7&38623A69&0&1
USB\VID_17DE&PID_00FF\24842CB953324D5946202020FF170720
USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002
USB\VID_21A9&PID_1004\7&38623A69&0&3
USB\VID_03EB&PID_2141\J42700037974
I am wondering if it does not match for some reason.
PS C:\Users\jonathan.woodward> Get-WmiObject Win32_UsbHub -Filter 'DeviceID = "USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002"' | Select-Object DeviceID
Get-WmiObject : Invalid query "select * from Win32_UsbHub where DeviceID = "USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002""
At line:1 char:1
If you want to get drive letter(s) from this DeviceID, you can do it with WMI
I had posted a sample in VB in this thread (GetDriveLettersFromDevInstID function)
,which can be converted into C++ (CoInitializeSecurity and so on...)
@Castorix31 I tried similar in csharp
foreach (ManagementObject drive in new ManagementObjectSearcher("SELECT DeviceID FROM Win32_DiskDrive WHERE PNPDeviceID='" + childDeviceId.Replace(@"\", @"\") + "'").Get())
Dim sQuery As String = "SELECT * FROM Win32_DiskDrive where PNPDeviceID = """ + sDeviceInstanceID + """"
But this PNPDeviceID does not exist and nothing is found or matched to my USB device.
PS C:\Users\jonathan.woodward> Get-WmiObject Win32_DiskDrive -Namespace root/cimv2
Partitions : 4
DeviceID : \.\PHYSICALDRIVE0
Model : ADATA SX8200PNP
Size : 512105932800
Caption : ADATA SX8200PNP
Partitions : 1
DeviceID : \.\PHYSICALDRIVE1
Model : Logger USB Device
Size : 8192000
Caption : Logger USB Device
Partitions : 1
DeviceID : \.\PHYSICALDRIVE2
Model : KIOXIA TransMemory USB Device
Size : 31001080320
Caption : KIOXIA TransMemory USB Device
And if you do :
Is there a way I can query all these hidden properties to see how things link?
Forget that I found it
Get-WmiObject Win32_logicalDisk -Filter 'DeviceID = "D:"' |Format-List *
Sign in to comment
I did this test on my PC (in C++ as you tagged C++...) by enumerating disk drives; I get :
Test code :
Utility functions :
This solution worked with both devices, thank you for your help!
1642586082.742|default |SquirrelViewService_deviceAdded GRANT_UF2_BOOTLOADER_USB_PID dev(USB\VID_17DE&PID_00FF\24842CB953324D5946202020FF170720) isPort(0)
1642586082.742|default |SquirrelViewService_deviceAdded dev(USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002) isPort(0)
1642586082.746|default |SquirrelWebSocketCommand_devices:
1642586082.865|default |**************
DeviceName[SCSI\DISK&VEN_NVME&PROD_ADATA_SX8200PNP\5&34B7B25D&0&000000]
DeviceID[SCSI\DISK&VEN_NVME&PROD_ADATA_SX8200PNP\5&34B7B25D&0&000000]
ParentDeviceID[PCI\VEN_1CC1&DEV_8201&SUBSYS_82011CC1&REV_03\4&2B5FE750&0&0009]
wsDriveLetters[C:]
1642586082.929|default |**************
DeviceName[USBSTOR\DISK&VEN_LOGGER&PROD_&REV_1.00\9&319384A2&0&24842CB953324D5946202020FF170720&0]
DeviceID[USBSTOR\DISK&VEN_LOGGER&PROD_&REV_1.00\9&319384A2&0&24842CB953324D5946202020FF170720&0]
ParentDeviceID[USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002]
wsDriveLetters[D:]
1642586082.997|default |**************
DeviceName[USBSTOR\DISK&VEN_KIOXIA&PROD_TRANSMEMORY&REV_1.00\0022CFF653ECC411A350A2F9&0]
DeviceID[USBSTOR\DISK&VEN_KIOXIA&PROD_TRANSMEMORY&REV_1.00\0022CFF653ECC411A350A2F9&0]
ParentDeviceID[USB\VID_30DE&PID_6544\0022CFF653ECC411A350A2F9]
wsDriveLetters[F:]
Sign in to comment
3 additional answers
Sort by: Most helpful
This third party software may be useful:
https://www.nirsoft.net/utils/usb_devices_view.html
https://www.nirsoft.net/utils/usbdeview-x64.zip
.
.
.
.
.
Please remember to vote and to mark the replies as answers if they help.
On the bottom of each post there is:
Propose as answer = answered the question
On the left side of each post there is /\ with a number: click = a helpful post
.
.
.
.
.
Sign in to comment
I have 2 removable devices attached one of which I can match to a drive and the other not.
0, VID = USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002, Drive = Unknown
1, VID = USB\VID_30DE&PID_6544\0022CFF653ECC411A350A2F9, Drive = F:
PS C:\Users\jonathan.woodward> Get-WmiObject -ClassName Win32_USBHub | Select-Object DeviceID, PNPDeviceID, Description
DeviceID PNPDeviceID Description
USB\VID_17DE&PID_00FF\24842CB953324D5946202020FF170720 USB\VID_17DE&PID_00FF\24842CB953324D5946202020FF170720 USB Composite Device
USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002 USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002 USB Mass Storage Device
USB\VID_03EB&PID_2141\J42700037974 USB\VID_03EB&PID_2141\J42700037974 USB Composite Device
USB\VID_30DE&PID_6544\0022CFF653ECC411A350A2F9 USB\VID_30DE&PID_6544\0022CFF653ECC411A350A2F9 USB Mass Storage Device
PS C:\Users\jonathan.woodward> Get-WmiObject -ClassName Win32_DiskDrive | Select-Object DeviceID, PNPDeviceID, Description
DeviceID PNPDeviceID Description
\.\PHYSICALDRIVE1 USBSTOR\DISK&VEN_LOGGER&PROD_&REV_1.00\9&319384A2&0&24842CB953324D5946202020FF170720&0 Disk drive
\.\PHYSICALDRIVE2 USBSTOR\DISK&VEN_KIOXIA&PROD_TRANSMEMORY&REV_1.00\0022CFF653ECC411A350A2F9&0 Disk drive
For whatever reason VID_17DE does not show me the link the other one does however it is somehow mapped to D: drive.
This is how I get the link for the other drive:
But it returns null...
Sign in to comment
The PnPEntity is USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003 and not USB\VID_17DE&PID_00FF&MI_02\8&1D8B0748&0&0002 how can this happen?
PS C:\Users\jonathan.woodward> Get-WmiObject Win32_PnPEntity -Filter 'DeviceID = "USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003"'
__GENUS : 2
__CLASS : Win32_PnPEntity
__SUPERCLASS : CIM_LogicalDevice
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_PnPEntity.DeviceID="USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003"
__PROPERTY_COUNT : 26
__DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : GCN0185
__NAMESPACE : root\cimv2
__PATH : \GCN0185\root\cimv2:Win32_PnPEntity.DeviceID="USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003"
Availability :
Caption : USB Input Device
ClassGuid : {745a17a0-74d3-11d0-b6fe-00a0c90f57da}
CompatibleID : {USB\Class_03&SubClass_00&Prot_00, USB\Class_03&SubClass_00, USB\Class_03}
ConfigManagerErrorCode : 0
ConfigManagerUserConfig : False
CreationClassName : Win32_PnPEntity
Description : USB Input Device
DeviceID : USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003
ErrorCleared :
ErrorDescription :
HardwareID : {USB\VID_17DE&PID_00FF&REV_4201&MI_03, USB\VID_17DE&PID_00FF&MI_03}
InstallDate :
LastErrorCode :
Manufacturer : (Standard system devices)
Name : USB Input Device
PNPClass : HIDClass
PNPDeviceID : USB\VID_17DE&PID_00FF&MI_03\8&1D8B0748&0&0003
Sign in to comment
Activity