Click here to Skip to main content
6,822,123 members and growing! (19,071 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate

Simple Device Manager

By Chuan-Liang Teng

This article demonstrates a simple enumeration device and a dynamic, driver load/unload facility.
Windows, Visual-Studio, Dev
Posted:15 Jun 2006
Views:41,436
Bookmarked:41 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 3.44 Rating: 2.86 out of 5
9 votes, 56.3%
1

2
2 votes, 12.5%
3
2 votes, 12.5%
4
3 votes, 18.8%
5

Introduction

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:

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.

SetupDiGetDeviceRegistryProperty(hDevInfo,
                                 &spDevInfoData,
  // Supplies one of the following values,

  // indicating the property to be retrieved.

                                 SPDRP_CLASS, 
                                 0L,
                                 (PBYTE)szBuf,
                                 2048,
                                 0);

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.

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.

License

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


Member

Occupation: Web Developer
Location: Taiwan Taiwan

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralFails to work on 64 bit machines Pinmemberdivian4417:05 20 Jan '09  
GeneralRe: Fails to work on 64 bit machines Pinmembermihi643:59 18 Nov '09  
GeneralHello, I have a question. PinmemberMember 410338915:28 28 Oct '08  
GeneralDebug errorr Pinmemberpkjlitu20:33 7 Aug '08  
AnswerRe: Debug errorr Pinmemberworldsky4:43 5 Sep '08  
QuestionIs there way to check perfection State of Devices By this Demo / Code ? PinmemberDevEx21:15 12 Nov '07  
QuestionError "Device Path" Infomation PinmemberYesy21:29 15 Oct '07  
QuestionRun-Time Check Failure #2 PinmemberFaresDZ5:05 15 Aug '07  
AnswerRe: Run-Time Check Failure #2 PinmemberGiulliano Bueno7:18 28 Aug '07  
AnswerRun-Time Check Failure #2 PinmemberSARI_SWEETH21:52 23 Oct '07  
GeneralRe: Run-Time Check Failure #2 PinmemberMember 45918244:25 1 Apr '08  
GeneralRe: Run-Time Check Failure #2 Pinmemberomkarsomani0:08 2 Jul '08  
QuestionC# Pinmemberikrigel22:02 18 May '07  
GeneralThe application does not compile PinmemberLeonardo Heis2:54 11 May '07  
GeneralRe: The application does not compile Pinmemberjkasdhklfadshfsdjka21:36 7 Jun '07  
QuestionMissing files newdev.h and newdev.lib PinmemberPhanLam18:09 6 Mar '07  
AnswerRe: Missing files newdev.h and newdev.lib PinmemberMike Angel Martin7:26 14 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 15 Jun 2006
Editor: Smitha Vijayan
Copyright 2006 by Chuan-Liang Teng
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project