Find which drive corresponds to which USB mass storage device in WinXP
Asked Answered
A

2

10

I have several USB drives connected to a WinXP SP3 computer, and I need to tell them apart programatically - I need to find which drive letter corresponds to which device (in this case, one device ~ one volume). I can get their Volume IDs and drive letters using mountvol, looking something like this:

C:\WINDOWS\> mountvol
\\?\Volume{bdb681b2-1ddf-11dd-bf71-806d6172696f}\
    C:\

\\?\Volume{6a8784f8-7406-11dd-a8c3-001e8c829b67}\
    A:\

Also, using devcon or the Device Manager, I can see the device IDs:

C:\WINDOWS\> devcon resources *STOR*
STORAGE\REMOVABLEMEDIA\7&190C24E5&0&RM
    Name: Generic volume
STORAGE\VOLUME\1&30A96598&0&SIGNATURED84ED84EOFFSET7E00LENGTH2543150400
    Name: Generic volume
USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER2.0&REV_1.00\0803240752536&0
    Name: Kingston DataTraveler2.0 USB Device

However, I haven't found a way to link the device ID and the volume ID/letter, like the "Safely remove hardware" dialog does (therefore I assume it's possible):

Generic volume - A:
(source: piskvor.org)

As you may see, these are the same devices that I see in devcon and the same volume that mountvol sees; but so far I haven't found the link between them.

I've found some related questions, but those seem to use the approach "whatever you find first is your USB device", which is not very useful in my case, since there will be several similar devices (same vendor, often same product type) connected.


Edit:

@MSalters' answer looks promising: On XP, HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices has REG_BINARY values \DosDevices\x: (where x is [A-Z]); the comment is (UTF-16) name of the correct device (e.g.
\DosDevices\A: = "\??\STORAGE#RemovableMedia#7&190c24e5&0&RM#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}\", which corresponds to
STORAGE\REMOVABLEMEDIA\7&190C24E5&0&RM seen above in the device list).

Will see if that's the way to go.

Areaway answered 9/4, 2009 at 7:35 Comment(3)
Did you ever find a way to do this? I'm also trying to determine the mounted volume for a USB mass storage device. Thanks.Razid
@Stephen Nutt: No, not really; it was too complicated - have spent too much time digging in it, compared to the overall task; using undocumented APIs can get too messy. (In addition, changes in the overall task have mooted the need for this for me)Areaway
@Stephen Nutt: Although I have abandoned this effort in Windows, I have now found a way to do this in Linux, if you're interested: #3881949Areaway
C
5

It's a non-trivial question. There is no official API for it, as far as I can tell. So, you need an undocumented API: the registry. HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices contains entries for both drive letters and volume IDs. If you look at the actual data, you'll find that it identifies the drive. Look at the binary data as a Unicode string. It will point you to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ENUM\. However, XP and Vista differ in exactly what subkeys are referenced there, and how. (Vista is easier, so try that first).

Crosscurrent answered 9/4, 2009 at 8:5 Comment(1)
This is incorrect. There is an official API to do it. It's done using IOCTL_STORAGE_GET_DEVICE_NUMBER. This is a fair sample: codeproject.com/KB/system/RemoveDriveByLetter.aspxDelly
D
3

There is an official API to do this. See this sample: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx

The principle is that each storage volume on Windows is assigned a global number. First, you ask your drive for its number (by opening "\X:" and sending a IOCTL_STORAGE_GET_DEVICE_NUMBER request). Second, you go over all disk devices and ask each one for its number (by opening it, through a different path, so you can't just string-compare).

Once you find a disk device carrying the same number as you queried your drive, you know you have a winner.

Delly answered 21/8, 2011 at 13:5 Comment(1)
Hmmm, that link looks interesting. Could you perhaps edit a short summary into your answer? Thanks.Areaway

© 2022 - 2024 — McMap. All rights reserved.