当前位置:网站首页>Enumerate all USB device codes
Enumerate all USB device codes
2022-06-12 09:44:00 【Tody Guo】
Enumerate all connected USB Device code , Compile environment VS2010
Project address :https://gitee.com/tody_guo/ls_usb.git
// ls_usb.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <windows.h>
#include <basetyps.h>
#include <winioctl.h>
#include <setupapi.h>
#include <initguid.h>
#include <string.h>
#include <stdio.h>
#include <tchar.h>
#include "usb100.h"
#include "usbdesc.h"
#include "usbioctl.h"
#include "usbiodef.h"
#define MAX_HCD 10
typedef struct _STRING_DESCRIPTOR_NODE
{
struct _STRING_DESCRIPTOR_NODE *Next;
UCHAR DescriptorIndex;
USHORT LanguageID;
USB_STRING_DESCRIPTOR StringDescriptor[0];
} STRING_DESCRIPTOR_NODE, *PSTRING_DESCRIPTOR_NODE;
/******************************************************************/
PCHAR GetDriverKeyName(HANDLE Hub, ULONG ConnectionIndex);
PCHAR GetHCDDriverKeyName(HANDLE HCD);
PCHAR GetRootHubName(HANDLE HostController);
PCHAR WideStrToMultiStr(PWCHAR WideStr);
bool GetStringDescriptor (HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex, CHAR * outBuff);
PCHAR GetExternalHubName(HANDLE Hub,ULONG ConnectionIndex);
bool EnumerateHostControllers();
void EnumerateHub(PCHAR HubName, PCHAR Msg);
void EnumerateHubPorts(HANDLE hHubDevice,ULONG NumPorts);
PUSB_DESCRIPTOR_REQUEST GetConfigDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex);
BOOL AreThereStringDescriptors(PUSB_DEVICE_DESCRIPTOR DeviceDesc,PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc);
/******************************************************************/
void EnumerateHubPorts(HANDLE hHubDevice,ULONG NumPorts)
{
ULONG index;
BOOL success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
PUSB_DESCRIPTOR_REQUEST configDesc;
//list ports of root hub
unsigned int port;
port=NumPorts;
for (index=1; index <= port; index++)
{
ULONG nBytes;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) + sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)malloc(nBytes);
if (connectionInfo == NULL)
goto end;
connectionInfo->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION, connectionInfo, nBytes, connectionInfo, nBytes, &nBytes, NULL);
if (!success)
{
free(connectionInfo);
goto end;
}
if (connectionInfo->ConnectionStatus == DeviceConnected)
{
configDesc = GetConfigDescriptor(hHubDevice, index, 0);
if (connectionInfo->DeviceIsHub){
PCHAR extHubName;
extHubName = GetExternalHubName(hHubDevice,index);
if (extHubName != NULL){
EnumerateHub(extHubName," - External Hub");
//continue;
}
}
// printf("configIndex:%d\n",configDesc->SetupPacket.);
UCHAR nProduct = connectionInfo->DeviceDescriptor.iProduct;
UCHAR nManuf = connectionInfo->DeviceDescriptor.iManufacturer;
CHAR OutBuffPro[20] = {0};
CHAR OutBuffMan[20] = {0};
GetStringDescriptor(hHubDevice, connectionInfo->ConnectionIndex, nProduct, OutBuffPro);
GetStringDescriptor(hHubDevice, connectionInfo->ConnectionIndex, nManuf, OutBuffMan);
printf("\n\t[PORT%d]: %04X:%04X\t%04X\t%s - %s", index, connectionInfo->DeviceDescriptor.idVendor, connectionInfo->DeviceDescriptor.idProduct,
connectionInfo->DeviceDescriptor.bcdUSB, OutBuffMan, OutBuffPro);
}
}
end:
CloseHandle(hHubDevice);
}
void EnumerateHub(PCHAR rootHubName, PCHAR Msg)
{
ULONG index;
BOOL success;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
HANDLE hHubDevice;
PCHAR driverKeyName, deviceDesc;
ULONG nBytes;
PUSB_NODE_INFORMATION HubInfo;
HubInfo = (PUSB_NODE_INFORMATION)malloc(sizeof(USB_NODE_INFORMATION));
PCHAR deviceName;
deviceName = (PCHAR)malloc(strlen(rootHubName) + sizeof("\\\\.\\"));
if (rootHubName != NULL)
{
strcpy(deviceName, "\\\\.\\");
strcpy(deviceName + sizeof("\\\\.\\") - 1, rootHubName);
printf("\n%s: %s", Msg, deviceName);
hHubDevice = CreateFileA(deviceName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hHubDevice == INVALID_HANDLE_VALUE){
printf("\nCreateFile");
exit(1);
}
free(deviceName);
success = DeviceIoControl(hHubDevice, IOCTL_USB_GET_NODE_INFORMATION, HubInfo, sizeof(USB_NODE_INFORMATION), HubInfo, sizeof(USB_NODE_INFORMATION), &nBytes, NULL);
if (!success){
printf("\nDeviceIoControl");
exit(1);
}
}
// noew emuerate all ports
EnumerateHubPorts(hHubDevice, HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts);
}
bool EnumerateHostControllers()
{
WCHAR HCName[16];
HANDLE hHCDev;
int HCNum;
PCHAR driverKeyName;
PCHAR deviceDesc;
PCHAR rootHubName;
for (HCNum = 0; HCNum < MAX_HCD; HCNum++){
wsprintf(HCName, _T("\\\\.\\HCD%d"), HCNum);
hHCDev = CreateFile(HCName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hHCDev == INVALID_HANDLE_VALUE){
return false;
}
driverKeyName = GetHCDDriverKeyName(hHCDev);
if (driverKeyName)
{
rootHubName=GetRootHubName(hHCDev);
if(rootHubName!=NULL)
{
EnumerateHub(rootHubName, "Root Hub");
}else{
CloseHandle(hHCDev);
return false;
}
}else{
CloseHandle(hHCDev);
return false;
}
CloseHandle(hHCDev);
}
return true;
}
PCHAR GetExternalHubName(HANDLE Hub,ULONG ConnectionIndex)
{
BOOL success;
ULONG nBytes;
USB_NODE_CONNECTION_NAME extHubName;
PUSB_NODE_CONNECTION_NAME extHubNameW;
PCHAR extHubNameA;
extHubNameW = NULL;
extHubNameA = NULL;
extHubName.ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub, IOCTL_USB_GET_NODE_CONNECTION_NAME, &extHubName, sizeof(extHubName), &extHubName, sizeof(extHubName), &nBytes,NULL);
if (!success)
goto GetExternalHubNameError;
nBytes = extHubName.ActualLength;
if (nBytes <= sizeof(extHubName))
goto GetExternalHubNameError;
extHubNameW=(PUSB_NODE_CONNECTION_NAME)GlobalAlloc(GPTR,nBytes);
if (extHubNameW == NULL)
goto GetExternalHubNameError;
extHubNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub, IOCTL_USB_GET_NODE_CONNECTION_NAME, extHubNameW, nBytes, extHubNameW, nBytes, &nBytes, NULL);
if (!success)
goto GetExternalHubNameError;
extHubNameA = WideStrToMultiStr(extHubNameW->NodeName);
GlobalFree(extHubNameW);
return extHubNameA;
GetExternalHubNameError:
if (extHubNameW != NULL)
{
GlobalFree(extHubNameW);
extHubNameW = NULL;
}
return NULL;
}
int main(int argc, char* argv[])
{
EnumerateHostControllers();
getchar();
return 0;
}
PCHAR GetDriverKeyName(HANDLE Hub, ULONG ConnectionIndex)
{
BOOL success;
ULONG nBytes;
USB_NODE_CONNECTION_DRIVERKEY_NAME driverKeyName;
PUSB_NODE_CONNECTION_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
driverKeyName.ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, &driverKeyName, sizeof(driverKeyName), &driverKeyName, sizeof(driverKeyName), &nBytes, NULL);
if (!success)
{
goto GetDriverKeyNameError;
}
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName))
{
goto GetDriverKeyNameError;
}
driverKeyNameW = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)malloc(nBytes);
if (driverKeyNameW == NULL)
{
goto GetDriverKeyNameError;
}
driverKeyNameW->ConnectionIndex = ConnectionIndex;
success = DeviceIoControl(Hub, IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME, driverKeyNameW, nBytes, driverKeyNameW, nBytes, &nBytes, NULL);
if (!success)
{
goto GetDriverKeyNameError;
}
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
free(driverKeyNameW);
return driverKeyNameA;
GetDriverKeyNameError:
if (driverKeyNameW != NULL)
{
free(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;
}
PCHAR GetRootHubName(HANDLE HostController)
{
BOOL success;
ULONG nBytes;
USB_ROOT_HUB_NAME rootHubName;
PUSB_ROOT_HUB_NAME rootHubNameW;
PCHAR rootHubNameA;
rootHubNameW = NULL;
rootHubNameA = NULL;
success = DeviceIoControl(HostController, IOCTL_USB_GET_ROOT_HUB_NAME, 0, 0, &rootHubName, sizeof(rootHubName), &nBytes, NULL);
if (!success)
{
goto GetRootHubNameError;
}
nBytes = rootHubName.ActualLength;
rootHubNameW =(PUSB_ROOT_HUB_NAME) malloc(nBytes);
if (rootHubNameW == NULL)
{
goto GetRootHubNameError;
}
success = DeviceIoControl(HostController, IOCTL_USB_GET_ROOT_HUB_NAME, NULL, 0, rootHubNameW, nBytes, &nBytes, NULL);
if (!success)
{
goto GetRootHubNameError;
}
rootHubNameA = WideStrToMultiStr(rootHubNameW->RootHubName);
free(rootHubNameW);
return rootHubNameA;
GetRootHubNameError:
if (rootHubNameW != NULL)
{
free(rootHubNameW);
rootHubNameW = NULL;
}
return NULL;
}
PCHAR GetHCDDriverKeyName(HANDLE HCD)
{
BOOL success;
ULONG nBytes;
USB_HCD_DRIVERKEY_NAME driverKeyName;
PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
PCHAR driverKeyNameA;
driverKeyNameW = NULL;
driverKeyNameA = NULL;
success = DeviceIoControl(HCD, IOCTL_GET_HCD_DRIVERKEY_NAME, &driverKeyName, sizeof(driverKeyName), &driverKeyName, sizeof(driverKeyName), &nBytes, NULL);
if (!success)
{
goto GetHCDDriverKeyNameError;
}
nBytes = driverKeyName.ActualLength;
if (nBytes <= sizeof(driverKeyName))
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameW =(PUSB_HCD_DRIVERKEY_NAME) malloc(nBytes);
if (driverKeyNameW == NULL)
{
goto GetHCDDriverKeyNameError;
}
success = DeviceIoControl(HCD, IOCTL_GET_HCD_DRIVERKEY_NAME, driverKeyNameW, nBytes, driverKeyNameW, nBytes, &nBytes, NULL);
if (!success)
{
goto GetHCDDriverKeyNameError;
}
driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
free(driverKeyNameW);
return driverKeyNameA;
GetHCDDriverKeyNameError:
if (driverKeyNameW != NULL)
{
free(driverKeyNameW);
driverKeyNameW = NULL;
}
return NULL;
}
PCHAR WideStrToMultiStr(PWCHAR WideStr)
{
ULONG nBytes;
PCHAR MultiStr;
nBytes = WideCharToMultiByte(CP_ACP, 0, WideStr, -1, NULL, 0, NULL, NULL);
if (nBytes == 0)
{
return NULL;
}
MultiStr =(PCHAR) malloc(nBytes);
if (MultiStr == NULL)
{
return NULL;
}
nBytes = WideCharToMultiByte(CP_ACP, 0, WideStr, -1, MultiStr, nBytes, NULL, NULL);
if (nBytes == 0)
{
free(MultiStr);
return NULL;
}
return MultiStr;
}
BOOL AreThereStringDescriptors(PUSB_DEVICE_DESCRIPTOR DeviceDesc,PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc)
{
PUCHAR descEnd;
PUSB_COMMON_DESCRIPTOR commonDesc;
// Check Device Descriptor strings
if(DeviceDesc->iManufacturer||DeviceDesc->iProduct||DeviceDesc->iSerialNumber)
return TRUE;
// Check the Configuration and Interface Descriptor strings
descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;
commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;
while ((PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd &&
(PUCHAR)commonDesc + commonDesc->bLength <= descEnd)
{
switch (commonDesc->bDescriptorType)
{
case USB_CONFIGURATION_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
break;
if (((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc)->iConfiguration)
return TRUE;
continue;
case USB_INTERFACE_DESCRIPTOR_TYPE:
if (commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR) &&
commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR2))
break;
if (((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->iInterface)
return TRUE;
continue;
default:
continue;
}
break;
}
return FALSE;
}
PUSB_DESCRIPTOR_REQUEST GetConfigDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex)
{
BOOL success;
ULONG nBytes;
ULONG nBytesReturned;
UCHAR configDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST)+sizeof(USB_CONFIGURATION_DESCRIPTOR)];
PUSB_DESCRIPTOR_REQUEST configDescReq;
PUSB_CONFIGURATION_DESCRIPTOR configDesc;
// Request the Configuration Descriptor the first time using our
// local buffer, which is just big enough for the Cofiguration
// Descriptor itself.
nBytes = sizeof(configDescReqBuf);
configDescReq = (PUSB_DESCRIPTOR_REQUEST)configDescReqBuf;
configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
memset(configDescReq, 0, nBytes);
// This is it hub Which one is on port Need to be request
configDescReq->ConnectionIndex = ConnectionIndex;
// USBHUB uses URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE to process this
// IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION request.
// USBD will automatically initialize these fields:
// bmRequest = 0x80
// bRequest = 0x06
// We must inititialize these fields:
// wValue = Descriptor Type (high) and Descriptor Index (low byte)
// wIndex = Zero (or Language ID for String Descriptors)
// wLength = Length of descriptor buffer
configDescReq->SetupPacket.wValue=(USB_CONFIGURATION_DESCRIPTOR_TYPE<<8)|DescriptorIndex;
configDescReq->SetupPacket.wLength=(USHORT)(nBytes-sizeof(USB_DESCRIPTOR_REQUEST));
success=DeviceIoControl(hHubDevice, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, configDescReq, nBytes, configDescReq, nBytes, &nBytesReturned, NULL);
if(!success)
return NULL;
if(nBytes!=nBytesReturned)
return NULL;
if(configDesc->wTotalLength<sizeof(USB_CONFIGURATION_DESCRIPTOR))
return NULL;
// Use enough of a dynamic application to place all descriptor Of buffer To request all configuration descriptor
nBytes=sizeof(USB_DESCRIPTOR_REQUEST)+configDesc->wTotalLength;
configDescReq=(PUSB_DESCRIPTOR_REQUEST)GlobalAlloc(GPTR,nBytes);
if (configDescReq==NULL)
return NULL;
configDesc = (PUSB_CONFIGURATION_DESCRIPTOR)(configDescReq+1);
// Indicate the port from which the descriptor will be requested
configDescReq->ConnectionIndex = ConnectionIndex;
// USBHUB uses URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE to process this
// IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION request.
// USBD will automatically initialize these fields:
// bmRequest = 0x80
// bRequest = 0x06
// We must inititialize these fields:
// wValue = Descriptor Type (high) and Descriptor Index (low byte)
// wIndex = Zero (or Language ID for String Descriptors)
// wLength = Length of descriptor buffer
configDescReq->SetupPacket.wValue=(USB_CONFIGURATION_DESCRIPTOR_TYPE<<8)|DescriptorIndex;
configDescReq->SetupPacket.wLength=(USHORT)(nBytes-sizeof(USB_DESCRIPTOR_REQUEST));
// Now issue the get descriptor request.
success = DeviceIoControl(hHubDevice, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, configDescReq, nBytes, configDescReq, nBytes, &nBytesReturned, NULL);
if(!success)
{
GlobalFree(configDescReq);
return NULL;
}
if(nBytes!=nBytesReturned)
{
GlobalFree(configDescReq);
return NULL;
}
if(configDesc->wTotalLength!=(nBytes-sizeof(USB_DESCRIPTOR_REQUEST)))
{
GlobalFree(configDescReq);
return NULL;
}
return configDescReq;
}
bool GetStringDescriptor (HANDLE hHubDevice, ULONG ConnectionIndex,UCHAR DescriptorIndex, CHAR *outBuff)
{
BOOL success;
ULONG nBytes;
ULONG nBytesReturned;
UCHAR stringDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST) + MAXIMUM_USB_STRING_LENGTH];
PUSB_DESCRIPTOR_REQUEST stringDescReq;
PUSB_STRING_DESCRIPTOR stringDesc;
nBytes = sizeof(stringDescReqBuf);
stringDescReq = (PUSB_DESCRIPTOR_REQUEST)stringDescReqBuf;
stringDesc = (PUSB_STRING_DESCRIPTOR)(stringDescReq+1);
ZeroMemory(stringDescReq,nBytes);
stringDescReq->ConnectionIndex = ConnectionIndex;
stringDescReq->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8) | DescriptorIndex;
stringDescReq->SetupPacket.wIndex = GetSystemDefaultLangID();
stringDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
success = DeviceIoControl(hHubDevice, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, stringDescReq,nBytes, stringDescReq,nBytes, &nBytesReturned, NULL);
if (!success || nBytesReturned < 2)
return false;
if (stringDesc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE)
return false;
if (stringDesc->bLength != nBytesReturned - sizeof(USB_DESCRIPTOR_REQUEST))
return false;
if (stringDesc->bLength % 2 != 0)
return false;
WCHAR * wChar = new WCHAR[stringDesc->bLength + 1];
memcpy(wChar,stringDesc->bString,stringDesc->bLength);
char *szTemp = WideStrToMultiStr(wChar);
lstrcpyA(outBuff, szTemp);
if(szTemp)
delete []szTemp;
if(wChar)
delete []wChar;
return true;
}
边栏推荐
- Abstract classes and interfaces
- C#入门系列(十二) -- 字符串
- MySQL index
- UEFI EDKII 编程学习
- Principle analysis of mongodb storage engine wiredtiger
- Mycat的使用
- 003:AWS认为什么是数据湖?
- SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
- II Transforming regular expressions into finite state automata: NFA state machine recognizes input strings
- Golandidea 2020.01 cracked version
猜你喜欢

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000

榜样访谈——董宇航:在俱乐部中收获爱情

List、Set、Map的区别

TAP 系列文章3 | Tanzu Application Platform 部署参考架构介绍

I Regular expression to finite state automata: regular expression to NFA

Four steps for sending rockertmq producer messages

Implementation of hotspot reference
![[cloud native] establishment of Eureka service registration](/img/da/0a700081be767db91edd5f3d49b5d0.png)
[cloud native] establishment of Eureka service registration

Do you know the meaning behind these questions?
![[cloud native] what exactly does it mean? This article shares the answer with you](/img/82/f268adcbdbe8195a066d065eb560d7.jpg)
[cloud native] what exactly does it mean? This article shares the answer with you
随机推荐
Test case and bug description specification reference
UEFI edkii programming learning
电脑启动快捷键一览表
Share the basic knowledge of software testing and write something you don't know
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
总有一根阴线(上影线)会阻止多军前进的脚步,总有一个阳线(下影线)会阻挡空军肆虐的轰炸
日本经济泡沫与房价泡沫
奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
5 most common CEPH failure scenarios
【云原生】具体指什么呢---此文和大伙儿分享答案
《第五项修炼》读书笔记
榜样访谈——董宇航:在俱乐部中收获爱情
2022 pole technology communication - anmou technology ushers in new opportunities for development
IV Transforming regular expressions into finite state automata: DFA minimization
使用Visual Studio 2017创建简单的窗口程序
Research progress of DNA digital information storage
Auto.js学习笔记7:js文件调用另一个js文件里的函数和变量,解决调用失败的各种问题
行业分析怎么做
抽象类和接口
002:数据湖有哪些特征