windows 在WinXP中查找哪个驱动器对应哪个USB大容量存储设备
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/733206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find which drive corresponds to which USB mass storage device in WinXP
提问by Piskvor left the building
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:
我有几个 USB 驱动器连接到 WinXP SP3 计算机,我需要以编程方式将它们区分开来 -我需要找到对应于哪个设备的驱动器号(在这种情况下,一个设备 ~ 一个卷)。我可以使用 获取他们的卷 ID 和驱动器号mountvol
,看起来像这样:
C:\WINDOWS\> mountvol
\?\Volume{bdb681b2-1ddf-11dd-bf71-806d6172696f}\
C:\
\?\Volume{6a8784f8-7406-11dd-a8c3-001e8c829b67}\
A:\
Also, using devconor the Device Manager, I can see the device IDs:
此外,使用devcon或设备管理器,我可以看到设备 ID:
C:\WINDOWS\> devcon resources *STOR*
STORAGE\REMOVABLEMEDIA&190C24E5&0&RM
Name: Generic volume
STORAGE\VOLUME&30A96598&0&SIGNATURED84ED84EOFFSET7E00LENGTH2543150400
Name: Generic volume
USBSTOR\DISK&VEN_KINGSTON&PROD_DATATRAVELER2.0&REV_1.0003240752536&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):
但是,我还没有找到链接设备 ID 和卷 ID/字母的方法,就像“安全删除硬件”对话框那样(因此我认为这是可能的):
(source: piskvor.org)
(来源: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.
如您所见,这些设备与我在 devcon 中看到的设备和 mountvol 看到的卷相同;但到目前为止我还没有找到它们之间的联系。
I've found some relatedquestions, 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.
我发现了一些相关的问题,但那些似乎使用了“无论你首先找到的是你的 USB 设备”的方法,这对我来说不是很有用,因为会有几个类似的设备(相同的供应商,通常是相同的产品类型) ) 连接的。
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 toSTORAGE\REMOVABLEMEDIA\7&190C24E5&0&RM
seen above in the device list).
@MSalters的回答看起来很有希望:在 XP 上,HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
有REG_BINARY
值\DosDevices\x:
(x
[AZ]在哪里);注释是正确设备的 (UTF-16) 名称(例如\DosDevices\A:
= " \??\STORAGE#RemovableMedia#7&190c24e5&0&RM#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}\"
,对应于STORAGE\REMOVABLEMEDIA\7&190C24E5&0&RM
上面在设备列表中看到的)。
Will see if that's the way to go.
将看看这是否是要走的路。
采纳答案by MSalters
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).
这是一个不平凡的问题。据我所知,它没有官方 API。因此,您需要一个未公开的 API:注册表。HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
包含驱动器号和卷 ID 的条目。如果查看实际数据,您会发现它标识了驱动器。将二进制数据视为 Unicode 字符串。它会指向你HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ENUM\
。但是,XP 和 Vista 的不同之处在于其中引用了哪些子项以及如何引用。(Vista 更容易,因此请先尝试)。
回答by Ilya
There is an official API to do this. See this sample: http://www.codeproject.com/KB/system/RemoveDriveByLetter.aspx
有一个官方 API 可以做到这一点。请参阅此示例: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).
原理是 Windows 上的每个存储卷都分配了一个全局编号。首先,您向驱动器询问其编号(通过打开“\X:”并发送IOCTL_STORAGE_GET_DEVICE_NUMBER
请求)。其次,您查看所有磁盘设备并询问每个磁盘设备的编号(通过打开它,通过不同的路径,因此您不能只进行字符串比较)。
Once you find a disk device carrying the same number as you queried your drive, you know you have a winner.
一旦找到与您查询驱动器编号相同的磁盘设备,您就知道您赢了。