关于usb设备枚举的文档,网上有很多,我是参看WDK目录下,src中关于usbview的文件写的。可能不同环境会有细微差别,我使用vs2008 + 目前最新的ddk。里面没有涉及的定义,可以从usbview里面找到,稍作修改就可以。
bool Cmfc1Dlg::EnumUsbDevice(){
TCHAR HCName[16];
int HCNum;
PTSTR rootHubName;
ULONG index;
BOOL success;
PUSB_HUB_CAPABILITIES hubCaps;
PUSB_HUB_CAPABILITIES_EX hubCapsEx;
PUSB_NODE_CONNECTION_INFORMATION_EX connectionInfoEx;
PUSB_DESCRIPTOR_REQUEST configDesc;
PSTRING_DESCRIPTOR_NODE stringDescs;
PUSB_NODE_CONNECTION_INFORMATION connectionInfo;
hubCaps = NULL;
hubCapsEx = NULL;
for (HCNum = 0; HCNum < NUM_HCS_TO_CHECK; HCNum++)
{
//直接查询usb 设备
_stprintf_s(HCName, sizeof(HCName)/sizeof(HCName[0]), _T("\\\\.\\HCD%d"), HCNum);
hHCDev = CreateFile(HCName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hHCDev == INVALID_HANDLE_VALUE)
continue;
PTSTR driverKeyName, deviceDesc;
driverKeyName = GetHCDDriverKeyName(hHCDev);
if(driverKeyName == NULL){
CloseHandle(hHCDev);
return FALSE;
}
deviceDesc = NULL;
deviceDesc = DriverNameToDeviceDesc(driverKeyName, FALSE);
if (deviceDesc)
{
leafName = deviceDesc;
}
else
{
OOPS();
}
ULONG nBytes;
rootHubName =GetRootHubName(hHCDev);//(char*)
if(rootHubName==NULL){
CloseHandle(hHCDev);
return FALSE;
}
PUSB_NODE_INFORMATION HubInfo;
HubInfo = (PUSB_NODE_INFORMATION)ALLOC(sizeof(USB_NODE_INFORMATION));
//PCHAR deviceName;
PTSTR deviceName;
//deviceName = (PCHAR)ALLOC(strlen(rootHubName) + sizeof("\\\\.\\"));
size_t deviceNameSize;
deviceNameSize = _tcslen(rootHubName) + _tcslen(_T("\\\\.\\")) + 1;
deviceName = (PTSTR)ALLOC(deviceNameSize * sizeof(TCHAR));
if (rootHubName != NULL)
{
_tcscpy_s(deviceName, deviceNameSize, _T("\\\\.\\"));
_tcscat_s(deviceName, deviceNameSize, rootHubName);
//
strcpy(deviceName, "\\\\.\\");
//
strcpy(deviceName + sizeof("\\\\.\\") - 1, rootHubName);
hHubDevice = CreateFile(deviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
FREE(deviceName);
if (hHubDevice == INVALID_HANDLE_VALUE){
Gotoend();
return FALSE;
}
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_INFORMATION,
HubInfo,
sizeof(USB_NODE_INFORMATION),
HubInfo,
sizeof(USB_NODE_INFORMATION),
&nBytes,
NULL);
if (!success)
{
Gotoend();
return FALSE;
}
}
int port;
port=HubInfo->u.HubInformation.HubDescriptor.bNumberOfPorts;
for (index=1; index <= port; index++)
{
ULONG nBytesEx;
nBytesEx = sizeof(USB_NODE_CONNECTION_INFORMATION_EX) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfoEx = (PUSB_NODE_CONNECTION_INFORMATION_EX)ALLOC(nBytesEx);
if (connectionInfoEx == NULL)
{
Gotoend();
return FALSE;
}
connectionInfoEx->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
connectionInfoEx,
nBytesEx,
connectionInfoEx,
nBytesEx,
&nBytesEx,
NULL);
if (!success)
{
FREE(connectionInfoEx);
Gotoend();
return FALSE;
}
ULONG nBytes;
nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
sizeof(USB_PIPE_INFO) * 30;
connectionInfo = (PUSB_NODE_CONNECTION_INFORMATION)ALLOC(nBytes);
if (connectionInfo == NULL)
{
Gotoend();
return FALSE;
}
connectionInfo->ConnectionIndex = index;
success = DeviceIoControl(hHubDevice,
IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
connectionInfo,
nBytes,
connectionInfo,
nBytes,
&nBytes,
NULL);
if (!success)
{
FREE(connectionInfo);
FREE(connectionInfoEx);
//Gotoend();
//return FALSE;
continue;
}
connectionInfoEx->ConnectionIndex =
connectionInfo->ConnectionIndex;
connectionInfoEx->DeviceDescriptor =
connectionInfo->DeviceDescriptor;
connectionInfoEx->CurrentConfigurationValue =
connectionInfo->CurrentConfigurationValue;
connectionInfoEx->Speed =
connectionInfo->LowSpeed ? UsbLowSpeed : UsbFullSpeed;
connectionInfoEx->DeviceIsHub =
connectionInfo->DeviceIsHub;
connectionInfoEx->DeviceAddress =
connectionInfo->DeviceAddress;
connectionInfoEx->NumberOfOpenPipes =
connectionInfo->NumberOfOpenPipes;
connectionInfoEx->ConnectionStatus =
connectionInfo->ConnectionStatus;
memcpy(&connectionInfoEx->PipeList[0],
&connectionInfo->PipeList[0],
sizeof(USB_PIPE_INFO) * 30);
FREE(connectionInfo);
deviceDesc = NULL;
/*
if (connectionInfoEx->ConnectionStatus != NoDeviceConnected)
{
driverKeyName = GetDriverKeyName(hHubDevice,
index);
deviceDesc = DriverNameToDeviceDesc(driverKeyName, FALSE);
if (deviceDesc)
{
leafName = deviceDesc;
}
else
{
OOPS();
}
if (driverKeyName)
{
FREE(driverKeyName);
}
}
*/
if (connectionInfoEx->ConnectionStatus == DeviceConnected)
{
driverKeyName = GetDriverKeyName(hHubDevice,
index);
deviceDesc = DriverNameToDeviceDesc(driverKeyName, FALSE);
//得到usb设备的描述 eg:
//usb printing support or usb人体输入设备等。
if (deviceDesc)
{
leafName = deviceDesc;
}
else
{
OOPS();
}
if (driverKeyName)
{
FREE(driverKeyName);
}
configDesc = GetConfigDescriptor(hHubDevice,index,0);
if (configDesc != NULL &&
AreThereStringDescriptors(&connectionInfoEx->DeviceDescriptor,
(PUSB_CONFIGURATION_DESCRIPTOR)(configDesc+1)))
{
stringDescs = GetAllStringDescriptors(
hHubDevice,
index,
&connectionInfoEx->DeviceDescriptor,
(PUSB_CONFIGURATION_DESCRIPTOR)(configDesc+1));
}
//取出序列号索引
//
UCHAR nSerialno = connectionInfoEx->DeviceDescriptor.iSerialNumber;
//
CHAR OutBuff[20] = {0};
//
GetStringDescriptor(hHubDevice,connectionInfo->ConnectionIndex,nSerialno,OutBuff);
//如何得到 该usb设备的class类型,eg:printer
//判断序列号是否有效
// if(序列号是否有效)
if(index == port){
CloseHandle(hHubDevice);
CloseHandle(hHCDev);
break;
}
FREE(connectionInfoEx);
continue;
//return true;
// }
}
}
}
//CloseHandle(hHubDevice);
//CloseHandle(hHCDev);
//return false;
}