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

📄 descriptors.cpp

📁 這個程式把主機所有設備的描述元列舉出來﹐包括所有標准描述元和非標准描述元。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    Show(pEd, "wTotalLength", pCD->wTotalLength);
    Show(pEd, "bNumInterfaces", pCD->bNumInterfaces);
    Show(pEd, "bConfigurationValue", pCD->bConfigurationValue);
    Show(pEd, "iConfiguration", pCD->iConfiguration);
    Show(pEd, "bmAttributes", pCD->bmAttributes);
    Show(pEd, "MaxPower", pCD->MaxPower);
	Show(pEd, "", "");
}

void CDescriptors::Show(CEdit *pEd, USB_ENDPOINT_DESCRIPTOR *pEPD)
{
	Show(pEd, "", "");
	Show(pEd, "Endpoint Descriptor", "");
	Show(pEd, "bLength", pEPD->bLength);
    Show(pEd, "bDescriptorType", pEPD->bDescriptorType);
    Show(pEd, "bEndpointAddress", pEPD->bEndpointAddress);
    Show(pEd, "bmAttributes", pEPD->bmAttributes);
    Show(pEd, "wMaxPacketSize", pEPD->wMaxPacketSize);
    Show(pEd, "bInterval", pEPD->bInterval);
	Show(pEd, "", "");
}

void CDescriptors::Show(CEdit *pEd, USB_INTERFACE_DESCRIPTOR *pID)
{
	Show(pEd, "", "");
	Show(pEd, "Interface Descriptor", "");
	Show(pEd, "bLength", pID->bLength);
    Show(pEd, "bDescriptorType", pID->bDescriptorType);
    Show(pEd, "bInterfaceNumber", pID->bInterfaceNumber);
    Show(pEd, "bAlternateSetting", pID->bAlternateSetting);
    Show(pEd, "bNumEndpoints", pID->bNumEndpoints);
    Show(pEd, "bInterfaceClass", pID->bInterfaceClass);
    Show(pEd, "bInterfaceSubClass", pID->bInterfaceSubClass);
    Show(pEd, "bInterfaceProtocol", pID->bInterfaceProtocol);
    Show(pEd, "iInterface", pID->iInterface);
	Show(pEd, "", "");
}

void CDescriptors::Show(CEdit *pEd, CString strDesc, USB_STRING_DESCRIPTOR *pSD)
{
	CString str = ToString(pSD->bString);
	Show(pEd, strDesc, str);
}

HANDLE CDescriptors::GetHostController(long nHC)
{
	CString strName;
	strName.Format("\\\\.\\HCD%d", nHC);
	return CreateFile(strName, GENERIC_WRITE, FILE_SHARE_WRITE,
					  NULL, OPEN_EXISTING, 0, NULL);
}

BOOL CDescriptors::GetRootHubName(CString& strRH, HANDLE hHostController)
{
	ULONG nBytes;
    USB_ROOT_HUB_NAME hubname, *phubname;
	if(!DeviceIoControl(hHostController, IOCTL_USB_GET_ROOT_HUB_NAME, 
						NULL, 0, &hubname, sizeof(hubname), &nBytes, NULL))
		return FALSE;

    nBytes = hubname.ActualLength;
    phubname = (USB_ROOT_HUB_NAME*)malloc(nBytes);
    if(!phubname) return FALSE;
    if(DeviceIoControl(hHostController, IOCTL_USB_GET_ROOT_HUB_NAME,
					   NULL, 0, phubname, nBytes, &nBytes, NULL))
	{
		strRH = ToString(phubname->RootHubName);
		free(phubname);
		return TRUE;
	}
	free(phubname);

	return FALSE;
}

HANDLE CDescriptors::GetHub(CString strHubName)
{
	strHubName = "\\\\.\\" + strHubName;
    return CreateFile(strHubName, GENERIC_WRITE, FILE_SHARE_WRITE,
					  NULL, OPEN_EXISTING, 0, NULL);
}

BOOL CDescriptors::GetExternalHubName(CString& strHub, HANDLE hHub, long nPort)
{
    ULONG nBytes;
    USB_NODE_CONNECTION_NAME hubname, *phubname;
    hubname.ConnectionIndex = nPort;

    if(!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME, &hubname,
						sizeof(hubname), &hubname, sizeof(hubname), &nBytes, NULL))
		return FALSE;

	nBytes = hubname.ActualLength;
    phubname = (USB_NODE_CONNECTION_NAME*)malloc(nBytes);
    if(!phubname) return FALSE;

    hubname.ConnectionIndex = nPort;
    if(DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_NAME, phubname,
					   nBytes, phubname, nBytes, &nBytes, NULL))
	{
		strHub = ToString(phubname->NodeName);
		free(phubname);
		return TRUE;
	}
	free(phubname);

	return FALSE;
}

long CDescriptors::GetNoOfPort(HANDLE hHub)
{
    ULONG nBytes;
    USB_NODE_INFORMATION nodeInfo;
	if(!DeviceIoControl(hHub, IOCTL_USB_GET_NODE_INFORMATION, &nodeInfo,
                        sizeof(nodeInfo), &nodeInfo, sizeof(nodeInfo),
						&nBytes, NULL))
		return -1;

	return nodeInfo.u.HubInformation.HubDescriptor.bNumberOfPorts;
}

BOOL CDescriptors::GetPortConnInfo(USB_NODE_CONNECTION_INFORMATION *info, HANDLE hHub, long nPort)
{
    ULONG nBytes = sizeof(USB_NODE_CONNECTION_INFORMATION) +
				   sizeof(USB_PIPE_INFO) * 30; //15 IN + 15 OUT pipes

	info->ConnectionIndex = nPort;
    return DeviceIoControl(hHub, IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
						   info, nBytes, info, nBytes, &nBytes, NULL);
}

USB_DESCRIPTOR_REQUEST* CDescriptors::GetDeviceDesc(HANDLE hHub, ULONG nPort)
{
    ULONG nBytes = sizeof(USB_DESCRIPTOR_REQUEST) +
				   sizeof(USB_DEVICE_DESCRIPTOR);
	USB_DESCRIPTOR_REQUEST* pRequest;
    if((pRequest = (USB_DESCRIPTOR_REQUEST*)malloc(nBytes))==NULL) return NULL;
    USB_DEVICE_DESCRIPTOR* pDesc = (USB_DEVICE_DESCRIPTOR*)(pRequest->Data);
    memset((void*)pRequest, 0, nBytes);
	
    pRequest->ConnectionIndex = nPort;
	pRequest->SetupPacket.wValue = (USB_DEVICE_DESCRIPTOR_TYPE << 8)
                                   | 0;
    pRequest->SetupPacket.wLength = (USHORT)sizeof(USB_DEVICE_DESCRIPTOR);
	pRequest->SetupPacket.bmRequest = 0x80;
    pRequest->SetupPacket.bRequest  = 0x06;

    ULONG nBytesGet;
    if(!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
                        pRequest, nBytes, pRequest, nBytes, &nBytesGet, NULL) ||
	   nBytes!=nBytesGet ||
	   pDesc->bLength<sizeof(USB_DEVICE_DESCRIPTOR))
	{
		free(pRequest);
		return NULL;
	}

    return pRequest;
}

USB_DESCRIPTOR_REQUEST* CDescriptors::GetConfigDesc(HANDLE hHub, ULONG nPort, UCHAR nConfig)
{
    ULONG nBytes = sizeof(USB_DESCRIPTOR_REQUEST) +
				   sizeof(USB_CONFIGURATION_DESCRIPTOR);
	USB_DESCRIPTOR_REQUEST* pRequest;
    if((pRequest = (USB_DESCRIPTOR_REQUEST*)malloc(nBytes))==NULL) return NULL;
    USB_CONFIGURATION_DESCRIPTOR* pDesc = (USB_CONFIGURATION_DESCRIPTOR*)(pRequest->Data);
    memset((void*)pRequest, 0, nBytes);
	
    pRequest->ConnectionIndex = nPort;
	pRequest->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
                                   | nConfig;
    pRequest->SetupPacket.wLength = (USHORT)sizeof(USB_CONFIGURATION_DESCRIPTOR);
	pRequest->SetupPacket.bmRequest = 0x80;
    pRequest->SetupPacket.bRequest  = 0x06;

	//request the config descriptor only
    ULONG nBytesGet;
    if(!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
                        pRequest, nBytes, pRequest, nBytes, &nBytesGet, NULL) ||
	   nBytes!=nBytesGet ||
	   pDesc->wTotalLength<sizeof(USB_CONFIGURATION_DESCRIPTOR))
	{
		free(pRequest);
		return NULL;
	}

	//request the config descriptor and other descriptors
    nBytes = sizeof(USB_DESCRIPTOR_REQUEST) + pDesc->wTotalLength;
	free(pRequest);
    if((pRequest = (USB_DESCRIPTOR_REQUEST*)malloc(nBytes))==NULL) return NULL;
    pDesc = (USB_CONFIGURATION_DESCRIPTOR*)(pRequest->Data);
  
	memset((void*)pRequest, 0, nBytes);
    pRequest->ConnectionIndex = nPort;
	pRequest->SetupPacket.wValue = (USB_CONFIGURATION_DESCRIPTOR_TYPE << 8)
                                  | nConfig;
    pRequest->SetupPacket.wLength = (USHORT)(nBytes - sizeof(USB_DESCRIPTOR_REQUEST));
	pRequest->SetupPacket.bmRequest = 0x80;
    pRequest->SetupPacket.bRequest  = 0x06;

    if(!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
                        pRequest, nBytes, pRequest, nBytes, &nBytesGet, NULL) ||
	   nBytes!=nBytesGet ||
	   pDesc->wTotalLength<sizeof(USB_CONFIGURATION_DESCRIPTOR))
	{
		free(pRequest);
		return NULL;
	}

    return pRequest;
}

USB_DESCRIPTOR_REQUEST* CDescriptors::GetStringDesc(HANDLE hHub, ULONG nPort, 
													UCHAR nDesc, USHORT nLangID)
{
    ULONG nBytes = sizeof(USB_DESCRIPTOR_REQUEST) +
				   MAXIMUM_USB_STRING_LENGTH;
	USB_DESCRIPTOR_REQUEST* pRequest;
    if((pRequest = (USB_DESCRIPTOR_REQUEST*)malloc(nBytes))==NULL) return NULL;
    USB_STRING_DESCRIPTOR* pDesc = (USB_STRING_DESCRIPTOR*)(pRequest->Data);
    memset((void*)pRequest, 0, nBytes);
	
    pRequest->ConnectionIndex = nPort;
	pRequest->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8)
                                   | nDesc;
	pRequest->SetupPacket.wIndex = nLangID;
    pRequest->SetupPacket.wLength = (USHORT)MAXIMUM_USB_STRING_LENGTH;
	pRequest->SetupPacket.bmRequest = 0x80;
    pRequest->SetupPacket.bRequest  = 0x06;

    ULONG nBytesGet;
    if(!DeviceIoControl(hHub, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
                        pRequest, nBytes, pRequest, nBytes, &nBytesGet, NULL) ||
	   pDesc->bLength>MAXIMUM_USB_STRING_LENGTH)
	{
		free(pRequest);
		return NULL;
	}

    return pRequest;
}

BOOL CDescriptors::IsStringDescExist(USB_DEVICE_DESCRIPTOR *pDD, USB_CONFIGURATION_DESCRIPTOR *pCD)
{
    if(pDD->iManufacturer || pDD->iProduct || pDD->iSerialNumber)
		return TRUE;

    PUCHAR pEnd = (PUCHAR)pCD + pCD->wTotalLength;
    USB_COMMON_DESCRIPTOR* pCurDesc = (USB_COMMON_DESCRIPTOR*)pCD;

    while((PUCHAR)pCurDesc + sizeof(USB_COMMON_DESCRIPTOR)<pEnd &&
          (PUCHAR)pCurDesc + pCurDesc->bLength <= pEnd)
    {
        switch(pCurDesc->bDescriptorType)
        {
			case USB_CONFIGURATION_DESCRIPTOR_TYPE:
                if(pCurDesc->bLength != sizeof(USB_CONFIGURATION_DESCRIPTOR))
					return FALSE;
                if(((USB_CONFIGURATION_DESCRIPTOR*)pCurDesc)->iConfiguration)
                    return TRUE;
                break;

            case USB_INTERFACE_DESCRIPTOR_TYPE:
                if(pCurDesc->bLength != sizeof(USB_INTERFACE_DESCRIPTOR))
					return FALSE;
                if(((USB_INTERFACE_DESCRIPTOR*)pCurDesc)->iInterface)
                    return TRUE;
                break;

            default: break;
        }
		pCurDesc = (USB_COMMON_DESCRIPTOR*)((PUCHAR)(pCurDesc) + pCurDesc->bLength);
    }

    return FALSE;
}

⌨️ 快捷键说明

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