Mapping USB storage device's VID PID to PhysicalDrive number using Device Installation Functions

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,<br/> <br/> I have seen this question asked before but never found a concrete solution. I need to find a USB mass storage device using its VID PID and then get a handle to its PhysicalDrive number.<br/> <br/> Here is what I got so far:<br/> <br/> - First I get device information set<br/> <br/> hDevInfo = SetupDiGetClassDevs(<br/> &GUID_DEVINTERFACE_USB_DEVICE,<br/> NULL,<br/> NULL,<br/> (DIGCF_PRESENT | DIGCF_INTERFACEDEVICE));<br/> <br/> - Then I iterate through each device and get SP_DEVICE_INTERFACE_DATA<br/> <br/> SetupDiEnumDeviceInterfaces(<br/> hDevInfo,<br/> NULL,<br/> &GUID_DEVINTERFACE_USB_DEVICE,<br/> ii,<br/> &DevInterfaceData);<br/> <br/> - Using SP_DEVICE_INTERFACE_DATA I get SP_DEVICE_INTERFACE_DETAIL_DATA, which will have the path for the device, and SP_DEVINFO_DATA<br/> <br/> typedef struct DEVICE_INTERFACE_DETAIL_WITH_PATH_BUFFER_<br/> {<br/> SP_DEVICE_INTERFACE_DETAIL_DATA Detail;<br/> char Buffer[256];<br/> } DEVICE_INTERFACE_DETAIL_WITH_PATH_BUFFER, *PDEVICE_INTERFACE_DETAIL_WITH_PATH_BUFFER;<br/> <br/> SetupDiGetDeviceInterfaceDetail(<br/> hDevInfo,<br/> &DevInterfaceData,<br/> &didwpb.Detail,<br/> sizeof(didwpb),<br/> &RequiredSize,<br/> &DevInfoData);<br/> <br/> - I can then use DevInfoData to get device instance ID, which will have USB VID PID in it<br/> <br/> SetupDiGetDeviceInstanceId(<br/> hDevInfo,<br/> &DevInfoData,<br/> DevIdent,<br/> sizeof(DevIdent),<br/> &RequiredSize);<br/> <br/> - Once I parse USB VID PID from the instance ID I thought I could open the device using the path in SP_DEVICE_INTERFACE_DETAIL_DATA and issue IOCTL_STORAGE_GET_DEVICE_NUMBER to it like so:<br/> <br/> STORAGE_DEVICE_NUMBER DevNum;<br/> <br/> CreateFile(<br/> didwpb.Detail.DevicePath,<br/> (GENERIC_WRITE | GENERIC_READ),<br/> (FILE_SHARE_WRITE | FILE_SHARE_READ),<br/> NULL,<br/> OPEN_EXISTING,<br/> 0,<br/> NULL);<br/> <br/> DeviceIoControl(<br/> hDiskLocal,<br/> IOCTL_STORAGE_GET_DEVICE_NUMBER,<br/> NULL,<br/> 0,<br/> &DevNum,<br/> sizeof(DevNum),<br/> &Returned,<br/> FALSE);<br/> <br/> CloseHandle(hDiskLocal);<br/> <br/> .... but turns out you cant do that since my path is to a USB device but not to a storage device.<br/> <br/> Question: once I get the device instance ID of the USB device what can I use to match that to its volume interface so that I can get the volumes path and issue my IOCTL to get the device number?<br/> <br/> Thank you in advance.<br/> <br/> -- ilya<br/>

View the full article
 
Back
Top