I'm trying to get device interfaces data via SetupDiEnumDeviceInterfaces()
for all devices which correspond to a specific PnP enumerator. Consider the following example (for interface with index 0 only):
#include <stdio.h>
#include <Windows.h>
#include <SetupAPI.h>
static int get_interface(HDEVINFO);
int main()
{
HDEVINFO devInfoSet = SetupDiGetClassDevsA(NULL, "USBSTOR", NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT);
if (devInfoSet == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "SetupDiGetClassDevsA: Error %lu\n", GetLastError());
return 1;
}
int ret = get_interface(devInfoSet);
SetupDiDestroyDeviceInfoList(devInfoSet);
return ret;
}
int get_interface(HDEVINFO devInfoSet)
{
SP_DEVICE_INTERFACE_DATA devIface = { sizeof(SP_DEVICE_INTERFACE_DATA) };
if (!SetupDiEnumDeviceInterfaces(devInfoSet, NULL, &GUID_DEVINTERFACE_DISK, 0, &devIface))
{
fprintf(stderr, "SetupDiEnumDeviceInterfaces: Error %lu\n", GetLastError());
return 1;
}
/* ... */
}
(A more complete version of this example is available here.)
Upon running that, SetupDiEnumDeviceInterfaces()
fails with GetLastError()
returning error 259 (ERROR_NO_MORE_ITEMS
), as if there weren't any interfaces for the device info set to begin with. It seems to be the behavior for all device info sets obtained for device setup classes rather than device interface classes.
However when I add DIGCF_DEVICEINTERFACE
flag to the last argument of SetupDiGetClassDevsA()
call, so that the set is obtained for interface classes, the latter fails with error 13 (ERROR_INVALID_DATA
). The same error usually ensues in case of device setup classes when the PnP enumerator specified is not registered within the system. Indeed, when I replace the enumerator argument with NULL
, the interface is obtained successfully. According to MSDN, specifying DIGCF_DEVICEINTERFACE
flag allows the use of a device instance id in place of the enumerator argument, but it occurs to me that aforementioned device instance ids and NULL
are the only options for SetupDiGetClassDevs()
call's enumerator argument, when it has DIGCF_DEVICEINTERFACE
flag present, with actual enumerators being out of option in the case.
Could you please explain me, what is actually going on here? I'm not familiar with SetupAPI semantics so I can't tell what limitations would getting device info set for device setup classes have imposed, in contrast with that for device interface classes. Also, could there be some version-specific behavior in here? The code above was tested on Windows 7 x64 system, but itself was being compiled into a 32-bit executable. I need to know specifically if any particular solution proposed for this would work on good ol' Windows XP as well.
P.S. Not a duplicate of Getting device path for CreateFile while enumerating devices with SetupDiEnumDeviceInfo, despite similar underlying purpose. ;)