枚举所有USB设备代码

枚举所有连接的USB设备代码,编译环境VS2010

项目地址: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);
	//是此hub上的哪个port需要被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;

	//用一个动态申请的足够放置全部descriptor的buffer来请求全部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;
}

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值