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

📄 usbviewdlg.cpp

📁 使用visual c++实现的usbview
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			
            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;
}

void CUsbviewDlg::OnRefresh() 
{
	GetDlgItem(IDC_EDIT)->SetWindowText("");
	if(CreateTextBuffer()==FALSE)
		return;

	m_tree.DeleteAllItems();
	m_hRoot=NULL;
	m_hRoot=AddLeaf(TVI_ROOT, 0, "USB root");
	EnumerateHostControllers(m_hRoot);
}


PCHAR CUsbviewDlg::WideStrToMultiStr (PWCHAR WideStr)
{
    ULONG nBytes;
    PCHAR MultiStr;
	
    // Get the length of the converted string
    nBytes = WideCharToMultiByte(
		CP_ACP,
		0,
		WideStr,
		-1,
		NULL,
		0,
		NULL,
		NULL);
	
    if (nBytes == 0)
        return NULL;
	
    // Allocate space to hold the converted string
    MultiStr = (PCHAR)GlobalAlloc(GPTR,nBytes);
    if (MultiStr == NULL)
        return NULL;
	
    // Convert the string
    nBytes = WideCharToMultiByte(
		CP_ACP,
		0,
		WideStr,
		-1,
		MultiStr,
		nBytes,
		NULL,
		NULL);
	
    if (nBytes == 0)
    {
        GlobalFree(MultiStr);
        return NULL;
    }
	
    return MultiStr;
}

PCHAR CUsbviewDlg::GetHCDDriverKeyName(HANDLE  HCD)
{
    BOOL                    success;
    ULONG                   nBytes;
    USB_HCD_DRIVERKEY_NAME  driverKeyName;
    PUSB_HCD_DRIVERKEY_NAME driverKeyNameW;
    PCHAR                   driverKeyNameA;
	
    driverKeyNameW = NULL;
    driverKeyNameA = NULL;
	
    // Get the length of the name of the driver key of the HCD
    success = DeviceIoControl(HCD,
		IOCTL_GET_HCD_DRIVERKEY_NAME,
		&driverKeyName,
		sizeof(driverKeyName),
		&driverKeyName,
		sizeof(driverKeyName),
		&nBytes,
		NULL);
		/*++
		BOOL DeviceIoControl( 
		HANDLE hDevice,				// 设备句柄 
		DWORD dwIoControlCode,		// IOCTL请求操作代码 
		LPVOID lpInBuffer,			// 输入缓冲区地址 
		DWORD nInBufferSize,		// 输入缓冲区大小 
		LPVOID lpOutBuffer,			// 输出缓冲区地址 
		DWORD nOutBufferSize,		// 输出缓冲区大小 
		LPDWORD lpBytesReturned,	// 存放返回字节数的指针 
		LPOVERLAPPED lpOverlapped	// 用于同步操作的Overlapped结构体指针 
		); 
	--*/	
    if (!success)
        goto GetHCDDriverKeyNameError;
	
    // Allocate space to hold the driver key name
    nBytes = driverKeyName.ActualLength;
	
    if (nBytes<= sizeof(driverKeyName))
        goto GetHCDDriverKeyNameError;
	
    driverKeyNameW = (PUSB_HCD_DRIVERKEY_NAME)GlobalAlloc(GPTR,nBytes);
	
    if (driverKeyNameW == NULL)
        goto GetHCDDriverKeyNameError;
	
    // Get the name of the driver key of the device attached to
    // the specified port.
    success = DeviceIoControl(HCD,
		IOCTL_GET_HCD_DRIVERKEY_NAME,
		driverKeyNameW,
		nBytes,
		driverKeyNameW,
		nBytes,
		&nBytes,
		NULL);
	
    if (!success)
        goto GetHCDDriverKeyNameError;
	
    // Convert the driver key name
    driverKeyNameA = WideStrToMultiStr(driverKeyNameW->DriverKeyName);
	
    // All done, free the uncoverted driver key name and return the
    // converted driver key name
    GlobalFree(driverKeyNameW);
    return driverKeyNameA;
	
	
GetHCDDriverKeyNameError:
    // There was an error, free anything that was allocated
    if (driverKeyNameW != NULL)
    {
        GlobalFree(driverKeyNameW);
        driverKeyNameW = NULL;
    }
	
    return NULL;
}
/*++
*函数:
	GetDriverKeyName();
*参数:
	Hub	打开的host controller句柄
	ConnectionIndex	此设备在上一个hub中的索引号(从 1 开始)
*作用:
	获取host controller的driver key name名称
*返回:
	非NULL	成功,返回driver key name
	NULL	失败
--*/
PCHAR CUsbviewDlg::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;

	//在连接的指定port上获取设备的driver key name的长度
    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;
	
    //分配内存保存driver key name
    nBytes=driverKeyName.ActualLength;	
    if(nBytes<=sizeof(driverKeyName))
		goto GetDriverKeyNameError;
	
    driverKeyNameW=(PUSB_NODE_CONNECTION_DRIVERKEY_NAME)GlobalAlloc(GPTR,nBytes);

    if (driverKeyNameW == NULL)
        goto GetDriverKeyNameError;
	
    //在连接的指定port上获取设备的driver key name
    driverKeyNameW->ConnectionIndex = ConnectionIndex;
    success=DeviceIoControl(Hub,
		IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
		driverKeyNameW,
		nBytes,
		driverKeyNameW,
		nBytes,
		&nBytes,
		NULL);
	
    if (!success)
        goto GetDriverKeyNameError;
	
    //unicode到multi
    driverKeyNameA=WideStrToMultiStr(driverKeyNameW->DriverKeyName);
    GlobalFree(driverKeyNameW);	
    return driverKeyNameA;
	
GetDriverKeyNameError:
    // There was an error, free anything that was allocated
    if (driverKeyNameW != NULL)
    {
        GlobalFree(driverKeyNameW);
        driverKeyNameW = NULL;
    }
	
    return NULL;
}

/*++
*函数:
	GetConfigDescriptor();
*参数:
	hHubDevice		要枚举的hub device的句柄
	ConnectionIndex		此hub上的port的编号
	DescriptorIndex		configuration descriptor index,从零开始
*作用:
	如果有设备连接到port上,从设备获取设备的configuration descriptor
*返回:
	PUSB_DESCRIPTOR_REQUEST
--*/
PUSB_DESCRIPTOR_REQUEST CUsbviewDlg::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;
}

/*++
*函数:
	AreThereStringDescriptors();
*参数:
	DeviceDesc	
	ConfigDesc		
*作用:
	枚举root hub name下面的usb hub
*返回:
	TRUE	成功
	FALSE	失败
--*/
BOOL CUsbviewDlg::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;
			
//			(PUCHAR)commonDesc += commonDesc->bLength;	//stam
			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)

⌨️ 快捷键说明

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