获取USB控制器下的根集线器名称
2023-10-22
142
0
每个USB根集线器下有惟一的USBS根控制器。当我们打开某个USB根控制器时,可以使用IOCTL_USB_GET_ROOT_HUB_NAME获取USB根控制的名称。而通过打开该控制器名称,就可以获取USB根集线器相的信息。
注意:这里的根集线器名需要加上\?\之后,才能通过CreateFile在应用层打开。
对于本人电脑的中获取USB根控制的信息,可以使用以下代码:
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <usbdi.h>
VOID UsbControlInfo(HANDLE h)
{
char buff[1024] = { 0 };
ULONG rtn = 0;
BOOL isok = DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0, buff, sizeof(buff), &rtn, NULL);
if (isok)
{
PUSB_ROOT_HUB_NAME pInfo = (PUSB_ROOT_HUB_NAME)buff;
printf("%ws\n", pInfo->RootHubName);
}
}
#define NUM_HCS_TO_CHECK 10
VOID EnumerateHostControllers()
{
TCHAR HCName[16];
for (int HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
{
_stprintf_s(HCName, sizeof(HCName) / sizeof(HCName[0]), _T("\\\\.\\HCD%d"), HCNum);
HANDLE hHCDev = CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hHCDev != INVALID_HANDLE_VALUE)
{
UsbControlInfo(hHCDev);
CloseHandle(hHCDev);
}
}
}
int main()
{
EnumerateHostControllers();
return 0;
}
其返回的内容如下:
USB#ROOT_HUB30#4&186df573&4&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8}
而我们使用UsbTreeViewr来查看其信息:
+++++++++++++++++ Device Information ++++++++++++++++++
Device Description : USB 根集线器(xHCI)
Device Path : \\?\USB#ROOT_HUB30#4&186df573&4&0#{f18a0e88-c30c-11d0-8815-00a0c906bed8} (GUID_DEVINTERFACE_USB_HUB)
Kernel Name : \Device\USBPDO-0
Device ID : USB\ROOT_HUB30\4&186DF573&4&0
Hardware IDs : USB\ROOT_HUB30&VID8086&PIDA2AF&REV0000 USB\ROOT_HUB30&VID8086&PIDA2AF USB\ROOT_HUB30
Driver KeyName : {36fc9e60-c465-11cf-8056-444553540000}\0001 (GUID_DEVCLASS_USB)
Driver : \SystemRoot\System32\drivers\UsbHub3.sys (Version: 10.0.14393.6078 Date: 2023-06-27)
Driver Inf : C:\Windows\inf\usbhub3.inf
Legacy BusType : PNPBus
Class : USB
Class GUID : {36fc9e60-c465-11cf-8056-444553540000} (GUID_DEVCLASS_USB)
Service : USBHUB3
Enumerator : USB
Location Info : -
Manufacturer Info : (标准 USB 集线器)
Capabilities : 0x80 (SurpriseRemovalOK)
Status : 0x0180000A (DN_DRIVER_LOADED, DN_STARTED, DN_NT_ENUMERATOR, DN_NT_DRIVER)
Problem Code : 0
Address : 0
HcDisableSelectiveSuspend: 0
EnableSelectiveSuspend : 0
SelectiveSuspendEnabled : 0
EnhancedPowerMgmtEnabled : 0
IdleInWorkingState : 1
WakeFromSleepState : 0
Power State : D0 (supported: D0, D2, D3, wake from D0, wake from D2)
HID人机交互QQ群:564808376
UAC音频QQ群:218581009
UVC相机QQ群:331552032
BOT&UASP大容量存储QQ群:258159197
STC-USB单片机QQ群:315457461
USB技术交流QQ群2:580684376
USB技术交流QQ群:952873936