Click here to Skip to main content
Click here to Skip to main content

How to Get a List of Non-Working Devices Using VB.NET

, 10 Jan 2010
Rate this:
Please Sign up or sign in to vote.
Get a list of non-working devices using VB.NET.

Introduction

When it comes to hardware interaction, many .NET developers feel frustrated and uncertain about the capability of doing what they have in mind. One common problem we face is enumerating the devices installed in a computer. A harder problem is to get the devices that are not working properly or disabled! If you are writing an application that requires the existence of a specific hardware device, it makes common sense to test whether the desired device is working and enabled.

The project with its latest source code can be downloaded from the original article here: How To Get Non Working Devices Using VB.NET.

In Windows, you can view the list of devices from the Device Manager. Disabled devices appear with a down arrow next to the device, and devices that are not working properly appear with a yellow exclamation mark icon next to it:

The solution to such problems in .NET Framework languages becomes simple if we make use of WMI Queries or Windows Management Instrumentation. Writing a WMI query to retrieve system information is as simple as writing a SQL query. In fact, it's almost exactly the same syntax except the objects we are querying do differ. Take a look at the WMI query below that retrieves all installed devices on the local system:

Select * from Win32_PnPEntity

As simple as that; Win32_PnpEntity stores information about installed devices. So, if you want to filter out the results by getting only the non working devices, we simply add a Where clause just like we do if we are filtering data from a database table.

Select * from Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0

The ConfigManagerErrorCode property stores the state of the device, a value of 0 means Working, and any other value means the device is not working or disabled.

Let's create a Device class to convert each retrieved device to an object that we can deal with in our application. After all, we would want to list out all non working devices and bind the list to some bindable data object such as a DataGrid or a ListView.

Below is our base Device class having all the properties we need to show:

Public Class Device

    Private mName As String
    Private mManufacturer As String
    Private mDescription As String
    Private mService As String
    Private mDeviceID As String
    Private mPNPDeviceID As String
    Private mClassGUID As String

    Public Property Name() As String
        Get
            Return mName
        End Get
        Set(ByVal value As String)
            mName = value
        End Set
    End Property

    Public Property Manufacturer() As String
        Get
            Return mManufacturer
        End Get
        Set(ByVal value As String)
            mManufacturer = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return mDescription
        End Get
        Set(ByVal value As String)
            mDescription = value
        End Set
    End Property

    Public Property Service() As String
        Get
            Return mService
        End Get
        Set(ByVal value As String)
            mService = value
        End Set
    End Property

    Public Property DeviceID() As String
        Get
            Return mDeviceID
        End Get
        Set(ByVal value As String)
            mDeviceID = value
        End Set
    End Property

    Public Property PNPDeviceID() As String
        Get
            Return mPNPDeviceID
        End Get
        Set(ByVal value As String)
            mPNPDeviceID = value
        End Set
    End Property

    Public Property ClassGUID() As String
        Get
            Return mClassGUID
        End Get
        Set(ByVal value As String)
            mClassGUID = value
        End Set
    End Property
End Class

We are now ready to implement our two main functions: GetAllDevices and GetNonWorkingDevices and add them to the Device class. Both methods will be Shared methods because they are generic and produce the same result across all instances of the Device class. Below is the implementation of both methods; note the use of the GetObject method which retrieves an instance of WMI on the local computer system:

Public Shared Function GetAllDevices() As List(Of Device)
    Dim pc As String = "." 'local
    Dim wmi As Object = GetObject("winmgmts:\\" & pc & "\root\cimv2")
    Dim allDevices As New List(Of Device)
    Dim devices As Object = wmi.ExecQuery("Select * from Win32_PnPEntity")
    Dim device As Device
    For Each d As Object In devices
        device = New Device
        With Device
        .mClassGUID = IIf(IsDBNull(d.ClassGuid), 0, d.ClassGuid)
        .mDescription = IIf(IsDBNull(d.Description), 0, d.Description)
        .DeviceID = IIf(IsDBNull(d.DeviceID), 0, d.DeviceID)
        .Manufacturer = IIf(IsDBNull(d.Manufacturer), 0, d.Manufacturer)
        .Name = IIf(IsDBNull(d.Name), 0, d.Name)
        .PNPDeviceID = IIf(IsDBNull(d.PNPDeviceID), 0, d.PNPDeviceID)
        .Service = IIf(IsDBNull(d.Service), 0, d.Service)        
        End With
        allDevices.Add(device)
    Next
    Return allDevices
End Function

Public Shared Function GetNonWorkingDevices() As List(Of Device)
    Dim pc As String = "." 'local
    Dim wmi As Object = GetObject("winmgmts:\\" & pc & "\root\cimv2")
    Dim notWorking As New List(Of Device)
    Dim devices As Object = wmi.ExecQuery("Select * from " & _
          "Win32_PnPEntity WHERE ConfigManagerErrorCode <> 0")
    Dim device As Device
    For Each d As Object In devices
        device = New Device
        With Device
        .mClassGUID = IIf(IsDBNull(d.ClassGuid), 0, d.ClassGuid)
        .mDescription = IIf(IsDBNull(d.Description), 0, d.Description)
        .DeviceID = IIf(IsDBNull(d.DeviceID), 0, d.DeviceID)
        .Manufacturer = IIf(IsDBNull(d.Manufacturer), 0, d.Manufacturer)
        .Name = IIf(IsDBNull(d.Name), 0, d.Name)
        .PNPDeviceID = IIf(IsDBNull(d.PNPDeviceID), 0, d.PNPDeviceID)
        .Service = IIf(IsDBNull(d.Service), 0, d.Service)
        End With
        notWorking.Add(device)
    Next
    Return notWorking
End Function

Our Device class is now ready; we can simply bind the results of each method to a DataGrid, as I have done in the figure below:

Here is the code that produces the above results after adding a DataGridView to a form:

Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
    DataGridView1.DataSource = Device.GetNonWorkingDevices
End Sub

The project with its latest source code can be downloaded from the original article here: How To Get Non Working Devices Using VB.NET.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ali Tarhini
Software Developer (Senior) Microgen
Lebanon Lebanon
For more articles and extreme topics please check out my personal website www.alitarhini.com

Comments and Discussions

 
Suggestionget disabled devices by ConfigManagerCode Pinmembercodedieb121-Jun-14 11:41 
GeneralMy vote of 5 Pinmembermanoj kumar choubey24-Apr-12 23:47 
QuestionGreat Pinmembertehnookster3-Jan-12 11:04 
Questionhow to get a lis of non-working devices using wmi in C# Pinmemberanaidy0u16-Aug-11 17:44 
GeneralNice work! I can see how this could be quite useful. I commented out one line to get it to work. PinmemberThe-Great-Kazoo14-Apr-11 5:26 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 10 Jan 2010
Article Copyright 2010 by Ali Tarhini
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid