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

📄 usbtree.cpp

📁 列举出WINDOWS中所有的USB设备的一个小程序。很实用
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                              &driverKeyName,
                              sizeof(driverKeyName),
                              &nBytes,
                              NULL);

    if (!bSuccess)
    {
		dwRV = UT_UNABLE_GET_DRVNAME;
        goto exitGetDriverKeyName;
    }

    // Allocate space to hold the driver key name
    //
	nBytes = driverKeyName.ActualLength;

    driverKeyNameW = (PUSB_NODE_CONNECTION_DRIVERKEY_NAME)malloc(nBytes);
    if (driverKeyNameW == NULL)
    {
		dwRV = UT_FAIL_MEM_ACCESS;
		goto exitGetDriverKeyName;
    }

    // Get the name of the driver key of the device attached to
    // the specified port.
    //
    driverKeyNameW->ConnectionIndex = dwPortIndex;
    bSuccess = DeviceIoControl(hHubDevice,
                              IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME,
                              driverKeyNameW,
                              nBytes,
                              driverKeyNameW,
                              nBytes,
                              &nBytes,
                              NULL);

    if (!bSuccess)
    {
		dwRV = UT_UNABLE_GET_DRVNAME;
        goto exitGetDriverKeyName;
    }

    // Convert the driver key name
    //
    *pdwNameLen = WideCharToMultiByte(CP_ACP, 0, driverKeyNameW->DriverKeyName,
					-1, (PCHAR)szDrvName, nBytes, NULL, NULL);

exitGetDriverKeyName:
    if (driverKeyNameW != NULL)
    {
        free(driverKeyNameW);
        driverKeyNameW = NULL;
    }

    return dwRV;
}

DWORD CUsbTree::GetExternalHubName (
	IN HANDLE	hHubDevice,
	IN DWORD	dwPortIndex,
	IN OUT LPDWORD pdwNameLen,
	OUT LPTSTR szExtHubName
)
{
	DWORD		dwRV = UT_SUCESS;
    BOOL		bSuccess;
    ULONG		nBytes;
    USB_NODE_CONNECTION_NAME    extHubName;
    PUSB_NODE_CONNECTION_NAME   extHubNameW;

    extHubNameW = NULL;

    // Get the length of the name of the external hub attached to the
    // specified port.
    //
    extHubName.ConnectionIndex = dwPortIndex;

    bSuccess = DeviceIoControl(hHubDevice,
                              IOCTL_USB_GET_NODE_CONNECTION_NAME,
                              &extHubName,
                              sizeof(extHubName),
                              &extHubName,
                              sizeof(extHubName),
                              &nBytes,
                              NULL);

    if (!bSuccess)
    {
		dwRV = UT_UNABLE_GET_NODEINFO;
        goto exitGetExternalHubName;
    }

    // Allocate space to hold the external hub name
    //
    nBytes = extHubName.ActualLength;

    extHubNameW = (PUSB_NODE_CONNECTION_NAME)malloc(nBytes);

    if (extHubNameW == NULL)
    {
		dwRV = UT_FAIL_MEM_ACCESS;
        goto exitGetExternalHubName;
    }

    // Get the name of the external hub attached to the specified port
    //
    extHubNameW->ConnectionIndex = dwPortIndex;
    bSuccess = DeviceIoControl(hHubDevice,
                              IOCTL_USB_GET_NODE_CONNECTION_NAME,
                              extHubNameW,
                              nBytes,
                              extHubNameW,
                              nBytes,
                              &nBytes,
                              NULL);

    if (!bSuccess)
    {
		dwRV = UT_UNABLE_GET_NODEINFO;
        goto exitGetExternalHubName;
    }

    // Convert the External Hub name
    //
    *pdwNameLen = WideCharToMultiByte(CP_ACP, 0, extHubNameW->NodeName,
					-1, (PCHAR)szExtHubName, nBytes, NULL, NULL);

exitGetExternalHubName:
    // There was an error, free anything that was allocated
    //
	if (extHubNameW != NULL)
    {
        free(extHubNameW);
        extHubNameW = NULL;
    }
	
    return dwRV;
}

DWORD CUsbTree::GetRootHubName (
    IN HANDLE hHcd,
	IN OUT LPDWORD pdwNameLen,
	OUT LPTSTR szRootHubName
)
{
    BOOL                bSuccess;
    ULONG               nBytes;
    USB_ROOT_HUB_NAME   rootHubName;
    PUSB_ROOT_HUB_NAME  rootHubNameW;

    rootHubNameW = NULL;

    // 获取连接到指定主机控制器的根集线器名称的长度
    bSuccess = DeviceIoControl(hHcd,
                              IOCTL_USB_GET_ROOT_HUB_NAME,
                              0,
                              0,
                              &rootHubName,
                              sizeof(rootHubName),
                              &nBytes,
                              NULL);
    if (!bSuccess)
		return UT_UNABLE_GET_ROOTHUB;

	// 分配接收UNICODE名称的内存
	nBytes = rootHubName.ActualLength;
    rootHubNameW = (PUSB_ROOT_HUB_NAME)malloc(nBytes);

    if (rootHubNameW == NULL)
        return UT_FAIL_MEM_ACCESS;

 	DWORD dwRV = UT_SUCESS;

    // 获取实际的根集线器名称
    bSuccess = DeviceIoControl(hHcd,
                              IOCTL_USB_GET_ROOT_HUB_NAME,
                              NULL,
                              0,
                              rootHubNameW,
                              nBytes,
                              &nBytes,
                              NULL);

    if (!bSuccess)
    {
		dwRV = UT_UNABLE_GET_ROOTHUB;
        goto GetRootHubNameError;
    }

    // Convert the Root Hub name
    //
    *pdwNameLen = WideCharToMultiByte(CP_ACP, 0, rootHubNameW->RootHubName,
					-1, (PCHAR)szRootHubName, nBytes, NULL, NULL);

GetRootHubNameError:
    // There was an error, free anything that was allocated
    //
    if (rootHubNameW != NULL)
    {
        free(rootHubNameW);
        rootHubNameW = NULL;
    }

    return dwRV;
}

DWORD CUsbTree::DriverNameToDeviceDesc (
	IN LPTSTR szDrvName,
	IN OUT LPDWORD pdwDescLen,
	OUT LPTSTR szDevDesc
)
{
    DEVINST     devInst;
    DEVINST     devInstNext;
    CONFIGRET   cr;
    ULONG       walkDone = 0;
    ULONG       len;
	CHAR buf[512];  // XXXXX How big does this have to be? Dynamically size it?

    // Get Root DevNode
    //
    cr = CM_Locate_DevNode(&devInst,
                           NULL,
                           0);

    if (cr != CR_SUCCESS)
    {
        return UT_UNABLE_LOCATE_NODE;
    }

    // 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(szDrvName, buf) == 0)
        {
            len = sizeof(buf);
            cr = CM_Get_DevNode_Registry_Property(devInst,
                                                  CM_DRP_DEVICEDESC,
                                                  NULL,
                                                  buf,
                                                  &len,
                                                  0);

            if (cr == CR_SUCCESS)
            {
				if ( *pdwDescLen < len )
				{
					 *pdwDescLen = len;
					 return UT_BUFFER_TOO_SMALL;
				}
				*pdwDescLen = len;
				lstrcpy(szDevDesc, buf);
                return UT_SUCESS;
            }
            else
            {
                return UT_UNABLE_GET_DEVDESC;
            }
        }

        // 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 UT_SUCESS;
}

⌨️ 快捷键说明

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