Enumerate installed devices
You should not enumerate devices by using registry keys directly. Registry keys do not contain the required information to enumerate installed devices on the system. This information, such as whether the device is actually present or is a phantom device (one that is not plugged in), is held by the Plug and Play (PnP) manager. The PnP manager also performs additional filtering of registry information.
To enumerate installed devices safely, follow these steps.
Using configuration manager functions:
Use CM_Get_Device_ID_List to retrieve a list of unique device instance identifier (ID) strings. To retrieve information only for devices that are present in the system, set CM_GETIDLIST_FILTER_PRESENT in the ulFlags parameter.
You can use the unique device instance ID with CM_Locate_DevNode to retrieve a DEVINST that represents the device to use with other configuration manager APIs.
Using SetupAPI functions:
Use SetupDiGetClassDevs to retrieve information for a set of devices. To retrieve information only for devices that are present in the system, set DIGCF_PRESENT in the Flags parameter.
Use SetupDiEnumDeviceInfo to enumerate the devices in the set.
You can use the SP_DEVINFO_DATA returned by SetupDiEnumDeviceInfo with other SetupApi APIs or use SetupDiGetDeviceInstanceId to retrieve a unique device instance identifier (ID) for the device.