Click here to Skip to main content
14,775,752 members
Articles » Languages » C / C++ Language » General
Posted 15 Jun 2006


71 bookmarked

Simple Device Manager

Rate me:
Please Sign up or sign in to vote.
3.15/5 (22 votes)
15 Jun 2006
This article demonstrates a simple enumeration device and a dynamic, driver load/unload facility.


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.

Device enumeration

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:

Image 1

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

Use the 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 SetupDiGetDeviceRegistryPropertyA function.

  // Supplies one of the following values,
  // indicating the property to be retrieved.

Device resource

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 CM_Get_First_Log_Conf function.
  • 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 CM_Get_Res_Des_Data_Size function.
  • Get resource data using the CM_Get_Res_Des_Data function.

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.

Image 2

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.


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


About the Author

Chuan-Liang Teng
Software Developer (Senior) Shan Hon Co., Ltd.
Taiwan Taiwan
No Biography provided

Comments and Discussions

QuestionVB.Net Code Pin
NekoNao8-Nov-15 16:12
MemberNekoNao8-Nov-15 16:12 
QuestionAccess is denied error Pin
Member 97701613-Nov-13 2:21
MemberMember 97701613-Nov-13 2:21 
QuestionI can't get device's info which has not driver Pin
tigerset6-Jan-11 17:21
Membertigerset6-Jan-11 17:21 
QuestionHow to use newdev.h and newdev.lib after installing Driver Development Kit Pin
adhoninside2-Dec-10 2:26
Memberadhoninside2-Dec-10 2:26 
GeneralMy vote of 2 Pin
ReorX22-Nov-10 2:51
MemberReorX22-Nov-10 2:51 
QuestionY para saber los de mi maquina desde una aplicacion ejecutada en servidor? Pin
luis__19827-Apr-10 6:29
Memberluis__19827-Apr-10 6:29 
GeneralFails to work on 64 bit machines Pin
divian4420-Jan-09 17:05
Memberdivian4420-Jan-09 17:05 
GeneralRe: Fails to work on 64 bit machines Pin
mihi6418-Nov-09 3:59
Membermihi6418-Nov-09 3:59 
GeneralRe: Fails to work on 64 bit machines Pin
The-Great-Kazoo13-Apr-11 10:49
professionalThe-Great-Kazoo13-Apr-11 10:49 
GeneralHello, I have a question. Pin
Jeong_Y28-Oct-08 15:28
MemberJeong_Y28-Oct-08 15:28 
GeneralDebug errorr Pin
pkjlitu7-Aug-08 20:33
Memberpkjlitu7-Aug-08 20:33 
AnswerRe: Debug errorr Pin
worldsky5-Sep-08 4:43
Memberworldsky5-Sep-08 4:43 
QuestionIs there way to check perfection State of Devices By this Demo / Code ? Pin
DevEx212-Nov-07 1:15
MemberDevEx212-Nov-07 1:15 
QuestionError "Device Path" Infomation Pin
Yesy15-Oct-07 21:29
MemberYesy15-Oct-07 21:29 
QuestionRun-Time Check Failure #2 Pin
FaresDZ15-Aug-07 5:05
MemberFaresDZ15-Aug-07 5:05 
AnswerRe: Run-Time Check Failure #2 Pin
Giulliano Bueno28-Aug-07 7:18
MemberGiulliano Bueno28-Aug-07 7:18 
AnswerRun-Time Check Failure #2 Pin
SARI_SWEETH23-Oct-07 21:52
MemberSARI_SWEETH23-Oct-07 21:52 
GeneralRe: Run-Time Check Failure #2 Pin
Member 45918241-Apr-08 4:25
MemberMember 45918241-Apr-08 4:25 
GeneralRe: Run-Time Check Failure #2 Pin
Omkar Somani2-Jul-08 0:08
MemberOmkar Somani2-Jul-08 0:08 
GeneralRe: Run-Time Check Failure #2 Pin
buddly29-Dec-18 6:58
Memberbuddly29-Dec-18 6:58 
QuestionC# Pin
ikrigel18-May-07 22:02
Memberikrigel18-May-07 22:02 
GeneralThe application does not compile Pin
Leonardo Heis11-May-07 2:54
MemberLeonardo Heis11-May-07 2:54 
GeneralRe: The application does not compile Pin
jkasdhklfadshfsdjka7-Jun-07 21:36
Memberjkasdhklfadshfsdjka7-Jun-07 21:36 
QuestionMissing files newdev.h and newdev.lib Pin
PhanLam6-Mar-07 18:09
MemberPhanLam6-Mar-07 18:09 
AnswerRe: Missing files newdev.h and newdev.lib Pin
Mike Angel Martin14-Apr-07 7:26
MemberMike Angel Martin14-Apr-07 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.