Getting Hardware Information Using Visual Basic.NET and VBScript

This article explains how to retrieve hardware information using both Visual Basic.NET and VBScript. We shall cover motherboards, on board devices and processors.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 22
January 25, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this story is available here.

The sample downloadable solution (zip) was entirely developed using Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003 Standard Edition. But, I am confident that it would work with other versions of Windows (which support .NET 1.1) versions as well.

I contributed several articles covering WMI with VB.NET and VBScript, including the articles on introductory or basic topics concerning WMI. I even contributed a series of about six articles on “WMI Programming on VB.NET” covering several aspects of WMI. I strongly suggest you go through the series, before going through this article.

Listing the “On Board Devices” available

Some scenarios may require some properties of the onboard devices of the motherboard to be listed. Instead of opening the CPU and going through each and every device, we can retrieve the same information dynamically using VB.NET. The following VB.NET code should support retrieving some minimum information about devices available on your system.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_OnBoardDevice")
 
            Dim dt As DataTable = globals.getOnBoardDeviceStructure
            For Each queryObj As ManagementObject In searcher.Get()
                globals.addOnBoardDevice(dt, queryObj("Description"), Convert.ToString(queryObj("DeviceType")), queryObj("Enabled"), queryObj("Tag"))
            Next
            Me.DataGrid1.DataSource = dt
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try
    End Sub

You can achieve the same thing with VBScript as follows:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_OnBoardDevice",,48)
For Each objItem in colItems
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "DeviceType: " & objItem.DeviceType
    Wscript.Echo "Enabled: " & objItem.Enabled
    Wscript.Echo "Tag: " & objItem.Tag
Next

The class Win32 OnBoardDevice in detail

In the previous section, I touched on only some of the most important properties from the existing WMI class (Win32_OnBoardDevice); had I done otherwise, it would have made the program too long. The program above will list the description, device type and tag properties. You can further extend the program with several other properties available in the “Win32_OnBoardDevice” class. You can even refer to MSDN online for further properties.

Now, let us go into the details of the properties I mentioned in the previous section.

Description: This property gives you a better explanation of the object.

Device Type: In general, there will be many types of devices “OnBoard.” The devices include Video, SCSI controller, Ethernet (especially for LAN), and so on. The “Device Type” property explains the type of device being represented. The following is the table extracted from MSDN to provide you with an explanation of those values returned by “Device Type.”

Value

Meaning

1

Other

2

Unknown

3

Video

4

SCSI Controller

5

Ethernet

6

Token Ring

7

Sound

  

Enabled: This property specifies whether the device is enabled to work or not. Sometimes, it also depends on hardware settings or configuration.

Tag: This property gives you the unique identifier of the onboard device available for your system.

Listing the processors available

In previous sections, we have seen only “On Board Devices.” Now, let us go through the information related to the processors existing in your system. The following VB.NET code should support retrieving some minimum information about processors available on your system.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_Processor")
            Dim dt As DataTable = globals.getProcessorStructure
            For Each queryObj As ManagementObject In searcher.Get()
                globals.addProcessorDevice(dt, Convert.ToString(queryObj("Availability")), queryObj("Caption"), Convert.ToString(queryObj("CpuStatus")), Convert.ToString(queryObj("CurrentClockSpeed")), queryObj("DeviceID"), Convert.ToString(queryObj("Level")), queryObj("Name"), queryObj("ProcessorId"), Convert.ToString(queryObj("ProcessorType")), queryObj("SystemName"))
            Next
            Me.DataGrid1.DataSource = dt
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try
    End Sub

You can achieve the same thing with VBScript as follows:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Processor",,48)
For Each objItem in colItems
    Wscript.Echo "Availability: " & objItem.Availability
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "CpuStatus: " & objItem.CpuStatus
    Wscript.Echo "CurrentClockSpeed: " & objItem.CurrentClockSpeed
    Wscript.Echo "DeviceID: " & objItem.DeviceID
    Wscript.Echo "Level: " & objItem.Level
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "ProcessorId: " & objItem.ProcessorId
    Wscript.Echo "ProcessorType: " & objItem.ProcessorType
    Wscript.Echo "SystemName: " & objItem.SystemName
Next

The class Win32_Processor in detail

In the previous section, plenty of properties are available to retrieve information about processors. But I included only the most important properties from the existing WMI class (Win32_Processor); had I done otherwise, it would have made the program too long.

The above program would list the information related to the properties of availability, Caption, CPUStatus, CurrentClockSpeed, DeviceID, Level, Name, ProcessorID, ProcessorType and SystemName. By using these properties, you can already retrieve plenty of information about the processors. You can further extend the above program with several other properties available in the “Win32_Processor” class. You can even refer to MSDN online for further properties.

Let us go through these properties to understand them better.

Availability: This property gives you the status of the processor's “availability.” The value, along with the description (I've included the most important ones) could be something like the following:

Value

Meaning

1

0x1

Other

2

0x2

Unknown

3

0x3

Running/Full Power

4

0x4

Warning

5

0x5

In Test

6

0x6

Not Applicable

7

0x7

Power Off

8

0x8

Off Line

9

0x9

Off Duty

10

0xA

Degraded

11

0xB

Not Installed

12

0xC

Install Error

Caption: This property gives a short description of an object.

CPUStatus: This property gives you the current status of the processor. The status changes indicate processor usage, not the physical condition of the processor. The following table shows you the status of the processor:

Value

Meaning

0

Unknown

1

CPU Enabled

2

CPU Disabled by User via BIOS Setup

3

CPU Disabled by User via BIOS (POST Error)

4

CPU is Idle

5

Reserved

6

Reserved

7

Other

CurrentClockSpeed: This property shows you the current speed of the processor in MHz.

DeviceID: This property gives you the unique identifier of a processor on the system.

Level: This property gives you a definition of the processor type. The value depends on the architecture of the processor.

Name: This property explains the label by which the object is known. When this property is a subclass, it can be overridden to be a key property.

ProcessorID: This property provides us processor information that describes the processor's features.

ProcessorType: This property gives you the type of processor available in your system.

Value

Meaning

1

Other

2

Unknown

3

Central Processor

4

Math Processor

5

DSP Processor

6

Video Processor

System Name: This property gives you the name of your system (or the one to which you're connected).

Digging further into the motherboard's details

What about the motherboard? After all, it's the heart of the system! Let us dig up even that information. The following VB.NET code should support retrieving some minimum information on your server.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Dim searcher As New ManagementObjectSearcher( _
                "root\CIMV2", _
                "SELECT * FROM Win32_BaseBoard")
            Dim dt As DataTable = globals.getBaseBoardStructure
            For Each queryObj As ManagementObject In searcher.Get()
                globals.addBaseBoard(dt, queryObj("HostingBoard"), queryObj("Manufacturer"), queryObj("PoweredOn"), queryObj("Product"), queryObj("SerialNumber"), queryObj("Version"))
            Next
            Me.DataGrid1.DataSource = dt
 
        Catch err As ManagementException
            MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
        End Try
    End Sub

You can achieve the same thing with VBScript as follows:

 strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_BaseBoard",,48)
For Each objItem in colItems
    Wscript.Echo "HostingBoard: " & objItem.HostingBoard
    Wscript.Echo "Manufacturer: " & objItem.Manufacturer
    Wscript.Echo "PoweredOn: " & objItem.PoweredOn
    Wscript.Echo "Product: " & objItem.Product
    Wscript.Echo "SerialNumber: " & objItem.SerialNumber
    Wscript.Echo "Version: " & objItem.Version
Next

As the properties are quite understandable (just from the words), I am not going to explain much. But for further information (or all properties of the class “Win32_BaseBoard”) you can refer to MSDN.

The wrappers for Win32_Processor, Win32_OnBoardDevice and Win32_BaseBoard

In all of the above VB.NET programs, you must have observed some statements like the following:

globals.getOnBoardDeviceStructure
globals.addOnBoardDevice
globals.getProcessorStructure
globals.addProcessorDevice
etc

Those are actually the wrapper in-memory tables; I designed them to work flexibly with those classes (“Win32_Processor”, “Win32_OnBoardDevice” and “Win32_BaseBoard”). The wrappers are defined below.

The following would be the wrapper for “Win32_Processor”:

Public Function getProcessorStructure() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add (New DataColumn("Availability"))
        dt.Columns.Add (New DataColumn("Caption"))
        dt.Columns.Add (New DataColumn("CpuStatus"))
        dt.Columns.Add (New DataColumn("CurrentClockSpeed"))
        dt.Columns.Add (New DataColumn("DeviceID"))
        dt.Columns.Add (New DataColumn("Level"))
        dt.Columns.Add (New DataColumn("Name"))
        dt.Columns.Add (New DataColumn("ProcessorId"))
        dt.Columns.Add (New DataColumn("ProcessorType"))
        dt.Columns.Add (New DataColumn("SystemName"))
        Return dt
    End Function

The following would be the wrapper for “Win32_OnBoardDevice”:

Public Function getOnBoardDeviceStructure() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add (New DataColumn("Description"))
        dt.Columns.Add (New DataColumn("DeviceType"))
        dt.Columns.Add (New DataColumn("Enabled"))
        dt.Columns.Add (New DataColumn("Tag"))
        Return dt
    End Function

The following would be the wrapper for “Win32_BaseBoard”:

Public Function getBaseBoardStructure() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("HostingBoard"))
        dt.Columns.Add(New DataColumn("Manufacturer"))
        dt.Columns.Add(New DataColumn("PoweredOn"))
        dt.Columns.Add(New DataColumn("Product"))
        dt.Columns.Add(New DataColumn("SerialNumber"))
        dt.Columns.Add(New DataColumn("Version"))
        Return dt
    End Function

Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials