Getting Hardware Information Using Visual Basic
Getting Hardware Information Using Visual Basic
This article explains how to retrieve hardware information using both Visual Basic.NET and
VBScript. We shall cover motherboards, on board devices and processors.
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")
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
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 Other
0x1
2 Unknown
0x2
3 Running/Full Power
0x3
4 Warning
0x4
5 In Test
0x5
6 Not Applicable
0x6
7 Power Off
0x7
8 Off Line
0x8
9 Off Duty
0x9
10 Degraded
0xA
11 Not Installed
0xB
12 Install Error
0xC
Value Meaning
0 Unknown
1 CPU Enabled
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).
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