Enumeration of interfaces in a device information set obtained for a particular PnP counter

I am trying to get device interfaces data through SetupDiEnumDeviceInterfaces()

for all devices that match a specific PnP counter. Consider the following example (only for interface with index 0):

#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 startup, it SetupDiEnumDeviceInterfaces()

exits with an error GetLastError()

returning error 259 ( ERROR_NO_MORE_ITEMS

), as if there were no interfaces for the device information it started with. It looks like the behavior of all the sets of device information obtained for the device setup classes rather than the device interface classes.

However, when I add a flag DIGCF_DEVICEINTERFACE

to the last argument of the call SetupDiGetClassDevsA()

so that the set is obtained for the interface classes, the latter fails with error 13 ( ERROR_INVALID_DATA

). The same error usually occurs in the case of device installation classes if the listed PnP enumerator is not registered in the system. Indeed, when I replace the enumerator argument with NULL

, the interface succeeds. According to MSDN , specifying the flag DIGCF_DEVICEINTERFACE

allows the device instance ID to be used instead of the enumerator argument, but it occurs to me that the above device instance IDs NULL

are the only parameters for the call enumerator argument SetupDiGetClassDevs()

when it has a flagDIGCF_DEVICEINTERFACE

however, actual counters have no option in this case.

Could you please explain to me what is really going on here? I'm not familiar with the semantics of SetupAPI, so I can't tell you what constraints device information would impose on device setup classes as opposed to device interface classes. Also, could there be some version related behavior? The above code was tested on a Windows 7 x64 system, but itself was compiled into a 32-bit executable. I need to know specifically if any specific solution suggested for this will work on Windows XP as well.

PS Not a duplicate Getting device path for CreateFile while enumerating devices using SetupDiEnumDeviceInfo , despite a similar primary purpose.;)

+3


source to share





All Articles