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
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;
}
I recommend looking at the get_filename_from_devinst_and_guid function which is part of libusbp.
The basic outline is:
GUID
struct.SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
to get a list of all the present (connected) devices supporting that device interface GUID.SetupDiEnumDeviceInfo
to iterate through the list until you find the item that matches the DEVINST you already found.SetupDiEnumDeviceInterfaces
to get the list of interfaces for that item. There should only be one entry because you already specified the device interface GUID you are interested in.SetupDiGetDeviceInterfaceDetail
to get SP_DEVICE_INTERFACE_DETAIL_DATA_A
data structure.DevicePath
member of that structure.