Working with Devices: Enumerating Device Setup Classes

So as promised lets enumerate through the Device Setup Classes. Sometimes I may say Device Class. This may be ambiguous with Device Setup Class and Device Interface Class. Most times I mean Device Setup Class, but I will try to correct myself as I go.

So the easiest way to see the device setup classes on your computer is simply to view it from the Registry. Under HKLM\SYSTEM\CurrentControlSet\Control\Class you'll find several Subkeys. Each subkey represents a setup class by GUID. You can look at each one and see the name for the setup class.

For example look at the GUID {4D36E972-E325-11CE-BFC1-08002bE10318}. You should see that this correlates to the net class. 'net' translates into the friendly name 'Network adapters'. By expanding this key you'll see more subkeys. Each subkey represents a device that falls under the net class.

You may be asking yourself "why do I have so many listed when only a handful show up in device manager?" This is because a key is created at any time a device is added to the computer. Even if it is removed the key will remain. Also there is typically more than one device installed when a physical device is installed. Another reason is that programs can install non-physical devices like VPN Adapters. Finally many devices are simply hidden in the device manager.

So we can get at this through the function CM_Enumerate_Classes

Here is the declare statement that I used.

Private Declare Auto Function CM_Enumerate_Classes Lib "cfgmgr32.dll" (ByVal ClassIndex As Integer, ByRef ClassGuid As Guid, ByVal Reserved As Integer) As Integer

The first argument is an index. We'll start at 0 and then keep incrementing. The next argument is the Guid that is outputted. This is the Guid that is at that specific index. The last argument is reserved and a 0 should be passed.

Here is some code that will set an index at 0 and keep incrementing it until the result is not 0. Technically, I believe an error CR_NO_SUCH_VALUE will be returned. The Guid will be added to an array list and then that list is sorted and returned.

'Returns an array of all the Class Guids on the System
Private Function GetClassGuids() As ArrayList
Dim i As Integer = 0
Dim Result As Integer
Dim ClassGuid As Guid
Dim GuidList As New ArrayList
Do
Result = CM_Enumerate_Classes(i, ClassGuid, 0)
i += 1
If Result = 0 Then GuidList.Add(ClassGuid)
Loop Until Result <> 0
GuidList.Sort()
Return GuidList
End Function


So thats it.

Oh, this code was written in VS.NET 2003 for .NET v1.1

No comments: