Platform SDK: Hardware

Enumerating Current Devices

Applications can use the SetupDiGetClassDevsEx function to retrieve the list of current devices. The following example function demonstrates how to retrieve this list.

HDEVINFO DoDeviceEnum( GUID InterfaceClassGuid 
)
/*
Routine Description:
    Retrieves the device information set that contains that contains
    the devices of the specified class. 

Parameters:
    InterfaceClassGuid - The interface class GUID. 

Return Value:
    If the function succeeds, the return value is a handle to the 
    device information set.

    If the function fails, the return value is zero.
*/
{
    HDEVINFO DeviceInfoSet;
    HDEVINFO NewDeviceInfoSet;

    // Create a device information set that will be the container for 
    // the device interfaces.

    DeviceInfoSet = SetupDiCreateDeviceInfoList(NULL, NULL);
    
    if(DeviceInfoSet == INVALID_HANDLE_VALUE) {
        Err = GetLastError();
        printf( "SetupDiCreateDeviceInfoList failed: %lx.\n", Err );
        return 0;
    }

    // Retrieve the device information set for the interface class.

    NewDeviceInfoSet = SetupDiGetClassDevsEx( InterfaceClassGuid,
        NULL,
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE,
        DeviceInfoSet,
        NULL,
        NULL
    );

    if(NewDeviceInfoSet == INVALID_HANDLE_VALUE) 
    {
        Err = GetLastError();
        printf( "SetupDiGetClassDevsEx failed: %lx.\n", Err );
        return 0;
    }

    return NewDeviceInfoSet;
}

After you have registered to receive device interface notification messages, you can call this function to obtain a device information set that contains the current list of device interfaces. When you receive a DBT_DEVICEARRIVAL event, you can use the SetupDiOpenDeviceInterface function to add the device interface to the device information set. When you receive a DBT_DEVICEREMOVECOMPLETE event, you can use the SetupDiDeleteDeviceInterfaceData function to delete the device interface from the device information set.