I have a DEVINST, I need the Device Path

后端 未结 2 1352
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 01:50

I\'m trying to open a WinUSB device that is part of a composite device. I have located the correct child device using cfgmgr32, and have its DEVINST number. In order to open

2条回答
  •  被撕碎了的回忆
    2021-01-16 02:25

    This function returns a list of NULL-terminated Device Paths (that's what we get from CM_Get_Device_Interface_List)

    You need to pass it the DEVINST, and the wanted interface GUID.

    Since both the DEVINST and interface GUID are specified, it is highly likely that CM_Get_Device_Interface_List will return a single Device Path for that interface, but technically you should be prepared to get more than one result.

    I used this function successfully in production code for getting the Device Interface of a USB HUB (GUID_CLASS_USBHUB): I used the resulting Device Path with CreateFile and opened it succesfully.

    It is responsibility of the caller to delete[] the returned list if the function returns successfully (return code 0)

    int GetDevInstInterfaces(DEVINST dev, LPGUID interfaceGUID, wchar_t**outIfaces, ULONG* outIfacesLen)
    {
        CONFIGRET cres;
        if (!outIfaces)
            return -1;
        if (!outIfacesLen)
            return -2;
    
        // Get System Device ID
        WCHAR sysDeviceID[256];
        cres = CM_Get_Device_ID(dev, sysDeviceID, sizeof(sysDeviceID) / sizeof(sysDeviceID[0]), 0);
        if (cres != CR_SUCCESS)
            return -11;
    
        // Get list size
        ULONG ifaceListSize = 0;
        cres = CM_Get_Device_Interface_List_Size(&ifaceListSize, interfaceGUID, sysDeviceID, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
        if (cres != CR_SUCCESS)
            return -12;
    
        // Allocate memory for the list
        wchar_t* ifaceList = new wchar_t[ifaceListSize];
    
        // Populate the list
        cres = CM_Get_Device_Interface_List(interfaceGUID, sysDeviceID, ifaceList, ifaceListSize, CM_GET_DEVICE_INTERFACE_LIST_PRESENT);
        if (cres != CR_SUCCESS) {
            delete[] ifaceList;
            return -13;
        }
    
        // Return list
        *outIfaces = ifaceList;
        *outIfacesLen = ifaceListSize;
    
        return 0;
    }
    

提交回复
热议问题