SetupDiGetClassDevs函数枚举设备

总结了用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”标记

虽然简单,但是实用 HDEVINFO hDevInfo; SP_DEVINFO_DATA DeviceInfoData; DWORD i,j; int ret=0; hDevInfo = SetupDiGetClassDevs((LPGUID) &GUID;_DEVCLASS_MOUSE, 0, 0,DIGCF_PRESENT);//DIGCF_PROFILE);// /*   GUID_DEVCLASS_FDC软盘控制器   GUID_DEVCLASS_DISPLAY显示卡   GUID_DEVCLASS_CDROM光驱   GUID_DEVCLASS_KEYBOARD键盘   GUID_DEVCLASS_COMPUTER计算机   GUID_DEVCLASS_SYSTEM系统   GUID_DEVCLASS_DISKDRIVE磁盘驱动器   GUID_DEVCLASS_MEDIA声音、视频和游戏控制器   GUID_DEVCLASS_MODEMMODEM   GUID_DEVCLASS_MOUSE鼠标和其他指针设备   GUID_DEVCLASS_NET网络设备器   GUID_DEVCLASS_USB通用串行总线控制器   GUID_DEVCLASS_FLOPPYDISK软盘驱动器   GUID_DEVCLASS_UNKNOWN未知设备   GUID_DEVCLASS_SCSIADAPTERSCSI 和 RAID 控制器   GUID_DEVCLASS_HDCIDE ATA/ATAPI 控制器   GUID_DEVCLASS_PORTS端口(COM 和 LPT)   GUID_DEVCLASS_MONITOR监视器   */ if (hDevInfo == INVALID_HANDLE_VALUE){ // Insert error handling here. // return ; } // Enumerate through all devices in Set. DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,&DeviceInfoData;);i++) { DWORD DataT; //LPTSTR buffer = NULL; char buffer[2048]; DWORD buffersize =sizeof(buffer); while (!SetupDiGetDeviceRegistryProperty( hDevInfo, &DeviceInfoData;, SPDRP_FRIENDLYNAME, &DataT;, (PBYTE)buffer, buffersize, &buffersize;)) { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { // Change the buffer size. //if (buffer) LocalFree(buffer); //buffer = (PSP_INF_INFORMATION)LocalAlloc(LPTR,buffersize); // <!--[if !supportEmptyParas]--><!--[endif]--> } else { // Insert error handling here. break; } } if (buffer != NULL && i == 0) { // temp.Format(""); // str += temp; } // temp.Format("%s",buffer); // str += temp; if (buffer) LocalFree(buffer); } if (i != 0) { // temp.Format(""); // str += temp; } if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS ) { return ; } // <!--[if !supportEmptyParas]--><!--[endif]--> // Cleanup SetupDiDestroyDeviceInfoList(hDevInfo);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值