⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanusbdlg.cpp

📁 关于usb的源码ScanUSB
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    }
	
	
    //
    // Get 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)
			{
				stringDescNodeTail = GetStringDescriptors(
					hHubDevice,
					ConnectionIndex,
					((PUSB_CONFIGURATION_DESCRIPTOR)commonDesc)->iConfiguration,
					numLanguageIDs,
					languageIDs,
					stringDescNodeTail);
			}
//gordon@kinpobj.com.cn
			commonDesc =(PUSB_COMMON_DESCRIPTOR)(commonDesc + commonDesc->bLength);
			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)
			{
				stringDescNodeTail = GetStringDescriptors(
					hHubDevice,
					ConnectionIndex,
					((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->iInterface,
					numLanguageIDs,
					languageIDs,
					stringDescNodeTail);
			}
//gordon@kinpobj.com.cn
			commonDesc =(PUSB_COMMON_DESCRIPTOR)(commonDesc + commonDesc->bLength);
			continue;
			
		default:
//gordon@kinpobj.com.cn
			commonDesc =(PUSB_COMMON_DESCRIPTOR)(commonDesc + commonDesc->bLength);
			continue;
        }
        break;
    }
	return supportedLanguagesString;
}

PSTRING_DESCRIPTOR_NODE CScanUsbDlg::GetStringDescriptors(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex, ULONG NumLanguageIDs, USHORT *LanguageIDs, PSTRING_DESCRIPTOR_NODE StringDescNodeTail)
{
	ULONG i;	
    for (i=0; i<NumLanguageIDs; i++)
    {
        StringDescNodeTail->Next = GetStringDescriptor(hHubDevice,
			ConnectionIndex,
			DescriptorIndex,
			*LanguageIDs);
		
        if (StringDescNodeTail->Next)
        {
            StringDescNodeTail = StringDescNodeTail->Next;
        }
		
        LanguageIDs++;
    }	
    return StringDescNodeTail;
}

PSTRING_DESCRIPTOR_NODE CScanUsbDlg::GetStringDescriptor(HANDLE hHubDevice, ULONG ConnectionIndex, UCHAR DescriptorIndex, USHORT LanguageID)
{
	BOOL    success;
    ULONG   nBytes;
    ULONG   nBytesReturned;	
    UCHAR   stringDescReqBuf[sizeof(USB_DESCRIPTOR_REQUEST)+MAXIMUM_USB_STRING_LENGTH];	
    PUSB_DESCRIPTOR_REQUEST stringDescReq;
    PUSB_STRING_DESCRIPTOR  stringDesc;
    PSTRING_DESCRIPTOR_NODE stringDescNode;
	
    nBytes = sizeof(stringDescReqBuf);	
    stringDescReq = (PUSB_DESCRIPTOR_REQUEST)stringDescReqBuf;
    stringDesc = (PUSB_STRING_DESCRIPTOR)(stringDescReq+1);
	
    // Zero fill the entire request structure
    //
    memset(stringDescReq, 0, nBytes);
	
    // Indicate the port from which the descriptor will be requested
    //
    stringDescReq->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
    //
    stringDescReq->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8)
		| DescriptorIndex;
	
    stringDescReq->SetupPacket.wIndex = LanguageID;
	
    stringDescReq->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
	
    // Now issue the get descriptor request.
    //
    success = DeviceIoControl(hHubDevice,
		IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
		stringDescReq,
		nBytes,
		stringDescReq,
		nBytes,
		&nBytesReturned,
		NULL);
	
    //
    // Do some sanity checks on the return from the get descriptor request.
    //
	
    if (!success) return NULL;
    if (nBytesReturned < 2)  return NULL;
    if (stringDesc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE)
    {
        return NULL;
    }
	
    if (stringDesc->bLength != nBytesReturned - sizeof(USB_DESCRIPTOR_REQUEST))
    {
        return NULL;
    }
	
    if (stringDesc->bLength % 2 != 0) return NULL;
	
    //
    // Looks good, allocate some (zero filled) space for the string descriptor
    // node and copy the string descriptor to it.
    //
	
    stringDescNode = (PSTRING_DESCRIPTOR_NODE)ALLOC(sizeof(STRING_DESCRIPTOR_NODE) +
		stringDesc->bLength);
	
    if (stringDescNode == NULL) return NULL;
	
    stringDescNode->DescriptorIndex = DescriptorIndex;
    stringDescNode->LanguageID = LanguageID;
	
    memcpy(stringDescNode->StringDescriptor,
		stringDesc,
		stringDesc->bLength);	
    return stringDescNode;
}


PCHAR CScanUsbDlg::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;
	
    // Get the length of the name of the external hub attached to the
    // specified port.
    //
    extHubName.ConnectionIndex = ConnectionIndex;	
    success = DeviceIoControl(Hub,
		IOCTL_USB_GET_NODE_CONNECTION_NAME,
		&extHubName,
		sizeof(extHubName),
		&extHubName,
		sizeof(extHubName),
		&nBytes,
		NULL);
	
    if (!success)  goto GetExternalHubNameError;	
    // Allocate space to hold the external hub name
    //
    nBytes = extHubName.ActualLength;	
    if (nBytes <= sizeof(extHubName)) goto GetExternalHubNameError;
    extHubNameW = (PUSB_NODE_CONNECTION_NAME)ALLOC(nBytes);	
    if (extHubNameW == NULL) goto GetExternalHubNameError;
    // Get the name of the external hub attached to the specified port
    //
    extHubNameW->ConnectionIndex = ConnectionIndex;	
    success = DeviceIoControl(Hub,
		IOCTL_USB_GET_NODE_CONNECTION_NAME,
		extHubNameW,
		nBytes,
		extHubNameW,
		nBytes,
		&nBytes,
		NULL);
	
    if (!success) goto GetExternalHubNameError;
    // Convert the External Hub name
    //
    extHubNameA = WideStrToMultiStr(extHubNameW->NodeName);
	
    // All done, free the uncoverted external hub name and return the
    // converted external hub name
    //
	FREE(extHubNameW);	
    return extHubNameA;	
GetExternalHubNameError:
    // There was an error, free anything that was allocated
    //
    if (extHubNameW != NULL)
    {
        FREE(extHubNameW);
        extHubNameW = NULL;
    }	
    return NULL;	
}


VOID CScanUsbDlg::DisplayConfigDesc (CString& strShow,PUSB_CONFIGURATION_DESCRIPTOR ConfigDesc,PSTRING_DESCRIPTOR_NODE  StringDescs)
{
	PUCHAR                  descEnd;
    PUSB_COMMON_DESCRIPTOR  commonDesc;
    UCHAR                   bInterfaceClass;
    UCHAR                   bInterfaceSubClass;
    BOOL                    displayUnknown;
	
    bInterfaceClass = 0;	
    descEnd = (PUCHAR)ConfigDesc + ConfigDesc->wTotalLength;	
    commonDesc = (PUSB_COMMON_DESCRIPTOR)ConfigDesc;	
    while(
		   (PUCHAR)commonDesc + sizeof(USB_COMMON_DESCRIPTOR) < descEnd 
		 &&(PUCHAR)commonDesc + commonDesc->bLength <= descEnd
		 )
    {
        displayUnknown = FALSE;		
        switch (commonDesc->bDescriptorType)
        {
		case USB_CONFIGURATION_DESCRIPTOR_TYPE:
			if (commonDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
			{
				displayUnknown = TRUE;
				break;
			}
			DisplayConfigurationDescriptor(strShow,(PUSB_CONFIGURATION_DESCRIPTOR)commonDesc,StringDescs);
			break;
			
		case USB_INTERFACE_DESCRIPTOR_TYPE:
			if ((commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR)) &&
				(commonDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR2)))
			{
				displayUnknown = TRUE;
				break;
			}
			bInterfaceClass = ((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->bInterfaceClass;
			bInterfaceSubClass = ((PUSB_INTERFACE_DESCRIPTOR)commonDesc)->bInterfaceSubClass;
			
			DisplayInterfaceDescriptor(strShow,(PUSB_INTERFACE_DESCRIPTOR)commonDesc,StringDescs);
			break;
			
		case USB_ENDPOINT_DESCRIPTOR_TYPE:
			if ((commonDesc->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR)) &&
				(commonDesc->bLength != sizeof(USB_ENDPOINT_DESCRIPTOR2)))
			{
				displayUnknown = TRUE;
				break;
			}
			DisplayEndpointDescriptor(strShow,(PUSB_ENDPOINT_DESCRIPTOR)commonDesc);
			break;
/*			
		case USB_HID_DESCRIPTOR_TYPE:
			if (commonDesc->bLength < sizeof(USB_HID_DESCRIPTOR))
			{
				displayUnknown = TRUE;
				break;
			}
			DisplayHidDescriptor((PUSB_HID_DESCRIPTOR)commonDesc);
			break;
*/			
		default:
/*
			switch (bInterfaceClass)
			{
			case USB_DEVICE_CLASS_AUDIO:
				displayUnknown = !DisplayAudioDescriptor(
					(PUSB_AUDIO_COMMON_DESCRIPTOR)commonDesc,
					bInterfaceSubClass);
				break;
				
			default:
				displayUnknown = TRUE;
				break;
			}
*/				
			break;
        }
		
        if (displayUnknown)
        {
            DisplayUnknownDescriptor(strShow,commonDesc);
        }		
        commonDesc = (PUSB_COMMON_DESCRIPTOR)(commonDesc + commonDesc->bLength);
    }

}



PCHAR CScanUsbDlg::DriverNameToDeviceDesc(PCHAR DriverName)
{
	DEVINST     devInst;
	DEVINST     devInstNext;
	CONFIGRET   cr;
	ULONG       walkDone = 0;
	ULONG       len;
	
    // Get Root DevNode
    //
    cr = CM_Locate_DevNode(&devInst,NULL,0);
	
    if (cr != CR_SUCCESS) return NULL;
	
    // Do a depth first search for the DevNode with a matching
    // DriverName value
    //
    while (!walkDone)
    {
        // Get the DriverName value
        //
        len = sizeof(buf);
        cr = CM_Get_DevNode_Registry_Property(devInst,CM_DRP_DRIVER,NULL,buf,&len,0);
		
        // If the DriverName value matches, return the DeviceDescription
        //
        if (cr == CR_SUCCESS && strcmp(DriverName, buf) == 0)
        {
            len = sizeof(buf);
            cr = CM_Get_DevNode_Registry_Property(devInst,CM_DRP_DEVICEDESC,NULL,buf,&len,0);
			
            if (cr == CR_SUCCESS) return buf;
            else return NULL;
        }
		
        // This DevNode didn't match, go down a level to the first child.
        //
        cr = CM_Get_Child(&devInstNext,devInst,0);
		
        if (cr == CR_SUCCESS)
        {
            devInst = devInstNext;
            continue;
        }
		
        // Can't go down any further, go across to the next sibling.  If
        // there are no more siblings, go back up until there is a sibling.
        // If we can't go up any further, we're back at the root and we're
        // done.
        //
        for (;;)
        {
            cr = CM_Get_Sibling(&devInstNext,devInst,0);			
            if (cr == CR_SUCCESS)
            {
                devInst = devInstNext;
                break;
            }			
            cr = CM_Get_Parent(&devInstNext,devInst,0);			
            if (cr == CR_SUCCESS)
            {
                devInst = devInstNext;
            }
            else
            {
                walkDone = 1;
                break;
            }
        }
    }	
    return NULL;
}
PCHAR CScanUsbDlg::GetRootHubName(HANDLE HostController)
{
	BOOL                success;
    ULONG               nBytes;
    USB_ROOT_HUB_NAME   rootHubName;
    PUSB_ROOT_HUB_NAME  rootHubNameW;
    PCHAR               rootHubNameA;
	
    rootHubNameW = NULL;
    rootHubNameA = NULL;
	
    // Get the length of the name of the Root Hub attached to the
    // Host Controller
    success = DeviceIoControl(HostController,
		IOCTL_USB_GET_ROOT_HUB_NAME,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -