This article demonstrates a simple enumeration device and a dynamic, driver load/unload facility. I tried to prepare four very simple functionality for everyone's use.
First of all, let us look at the image below which shows a window with device manager information. It is easy, the device manager enumerates devices when we expand the tree for a particular device.
The idea of device enumeration is not difficult for understanding:
Get the device information for a device using the
SetupDiGetClassDevs function. When first calling the function, the first and the second parameters should be set to “0”, and the last parameter should be set to the
DIGCF_ALLCLASSES constant to get all devices.
hDevInfo = SetupDiGetClassDevs(0L, 0L, _hDlg, DIGCF_PRESENT |
DIGCF_ALLCLASSES | DIGCF_PROFILE);
Getting the information
SetupDiEnumDeviceInfo function to enumerate all devices:
SetupDiEnumDeviceInfo(hDevInfo, wIndex, &spDevInfoData));
The second parameter is supplied a zero-based index of the device to be retrieved. Get the device name from the Registry via the
// Supplies one of the following values,
// indicating the property to be retrieved.
As you can see in the picture below, a device class has a name and a GUID (so it can be found in the Registry). The class can also have a description. For example, for class "
Ports" has the description: "Ports (COM & LPT)". A class can also have devices that are present in the configuration.
- Get information about the current configuration using the
- Get the resource descriptor from the current configuration using the
CM_Get_Next_Res_Des function. Do this and follow the same steps for every resource till they exist.
- Get information about the size of a resource data using the
- Get resource data using the
Load/unload non-PNP driver
Windows allows loading drivers at runtime using the Service Control Manager. Yes, the Service Control Manager in Windows can not only be used to load and manage services, but you can use it with device drivers to load/unload/start/stop Windows services.
If you want to send/receive data to the device driver loaded, 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 that Windows is so abstract in this aspect.
Load/unload class driver
Windows allows loading class drivers at runtime using the
SetupDiXXXX API. You can use it with Windows device drivers as you use it to load/unload your device driver.