Enumerating the device using the SetupDi* API provided with WinXP.
Windows has a rich collection of APIs to get useful information about installed devices. In this article, I will show how you can enumerate devices on a machine using SetupDiXXX API and CM_XXXX API.
The program has a simple GUI: only a a treeview control that shows all of the installed devices. All the information about a device (such as name and its icon) is grabbed from Windows SetupDiXXX API.
The Setup application programming interface (API) provides a set of functions that your setup application can call to perform installation operations or get several information about installed devices, their class and also their GUID (a unique identifier for every device).
The SetupDiGetClassImageList function builds an image list that contains bitmaps for every installed class and returns the list in a data structure.
The SetupDiDestroyClassImageList function destroys a class image list that was built by a call to SetupDiGetClassImageList.
The SetupDiGetClassImageIndex function retrieves the index within the class image list of a specified class.
Get device info set for device class (SetupDiGetClassDevs function), when first call , the first and second parameters for this function should be set to “0”, the last parameter should be set property DIGCF_ALLCLASSES constant to get all class device.
Using (SetupDiEnumDeviceInfo function) to enumeration all class devices.
The function returns TRUE if it is successful. Otherwise, it returns FALSE and the logged error can be retrieved with a call to GetLastError.
The second parameter is Supplies the zero-based index of the device information element to retrieve.
The Last parameter is Supplies a pointer to an SP_DEVINFO_DATA structure to receive information about this element. The caller must set cbSize to sizeof(SP_DEVINFO_DATA).
Get device name from Registry via SetupDiGetDeviceRegistryPropertyA function
All devices in the system join in the device classes. As you can see in the below picture, the class has name and GUID (so it can be found in Registry). The class can also have a description. For example, for class "Ports" the description is "Ports (COM & LPT)". Class also has devices that are present in the configuration.
Get information about current configuration (
Get resource descriptor from current configuration (
CM_Get_Next_Res_Des function, do this and follow steps for every resource till they exist)
Get information about size of resource data (
Get resource data (
Windows allows loading drivers at runtime using the Service Control Manager . Yes, the Service Control Manager in Windows not only can be used to load and manage services. You can use it with device drivers as you use it to load/unload/start/stop windows services.
The OpenSCManager function establishes a connection to the service control manager on the specified computer and opens the specified service control manager database.
The CreateService function creates a service object and installs it in the service control manager database by creating a key with the same name as the service under the following registry key:
The OpenService function opens an existing service.
If you want to send/receive data to the device driver loaded, to allow this, you have to get a handle to the driver. For this purpose you can use the function CreateFile, and pass the driver name as the file name. You can see Windows is so abstract in this aspect.
Windows also allows loading drivers at runtime using the SetupDiXXX API. The first we need to call SetuiDiGetINFClass to get the class of a specified device INF file.
The second parameter, Receives the class GUID for the specified INF file. If the INF file does not specify a class name, this variable is set to GUID_NULL. Call SetupDiClassGuidsFromName to determine if one or more classes with this name are already installed.
When function return TRUE, NOW we need create an empty device information set and optionally associates the set with a device setup class and a top-level window. Use SetupDiCreateDeviceInfo function creates a new device information element and adds it as a new member to the specified device information set.
The caller of this function must delete the returned device information set when it is no longer needed by calling SetupDiDestroyDeviceInfoList.
To create a device information list for a remote machine use SetupDiCreateDeviceInfoListEx.
The sixth parameter. Controls how the device information element is created. Can be a combination of the following values:
If this flag is specified, DeviceName contains only a Root-enumerated device ID and the system creates a unique device instance key for it. This unique device instance key is generated as:
where InstanceID is a four-digit, base-10 number that is unique among all subkeys under
Enum\Root\DeviceName . Call SetupDiGetDeviceInstanceId to find out what ID was generated for this device information element.
If this flag is specified, the resulting device information element inherits the class driver list, if any, associated with the device information set. In addition, if there is a selected driver for the device information set, that same driver is selected for the new device information element.
If this device instance is being added to a set that has an associated class, the device class must be the same or the call fails. In this case, a call to GetLastError returns
If the specified device instance is the same as an existing device instance key in the registry, the call fails. In this case, a call to GetLastError returns
ERROR_DEVINST_ALREADY_EXISTS . This occurs only if the
DICD_GENERATE_ID flag is not set.
If the new device information element was successfully created but the caller-supplied DeviceInfoData buffer is invalid, the function returns FALSE. In this case, a call to GetLastError returns
ERROR_INVALID_USER_BUFFER . However, the device information element will have been added as a new member of the set already.
That's it! If you have any suggestions or problems to report, post them here. There are plenty of ways this idea could be extended if you are of an inclination to do so.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here.