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
Next: Listing the “Sound Device” information >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee