Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

windows - Find which drive corresponds to which USB mass storage device in WinXP

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*
STORAGEREMOVABLEMEDIA7&190C24E5&0&RM
    Name: Generic volume
STORAGEVOLUME1&30A96598&0&SIGNATURED84ED84EOFFSET7E00LENGTH2543150400
    Name: Generic volume
USBSTORDISK&VEN_KINGSTON&PROD_DATATRAVELER2.0&REV_1.00803240752536&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_MACHINESYSTEMMountedDevices has REG_BINARY values DosDevicesx: (where x is [A-Z]); the comment is (UTF-16) name of the correct device (e.g.
DosDevicesA: = "??STORAGE#RemovableMedia#7&190c24e5&0&RM#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}", which corresponds to
STORAGEREMOVABLEMEDIA7&190C24E5&0&RM seen above in the device list).

Will see if that's the way to go.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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_MACHINESYSTEMMountedDevices 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_MACHINESYSTEMCurrentControlSetENUM. However, XP and Vista differ in exactly what subkeys are referenced there, and how. (Vista is easier, so try that first).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...