You are on page 1of 9

Getting Hardware Information using Visual

Basic.NET and VBScript continued


(Page 1 of 5 )

This article explains how to retrieve hardware information using both Visual Basic.NET and
VBScript. In my previous article, I covered motherboards, onboard devices and processors. In
this article, I concentrate on BIOS, BUS, motherboard, memory and audio related information.

A downloadable file for this article 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) as well.
I contributed several articles on WMI with VB.NET and VBScript (including the articles on
introductory or basic topics of 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 “BIOS” information
Some scenarios may require some properties of BIOS to be listed. We can retrieve this
information dynamically using VB.NET. Before trying to retrieve the hardware information, let
us create the wrapper with BIOS structure:
Public Sub addRow(ByRef dt As DataTable, ByVal p As String, ByVal
v As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Property") = p
dr("Value") = v
dt.Rows.Add(dr)
End Sub
The above method “addRow” adds a single row based on the structure you create for the data
table using the following method.
Public Function getStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Property"))
dt.Columns.Add(New DataColumn("Value"))
Return dt
End Function
Once you complete the creation of wrapper, the following VB.NET code should support retrieval
of some minimum information about the BIOS 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_BIOS")
Dim dt As DataTable = globals.getStructure
For Each queryObj As ManagementObject In searcher.Get
()
If queryObj("BIOSVersion") Is Nothing Then
globals.addRow(dt,"BIOSVersion", queryObj
("BIOSVersion"))
Else
Dim arrBIOSVersion As String()
arrBIOSVersion = queryObj("BIOSVersion")
For Each arrValue As String In arrBIOSVersion
globals.addRow(dt, "BIOSVersion",
arrValue)
Next
End If
globals.addRow(dt, "CurrentLanguage", queryObj
("CurrentLanguage"))
globals.addRow(dt, "Description", queryObj
("Description"))
globals.addRow(dt, "Manufacturer", queryObj
("Manufacturer"))
globals.addRow(dt, "PrimaryBIOS", queryObj
("PrimaryBIOS"))
globals.addRow(dt, "ReleaseDate", queryObj
("ReleaseDate"))
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_BIOS",,48)
For Each objItem in colItems
If isNull(objItem.BIOSVersion) Then
Wscript.Echo "BIOSVersion: "
Else
Wscript.Echo "BIOSVersion: " & Join(objItem.BIOSVersion,
",")
End If
Wscript.Echo "CurrentLanguage: " & objItem.CurrentLanguage
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "PrimaryBIOS: " & objItem.PrimaryBIOS
Wscript.Echo "ReleaseDate: " & objItem.ReleaseDate
Next

Getting Hardware Information using Visual


Basic.NET and VBScript continued - Listing
the “BUS” information
(Page 2 of 5 )

Some scenarios may require some properties of the BUS to be listed. We can retrieve the same
information dynamically using VB.NET. Before trying to retrieve the hardware information, let
us create the wrapper with BUS structure:
Public Function getBusStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("BusType"))
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("PNPDeviceID"))
dt.Columns.Add(New DataColumn("SystemName"))
Return dt
End Function
The following method “addBus” adds a single row based on the structure you create for the data
table using the above method.
Public Sub addBus(ByRef dt As DataTable, ByVal BusType As String, ByVal
DeviceID As String, ByVal PNPDeviceID As String, ByVal SystemName As String)
Dim dr As DataRow
dr = dt.NewRow
dr("BusType") = BusType
dr("DeviceID") = DeviceID
dr("PNPDeviceID") = PNPDeviceID
dr("SystemName") = SystemName
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support
retrieving some minimum information about the BUS 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_Bus")
Dim dt As DataTable = globals.getBusStructure
For Each queryObj As ManagementObject In searcher.Get()
globals.addBus(dt, Convert.ToString(queryObj("BusType")),
queryObj("DeviceID"), queryObj("PNPDeviceID"), 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_Bus",,48)
For Each objItem in colItems
Wscript.Echo "BusType: " & objItem.BusType
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
Wscript.Echo "SystemName: " & objItem.SystemName
Next

Getting Hardware Information using Visual


Basic.NET and VBScript continued - Listing
the “MotherBoard” information
(Page 3 of 5 )

My previous article already listed some “On Board Devices” and also some information abou the
“Base Board.” This section works as an extension to both of them. Basically, we are going to
try to retrieve the “PrimaryBusType”, “SecondaryBusType” and “DeviceID” of the available
“MotherBoard.” Before trying to retrieve the hardware information, let us create the wrapper
with “MotherBoard” structure:
Public Function getMotherBoardDevice() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("PrimaryBusType"))
dt.Columns.Add(New DataColumn("SecondaryBusType"))
Return dt
End Function
The following method “addMotherBoardDevice” adds a single row based on the structure you
create for the data table using the above method.
Public Sub addMotherBoardDevice(ByRef dt As DataTable, ByVal DeviceID As
String, ByVal PrimaryBusType As String, ByVal SecondaryBusType As String)
Dim dr As DataRow
dr = dt.NewRow
dr("DeviceID") = DeviceID
dr("PrimaryBusType") = PrimaryBusType
dr("SecondaryBusType") = SecondaryBusType
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support
retrieving some minimum information about the motherboard 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_MotherboardDevice")

Dim dt As DataTable = globals.getMotherBoardDevice


For Each queryObj As ManagementObject In searcher.Get
()
globals.addMotherBoardDevice(dt, queryObj("DeviceID"), queryObj
("PrimaryBusType"), queryObj("SecondaryBusType"))
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_MotherboardDevice",,48)
For Each objItem in colItems
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "PrimaryBusType: " & objItem.PrimaryBusType
Wscript.Echo "SecondaryBusType: " & objItem.SecondaryBusType
Next
Getting Hardware Information using Visual
Basic.NET and VBScript continued - Listing
the Memory information
(Page 4 of 5 )

If you really want to retrieve memory information (especially RAM), you must go through this
section. The following code displays all RAM devices on your computer along with the
starting and ending address. Before trying to retrieve the hardware information, let us create the
wrapper with “MemoryDevice” structure:
Public Function getMemoryDeviceStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("DeviceID"))
dt.Columns.Add(New DataColumn("EndingAddress"))
dt.Columns.Add(New DataColumn("StartingAddress"))
dt.Columns.Add(New DataColumn("SystemName"))
Return dt
End Function
The following method “addMemoryDevice” adds a single row based on the structure you create
for the data table using the above method.
Public Sub addMemoryDevice(ByRef dt As DataTable, ByVal DeviceID As
String, ByVal EndingAddress As String, ByVal StartingAddress As String, ByVal
SystemName As String)
Dim dr As DataRow
dr = dt.NewRow
dr("DeviceID") = DeviceID
dr("EndingAddress") = EndingAddress
dr("StartingAddress") = StartingAddress
dr("SystemName") = SystemName
dt.Rows.Add(dr)
End Sub
After creating the wrapper, the following VB.NET code should support retrieving some
minimum information about the memory 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_MemoryDevice")
Dim dt As DataTable = globals.getMemoryDeviceStructure
For Each queryObj As ManagementObject In searcher.Get
()
globals.addMemoryDevice(dt, queryObj("DeviceID"),
Convert.ToString(queryObj("EndingAddress")), Convert.ToString
(queryObj("StartingAddress")), 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_MemoryDevice",,48)
For Each objItem in colItems
Wscript.Echo "DeviceID: " & objItem.DeviceID
Wscript.Echo "EndingAddress: " & objItem.EndingAddress
Wscript.Echo "StartingAddress: " & objItem.StartingAddress
Wscript.Echo "SystemName: " & objItem.SystemName
Next

Getting Hardware Information using Visual


Basic.NET and VBScript continued - Listing
the “Sound Device” information
(Page 5 of 5 )

The following code displays information about the audio device installed on your computer.
Before trying to retrieve the hardware information, let us create the wrapper with “SoundDevice”
structure:
Public Function getSoundDeviceStructure() As DataTable
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Manufacturer"))
dt.Columns.Add(New DataColumn("Name"))
dt.Columns.Add(New DataColumn("PNPDeviceID"))
dt.Columns.Add(New DataColumn("ProductName"))
Return dt
End Function
The following method “addSoundDevice” adds single row based on the structure you create for
the data table using the above method.
Public Sub addSoundDevice(ByRef dt As DataTable, ByVal Manufacturer As
String, ByVal Name As String, ByVal PNPDeviceID As String, ByVal ProductName
As String)
Dim dr As DataRow
dr = dt.NewRow
dr("Manufacturer") = Manufacturer
dr("Name") = Name
dr("PNPDeviceID") = PNPDeviceID
dr("ProductName") = ProductName
dt.Rows.Add(dr)
End Sub
Once you complete the creation of wrapper, the following VB.NET code should support with
some minimum information about the sound device 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_SoundDevice")

Dim dt As DataTable = globals.getSoundDeviceStructure


For Each queryObj As ManagementObject In searcher.Get
()
globals.addSoundDevice(dt, queryObj
("Manufacturer"), queryObj("Name"), queryObj("PNPDeviceID"),
queryObj("ProductName"))
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_SoundDevice",,48)
For Each objItem in colItems
Wscript.Echo "Manufacturer: " & objItem.Manufacturer
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "PNPDeviceID: " & objItem.PNPDeviceID
Wscript.Echo "ProductName: " & objItem.ProductName
Next
Any comments, suggestions, bugs, errors, feedback etc. are highly appreciated at
jag_chat@yahoo.com.

You might also like