总结了用SetupDiGetClassDevs枚举U盘对应的盘符的两种方式:
其一:判断设备是否是u盘的办法是首先用GUID_DEVINTERFACE_VOLUME调用SetupDiGetClassDevs后再SetupDiEnumDeviceInterfaces,SetupDiGetDeviceInterfaceDetail……最后还需要和事先传进来的Device ID比较。
其二:首先是用SetupDiGetClassDevs枚举GUID_DEVINTERFACE_USB_DEVICE,找到所有USB设备。然后用 SetupDiGetClassDevs枚举GUID_DEVINTERFACE_VOLUME,找到所有VOLUME后依次递归取其Parent,直到 到达HTREE/ROOT/0或者某个Parent等于前面得到的USB设备为止。
值得注意的是,GUID_DEVINTERFACE_USB_DEVICE、GUID_DEVINTERFACE_DISK枚举出的设备只有一个Child,就是自身。
下面是SetupDiGetClassDevs在我机器上的一些结果:
Volume
{4D36E965-E325-11CE-BFC1-08002BE10318}
IDE/CDROMHL-DT-ST_CD-ROM_GCR-8523B_______________1.02____/5&4DE3C4&0&0.0.0
{4D36E965-E325-11CE-BFC1-08002BE10318}
SCSI/CDROM&VEN_GENERIC&PROD_DVD-ROM&REV_1.0/2&12B1DE20&0&000
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET27FCC3000LENGTH27FCB3400
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET4FF97E200LENGTH4510E1E00
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET7E00LENGTH27FCB3400
Disk
{4D36E967-E325-11CE-BFC1-08002BE10318}
IDE/DISKST340015A_______________________________3.15____/4C35484158483150202020202020202020202020
{4D36E967-E325-11CE-BFC1-08002BE10318}
USBSTOR/DISK&VEN_GENERIC&PROD_USB_FLASH_DRIVE&REV_1.00/6&2ECD5725&1
USB
{36FC9E60-C465-11CF-8056-444553540000}
USB/VID_1043&PID_8012/5&1E5B5D9&0&5
PARTITION
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
下面是一些VOLUME的父系层次:
{4D36E965-E325-11CE-BFC1-08002BE10318}
IDE/CDROMHL-DT-ST_CD-ROM_GCR-8523B_______________1.02____/5&4DE3C4&0&0.0.0
PCIIDE/IDECHANNEL/4&14E96D70&0&1
PCI/VEN_8086&DEV_24DB&SUBSYS_300E103C&REV_02/3&13C0B0C5&0&F9
ACPI/PNP0A03/1
ACPI_HAL/PNP0C08/0
ROOT/ACPI_HAL/0000
HTREE/ROOT/0
{4D36E965-E325-11CE-BFC1-08002BE10318}
SCSI/CDROM&VEN_GENERIC&PROD_DVD-ROM&REV_1.0/2&12B1DE20&0&000
PCI/D347PRT/0000
ROOT/SYSTEM/0003
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
USBSTOR/DISK&VEN_GENERIC&PROD_USB_FLASH_DRIVE&REV_1.00/6&2ECD5725&1
USB/VID_1043&PID_8012/5&1E5B5D9&0&5
USB/ROOT_HUB20/4&386BDE3F&0
PCI/VEN_8086&DEV_24DD&SUBSYS_300E103C&REV_02/3&13C0B0C5&0&EF
ACPI/PNP0A03/1
ACPI_HAL/PNP0C08/0
ROOT/ACPI_HAL/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET27FCC3000LENGTH27FCB3400
ROOT/FTDISK/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET4FF97E200LENGTH4510E1E00
ROOT/FTDISK/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET7E00LENGTH27FCB3400
ROOT/FTDISK/0000
HTREE/ROOT/0
对照一下可以发现以下值得注意的地方:
1、IDE硬盘中VOLUME的设备ID包括了VOLUME的位置、长度
2、IDE硬盘中VOLUME的Parents中不包括该硬盘的Device ID
3、U盘中VOLUME的Parents中包括该U盘的Device ID
4、U盘中VOLUME的Device ID里有“REMOVABLEMEDIA”标记
</div>
</div><div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod="popu_307" data-dsm="post">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e2445db1a8.css">
<div class="htmledit_views">
<p><span id="ctl00_MainContentPlaceholder_ctl01_ctl00_lblEntry">
网 络上有段代码是用SetupDiGetClassDevs枚举U盘对应的盘符。代码中判断设备是否是u盘的办法是首先用 GUID_DEVINTERFACE_VOLUME调用SetupDiGetClassDevs后再 SetupDiEnumDeviceInterfaces,SetupDiGetDeviceInterfaceDetail……最后还需要和事先传进来 的Device ID比较。
实际上,经过试验,我找到另外一个简单一点的办法:
首先是用 SetupDiGetClassDevs枚举GUID_DEVINTERFACE_USB_DEVICE,找到所有USB设备。然后用 SetupDiGetClassDevs枚举GUID_DEVINTERFACE_VOLUME,找到所有VOLUME后依次递归取其Parent,直到 到达HTREE/ROOT/0或者某个Parent等于前面得到的USB设备为止。
值得注意的是,GUID_DEVINTERFACE_USB_DEVICE、GUID_DEVINTERFACE_DISK枚举出的设备只有一个Child,就是自身。
下面是SetupDiGetClassDevs在我机器上的一些结果:
Volume
{4D36E965-E325-11CE-BFC1-08002BE10318}
IDE/CDROMHL-DT-ST_CD-ROM_GCR-8523B_______________1.02____/5&4DE3C4&0&0.0.0
{4D36E965-E325-11CE-BFC1-08002BE10318}
SCSI/CDROM&VEN_GENERIC&PROD_DVD-ROM&REV_1.0/2&12B1DE20&0&000
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET27FCC3000LENGTH27FCB3400
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET4FF97E200LENGTH4510E1E00
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET7E00LENGTH27FCB3400
Disk
{4D36E967-E325-11CE-BFC1-08002BE10318}
IDE/DISKST340015A_______________________________3.15____/4C35484158483150202020202020202020202020
{4D36E967-E325-11CE-BFC1-08002BE10318}
USBSTOR/DISK&VEN_GENERIC&PROD_USB_FLASH_DRIVE&REV_1.00/6&2ECD5725&1
USB
{36FC9E60-C465-11CF-8056-444553540000}
USB/VID_1043&PID_8012/5&1E5B5D9&0&5
PARTITION
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
下面是一些VOLUME的父系层次:
{4D36E965-E325-11CE-BFC1-08002BE10318}
IDE/CDROMHL-DT-ST_CD-ROM_GCR-8523B_______________1.02____/5&4DE3C4&0&0.0.0
PCIIDE/IDECHANNEL/4&14E96D70&0&1
PCI/VEN_8086&DEV_24DB&SUBSYS_300E103C&REV_02/3&13C0B0C5&0&F9
ACPI/PNP0A03/1
ACPI_HAL/PNP0C08/0
ROOT/ACPI_HAL/0000
HTREE/ROOT/0
{4D36E965-E325-11CE-BFC1-08002BE10318}
SCSI/CDROM&VEN_GENERIC&PROD_DVD-ROM&REV_1.0/2&12B1DE20&0&000
PCI/D347PRT/0000
ROOT/SYSTEM/0003
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/REMOVABLEMEDIA/7&35EAC781&0&RM
USBSTOR/DISK&VEN_GENERIC&PROD_USB_FLASH_DRIVE&REV_1.00/6&2ECD5725&1
USB/VID_1043&PID_8012/5&1E5B5D9&0&5
USB/ROOT_HUB20/4&386BDE3F&0
PCI/VEN_8086&DEV_24DD&SUBSYS_300E103C&REV_02/3&13C0B0C5&0&EF
ACPI/PNP0A03/1
ACPI_HAL/PNP0C08/0
ROOT/ACPI_HAL/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET27FCC3000LENGTH27FCB3400
ROOT/FTDISK/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET4FF97E200LENGTH4510E1E00
ROOT/FTDISK/0000
HTREE/ROOT/0
{71A27CDD-812A-11D0-BEC7-08002BE2092F}
STORAGE/VOLUME/1&30A96598&0&SIGNATURE8150814OFFSET7E00LENGTH27FCB3400
ROOT/FTDISK/0000
HTREE/ROOT/0
对照一下可以发现以下值得注意的地方:
1、IDE硬盘中VOLUME的设备ID包括了VOLUME的位置、长度
2、IDE硬盘中VOLUME的Parents中不包括该硬盘的Device ID
3、U盘中VOLUME的Parents中包括该U盘的Device ID
4、U盘中VOLUME的Device ID里有“REMOVABLEMEDIA”标记