WMI Question

S

shane

I am trying to get values from a WMI Query in my textbox, txtResults I don't
think I am understand what I need to do to move the value from the for each
statement in the quary into the textbox, as the default implementation just
provides for outputing information in the console.
' this wont work and when i try to replace the console writeline with some
kind of txtresluts.text .,.....I either get an error or only the first set
of values returned.
For Each queryObj As ManagementObject in searcher.Get()

Console.WriteLine("-----------------------------------")
Console.WriteLine("Win32_Product instance")
Console.WriteLine("-----------------------------------")
Console.WriteLine("Caption: {0}", queryObj("Caption"))
Next.

I can write values in a class property that do not return multiple values,
but am not able to get a return on anything but the first set of values.

What Am I doing Wrong???

thanks..Shane
 
N

Newbie Coder

Shane,

The code below does what you want but there is a pause getting the results
in the textbox

Start a new Windows Application
Add reference to System.Management
Paste in the code below:

----------------------------------------
Imports System.Management

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents txtResults As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.txtResults = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'txtResults
'
Me.txtResults.Location = New System.Drawing.Point(16, 16)
Me.txtResults.Multiline = True
Me.txtResults.Name = "txtResults"
Me.txtResults.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.txtResults.Size = New System.Drawing.Size(288, 192)
Me.txtResults.TabIndex = 0
Me.txtResults.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 224)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Get"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(232, 224)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(322, 263)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.txtResults)
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Results"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strTemp As String
Try
Dim mos As New ManagementObjectSearcher("root\CIMV2", "SELECT *
FROM Win32_Product")
For Each mo As ManagementObject In mos.Get()
txtResults.Text += CType(mo("Caption"), String) &
ControlChars.CrLf
Next
Catch ex As ManagementException
MessageBox.Show("An error occurred", Me.Text)
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Application.Exit()
End Sub
End Class
 
S

shane

thanks,

this code work great, but will not work for values of types other than
string, If i want to add a value that is not of type, how would i format the
value?

I have tried a few variations and keep getting the following message
"Additional information: Cast from type 'UInt64' to type 'Long' is not
valid."

here is the line that seems to be miscast.. txtresults.Text +=
CType(mo("size"), String) & ControlChars.CrLf

thanks,

shane





Dim strTemp As String

Try

Dim mos As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM
Win32_LogicalDisk")

For Each mo As ManagementObject In mos.Get()

txtResults.Text += CType(mo("size"), String) & ControlChars.CrLf

Next

Catch ex As ManagementException

MessageBox.Show("An error occurred", Me.Text)

End Try
 
S

shane

here is what I came up with...I can now pass in what area of the WMI inned
to look at..thanks for your help :)

Function WMI_TextBox_Writer(ByVal strRootWMI As String, ByVal strNameWMI As
String, ByVal WMIname As String) 'byval strRootWMI As String ,byval
strNameWMI As String, byval WMIname As String

Dim strTemp As Int64

'Dim test3 As String = m_strTPM

Try

'These Variables are for legacy implementation

'this was so i can remove comments and set function not to take variables

'Dim strRootWMI As String = "root\CIMV2"

'Dim strNameWMI As String = "SELECT * FROM Win32_LogicalDisk"

'Dim WMIname As String = "size"

'this set the ManagementObjectSearcher using 2 variables that are passed
into the function

Dim mos As New ManagementObjectSearcher(strRootWMI, strNameWMI)

' create a for each statment to loop through the ManagementObject

For Each mo As ManagementObject In mos.Get()

'strTemp is set to fist instance of ManagementObject

'

strTemp += System.Convert.ToInt64(mo(WMIname))

m_strTPM += System.Convert.ToString(strTemp) & vbCrLf

txtResults.Text &= "Disk Size = " & Me.propTextBox_Writer() & vbCrLf

Beep()



Next

Catch ex As ManagementException

MessageBox.Show("An error occurred", Me.Text)

End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top