I am trying to retrive some WMI properties using Option Strict On.
This requires the use of InvokeMember.
I know that there are alternative ways to get the values, but I want to
learn how to use WMI with InvokeMember and Option Strict On.
I'm getting an "Unknown Name" error in the following statement in the code
below.
objProp = typeObjProps.In vokeMember("Ite m", _
BindingFlags.De fault Or BindingFlags.Ge tProperty, Nothing, _
objProps, New Object() {propIndices(i) })
I expect that I am using the wrong object type somewhere or I need different
Binding flags.
Suggestions?
The code runs in the click event for a Button and uses a ListBox1 for
output.
Option Strict On
Imports WbemScripting
Imports System.Reflecti on
Public Class Form1
Inherits System.Windows. Forms.Form
Enum props
ComponentId
LocalServer32
ProgId
VersionIndepend entProgId
End Enum
Private Sub btnRunMe_Click( ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnRunMe.Click
Const high As Integer = props.VersionIn dependentProgId
Const strComputer As String = "." ' "." for local computer
Dim i As Integer
Dim objProp As Object
Dim objProps As Object
Dim propIndices(hig h) As String
Dim propValue(high) As String
Dim typeObjProp As Type
Dim typeObjProps As Type
Dim wmiObjectSet As SWbemObjectSet
Dim wmiServices As SWbemServices
propIndices(pro ps.ComponentId) = "ComponentI d"
propIndices(pro ps.LocalServer3 2) = "LocalServe r32"
propIndices(pro ps.ProgId) = "ProgId"
propIndices(pro ps.VersionIndep endentProgId) = "VersionIndepen dentProgId"
wmiServices = DirectCast(GetO bject("winmgmts :" _
& "{impersonation Level=impersona te}!\\" & strComputer _
& "\root\cimv 2"), SWbemServices)
wmiObjectSet = wmiServices.Exe cQuery _
("Select * from Win32_ClassicCO MClassSetting WHERE " _
& "VersionIndepen dentProgId='Moz illa.Browser'" _
& "Or VersionIndepend entProgId='Inte rnetExplorer.Ap plication'" _
& "Or VersionIndepend entProgId='Exce l.Application'" _
& "Or VersionIndepend entProgId='Word .Application'")
' Note: The following runs slowly. Give it a minute or so.
''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' ''''''''''''''' '
''''''''''
' Following is not correct
For Each objProps In wmiObjectSet
typeObjProps = objProps.GetTyp e()
For i = 0 To propIndices.Len gth - 1
Try
objProp = typeObjProps.In vokeMember("Ite m", _
BindingFlags.De fault Or BindingFlags.Ge tProperty, Nothing, _
objProps, New Object() {propIndices(i) })
If objProp Is Nothing Then
propValue(i) = ""
Else
typeObjProp = objProp.GetType ()
propValue(i) = typeObjProp.Inv okeMember("Valu e", _
BindingFlags.De fault Or BindingFlags.Ge tProperty, Nothing, _
objProp, New Object() {}).ToString()
End If
Catch ex As Exception
propValue(i) = ""
'MessageBox.Sho w(ex.Message, propIndices(i))
End Try
Next i
With ListBox1
With .Items
.Add("ProgId = " & propValue(props .ProgId))
.. Add("VersionInd ependentProgId = " &
propValue(props .VersionIndepen dentProgId))
.Add("Component Id = " & propValue(props .ComponentId))
.Add("LocalServ er32 = " & propValue(props .LocalServer32) )
End With
.Update()
End With
Next objProps
objProp = Nothing
objProps = Nothing
typeObjProp = Nothing
typeObjProps = Nothing
wmiObjectSet = Nothing
wmiServices = Nothing
GC.Collect()
GC.WaitForPendi ngFinalizers()
GC.Collect()
GC.WaitForPendi ngFinalizers()
End Sub
End Class