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

📄 usbviewdlg.cpp

📁 使用visual c++实现的usbview
💻 CPP
📖 第 1 页 / 共 5 页
字号:
				return TRUE;
			
//			(PUCHAR)commonDesc += commonDesc->bLength;	//stam
			continue;
			
		default:
//			(PUCHAR)commonDesc += commonDesc->bLength;	//stam
			continue;
        }
        break;
    }
	
    return FALSE;
}

//*****************************************************************************
// AddLeaf()
//*****************************************************************************
HTREEITEM CUsbviewDlg::AddLeaf(HTREEITEM hTreeParent,LPARAM lParam,LPTSTR lpszText,...)
{
    TCHAR           szBuffer[1024];
    va_list         list;
    TV_INSERTSTRUCT tvins;
    HTREEITEM       hti;

    // added for tree view icons
    PUSB_NODE_CONNECTION_INFORMATION ConnectInfo = NULL;

    if (lParam)
        ConnectInfo = ((PUSBDEVICEINFO)lParam)->ConnectionInfo;

    // end add
    va_start(list, lpszText);
    vsprintf(szBuffer,lpszText,list);
    memset(&tvins,0,sizeof(tvins));

    // Set the parent item
    tvins.hParent = hTreeParent;
    tvins.hInsertAfter = TVI_LAST;
    // pszText and lParam members are valid
    tvins.item.mask = TVIF_TEXT | TVIF_PARAM;
    // Set the text of the item.
    tvins.item.pszText = szBuffer;
    // Set the user context item
    tvins.item.lParam = lParam;
    // Add the item to the tree-view control.
    hti = TreeView_InsertItem(ghTreeWnd, &tvins);

	// added
    tvins.item.mask		= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
    tvins.item.hItem	= hti;
    // Determine which icon to display for the device
    tvins.item.iImage			= giUsbDevice;
    tvins.item.iSelectedImage	= giUsbDevice;
	
    if (!ConnectInfo)
    {
        if (!(strcmp("USB root", lpszText)))
		{
			tvins.item.iImage			= giComputer;
			tvins.item.iSelectedImage	= giComputer;
		}
		else
		{
            //it is the host controller
		}
		
    }
    else
    {
        if (ConnectInfo->DeviceIsHub)  // Device is a Hub!!
        {
            tvins.item.iImage			= giHub;
            tvins.item.iSelectedImage	= giHub;
        }
		
        if (!ConnectInfo->CurrentConfigurationValue)
        {
            tvins.item.iImage			= giBangDevice;
            tvins.item.iSelectedImage	= giBangDevice;
			
            if (NoDeviceConnected == ConnectInfo->ConnectionStatus) // Empty Port
            {
                tvins.item.iImage			= giPort;
                tvins.item.iSelectedImage	= giPort;
            }
        }
    }
	
    TreeView_SetItem(ghTreeWnd, &tvins.item);

    return hti;
}

void CUsbviewDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	HTREEITEM hTreeItem;
	hTreeItem = ((NM_TREEVIEW *)pNMHDR)->itemNew.hItem;
	
	UpdateEditControl(hTreeItem);
	
	*pResult = 0;
}


VOID CUsbviewDlg::UpdateEditControl(HTREEITEM hTreeItem)
{
    TV_ITEM         tvi;
    PUSBDEVICEINFO  info;
	
    // Start with an empty text buffer.
    if (!ResetTextBuffer())
        return;
	
    tvi.mask	= TVIF_HANDLE | TVIF_TEXT | TVIF_PARAM;
    tvi.hItem	= hTreeItem;
    tvi.pszText = (LPSTR)TextBuffer;
    tvi.cchTextMax = TextBufferLen-2;  // leave space for "\r\n'
	
    TreeView_GetItem(ghTreeWnd,&tvi);
	
    info = (PUSBDEVICEINFO)tvi.lParam;
	
    // If we didn't store any info for the item, just display the item's
    // name, else display the info we stored for the item.
    if (info != NULL)
    {
        if (info->ConnectionInfo == NULL)
        {
            // Must be Root HUB, external devices have Connection Info
            AppendTextBuffer("Root Hub: %s\r\n",info->HubName);
            DisplayHubInfo(&info->HubInfo->u.HubInformation);
        }
        else
        {
            if (info->HubInfo != NULL)
            {
                // Must be external HUB external
                AppendTextBuffer("External Hub: %s\r\n",info->HubName);		
                DisplayHubInfo(&info->HubInfo->u.HubInformation);
            }
            else
            {
                // Must be external non-HUB device.  Nothing special to
                // display here that isn't displayed in connection info
                // below.
            }
			
            // Display info common to any external device
            DisplayConnectionInfo(info->ConnectionInfo,info->StringDescs);
			
			if(info->ConfigDesc)
				DisplayConfigDesc((PUSB_CONFIGURATION_DESCRIPTOR)(info->ConfigDesc+1),
				info->StringDescs);
        }
    }
	
    // All done formatting text buffer with info, now update the edit
    // control with the contents of the text buffer
    GetDlgItem(IDC_EDIT)->SetWindowText(TextBuffer);
}

BOOL CUsbviewDlg::CreateTextBuffer()
{
    //分配内存
    TextBuffer = (CHAR*)GlobalAlloc(GPTR,BUFFERALLOCINCREMENT);
	
    if (TextBuffer == NULL)
        return FALSE;
	
    TextBufferLen	= BUFFERALLOCINCREMENT;
	
    *TextBuffer		= 0;
    TextBufferPos	= 0;
	
    return TRUE;
}

BOOL CUsbviewDlg::ResetTextBuffer()
{
    if (TextBuffer==NULL)
        return FALSE;
	
    // Reset the buffer position and terminate the buffer
    *TextBuffer = 0;
    TextBufferPos = 0;
	
    return TRUE;
}

VOID __cdecl CUsbviewDlg::AppendTextBuffer(LPCTSTR lpFormat,...)
{
    va_list arglist;
    va_start(arglist, lpFormat);
	
    // Make sure we have a healthy amount of space free in the buffer,
    // reallocating the buffer if necessary.
    if (TextBufferLen - TextBufferPos < BUFFERMINFREESPACE)
    {
        CHAR *TextBufferTmp;
        TextBufferTmp = (CHAR*)GlobalReAlloc(TextBuffer, TextBufferLen+BUFFERALLOCINCREMENT,
			GMEM_MOVEABLE|GMEM_ZEROINIT);
        if (TextBufferTmp != NULL)
        {
            TextBuffer = TextBufferTmp;
            TextBufferLen += BUFFERALLOCINCREMENT;
        }
        else
        {
            return;
        }
    }
	
    //wvsprintf的返回值为arglist指向字符串长度与lpFormat指向字符串长度的总和(不包
	//括原始TextBuffer中字符串的长度.
    TextBufferPos+=wvsprintf(TextBuffer+TextBufferPos,lpFormat,arglist);
}

VOID CUsbviewDlg::DisplayHubInfo(PUSB_HUB_INFORMATION HubInfo)
{

    USHORT wHubChar;
    AppendTextBuffer("Hub Power:               %s\r\n",
                     HubInfo->HubIsBusPowered ?
                     "Bus Power" : "Self Power");
    AppendTextBuffer("Number of Ports:         %d\r\n",
                     HubInfo->HubDescriptor.bNumberOfPorts);
    wHubChar = HubInfo->HubDescriptor.wHubCharacteristics;
    switch (wHubChar & 0x0003)
    {
        case 0x0000:
            AppendTextBuffer("Power switching:         Ganged\r\n");
            break;
        case 0x0001:
            AppendTextBuffer("Power switching:         Individual\r\n");
            break;
        case 0x0002:
        case 0x0003:
            AppendTextBuffer("Power switching:         None\r\n");
            break;
    }

    switch (wHubChar & 0x0004)
    {
        case 0x0000:
            AppendTextBuffer("Compound device:         No\r\n");
            break;
        case 0x0004:
            AppendTextBuffer("Compound device:         Yes\r\n");
            break;
    }

    switch (wHubChar & 0x0018)
    {
        case 0x0000:
            AppendTextBuffer("Over-current Protection: Global\r\n");
            break;
        case 0x0008:
            AppendTextBuffer("Over-current Protection: Individual\r\n");
            break;
        case 0x0010:
        case 0x0018:
            AppendTextBuffer("No Over-current Protection (Bus Power Only)\r\n");
            break;
    }
    AppendTextBuffer("\r\n");
}

//*****************************************************************************
// DisplayConnectionInfo()
// ConnectInfo - Info about the connection.
//*****************************************************************************
VOID CUsbviewDlg::DisplayConnectionInfo(
	PUSB_NODE_CONNECTION_INFORMATION    ConnectInfo,
	PSTRING_DESCRIPTOR_NODE             StringDescs)
{

    if (ConnectInfo->ConnectionStatus == NoDeviceConnected)
        AppendTextBuffer("ConnectionStatus: NoDeviceConnected\r\n");
    else
    {
        PCHAR VendorString;
        AppendTextBuffer("Device Descriptor:\r\n");
        AppendTextBuffer("bcdUSB:             0x%04X\r\n",
                         ConnectInfo->DeviceDescriptor.bcdUSB);
        AppendTextBuffer("bDeviceClass:         0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.bDeviceClass);
        AppendTextBuffer("bDeviceSubClass:      0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.bDeviceSubClass);
        AppendTextBuffer("bDeviceProtocol:      0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.bDeviceProtocol);
        AppendTextBuffer("bMaxPacketSize0:      0x%02X (%d)\r\n",
                         ConnectInfo->DeviceDescriptor.bMaxPacketSize0,
                         ConnectInfo->DeviceDescriptor.bMaxPacketSize0);
        VendorString = GetVendorString(ConnectInfo->DeviceDescriptor.idVendor);
        if (VendorString != NULL)
            AppendTextBuffer("idVendor:           0x%04X (%s)\r\n",
                             ConnectInfo->DeviceDescriptor.idVendor,
                             VendorString);
        else
            AppendTextBuffer("idVendor:           0x%04X\r\n",
                             ConnectInfo->DeviceDescriptor.idVendor);

        AppendTextBuffer("idProduct:          0x%04X\r\n",
                         ConnectInfo->DeviceDescriptor.idProduct);
        AppendTextBuffer("bcdDevice:          0x%04X\r\n",
                         ConnectInfo->DeviceDescriptor.bcdDevice);
        AppendTextBuffer("iManufacturer:        0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.iManufacturer);
        if (ConnectInfo->DeviceDescriptor.iManufacturer)
            DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iManufacturer,
                                    StringDescs);

        AppendTextBuffer("iProduct:             0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.iProduct);

        if (ConnectInfo->DeviceDescriptor.iProduct)
            DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iProduct,
                                    StringDescs);

        AppendTextBuffer("iSerialNumber:        0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.iSerialNumber);

        if (ConnectInfo->DeviceDescriptor.iSerialNumber)
            DisplayStringDescriptor(ConnectInfo->DeviceDescriptor.iSerialNumber,
                                    StringDescs);

        AppendTextBuffer("bNumConfigurations:   0x%02X\r\n",
                         ConnectInfo->DeviceDescriptor.bNumConfigurations);
        AppendTextBuffer("\r\nConnectionStatus: %s\r\n",
                         PConnectionStatuse[ConnectInfo->ConnectionStatus]);
        AppendTextBuffer("Current Config Value: 0x%02X\r\n",
                         ConnectInfo->CurrentConfigurationValue);

        if (ConnectInfo->LowSpeed)
            AppendTextBuffer("Device Bus Speed:      Low\r\n");
        else
            AppendTextBuffer("Device Bus Speed:     Full\r\n");

        AppendTextBuffer("Device Address:       0x%02X\r\n",
                         ConnectInfo->DeviceAddress);
        AppendTextBuffer("Open Pipes:             %2d\r\n",
                         ConnectInfo->NumberOfOpenPipes);
        if (ConnectInfo->NumberOfOpenPipes)
            DisplayPipeInfo(ConnectInfo->NumberOfOpenPipes,
                            ConnectInfo->PipeList);
    }
}

PCHAR CUsbviewDlg::GetVendorString (USHORT idVendor)
{
    PUSBVENDORID vendorID;
	
    if (idVendor != 0x0000)
    {
        vendorID = USBVendorIDs;
		
        while (vendorID->usVendorID != 0x0000)
        {
            if (vendorID->usVendorID == idVendor)
                return (vendorID->szVendor);

            vendorID++;
        }
    }
	
    return NULL;
}

VOID CUsbviewDlg::DisplayStringDescriptor(
	UCHAR                       Index,
	PSTRING_DESCRIPTOR_NODE     StringDescs)
{
    ULONG nBytes;
    while (StringDescs)
    {
        if (StringDescs->DescriptorIndex == Index)
        {
            AppendTextBuffer("0x%04X: \"",
				StringDescs->LanguageID);
			
            nBytes = WideCharToMultiByte(
				CP_ACP,     // CodePage
				0,          // CodePage
				StringDescs->StringDescriptor->bString,
				(StringDescs->StringDescriptor->bLength - 2) / 2,
				TextBuffer + TextBufferPos,
				TextBufferLen - TextBufferPos,
				NULL,       // lpDefaultChar
				NULL);      // pUsedDefaultChar
			
            TextBufferPos += nBytes;		
            AppendTextBuffer("\"\r\n");
        }
		
        StringDescs = StringDescs->Next;
    }
	return;
}

VOID CUsbviewDlg::DisplayPipeInfo(ULONG NumPipes,USB_PIPE_INFO *PipeInfo)
{
    ULONG i;
    for (i=0; i<NumPipes; i++)
        DisplayEndpointDescriptor(&PipeInfo[i].EndpointDescriptor);
	
	return;
}

VOID CUsbviewDlg::DisplayEndpointDescriptor(PUSB_ENDPOINT_DESCRIPTOR EndpointDesc)
{
	
    AppendTextBuffer("\r\nEndpoint Descriptor:\r\n");
    AppendTextBuffer("bEndpointAddress:     0x%02X\r\n",
		EndpointDesc->bEndpointAddress);
	
    switch (EndpointDesc->bmAttributes & 0x03)
    {
	case 0x00:
		AppendTextBuffer("Transfer Type:     Control\r\n");
		break;		
	case 0x01:
		AppendTextBuffer("Transfer Type: Isochronous\r\n");
		break;		
	case 0x02:
		AppendTextBuffer("Transfer Type:        Bulk\r\n");
		break;	

⌨️ 快捷键说明

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