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

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


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).


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.