Using Option Strict On witth InvokeMember and WMI

Howard Kaikow

Member
Joined
Mar 26, 2004
Messages
21
I am trying to retrieve 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.

Im getting an "Unknown Name" error in the following statement in the code
below.

objProp = typeObjProps.InvokeMember("Item", _
BindingFlags.Default Or BindingFlags.GetProperty, 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.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
Enum props
ComponentId
LocalServer32
ProgId
VersionIndependentProgId
End Enum
Private Sub btnRunMe_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRunMe.Click
Const high As Integer = props.VersionIndependentProgId
Const strComputer As String = "." "." for local computer
Dim i As Integer
Dim objProp As Object
Dim objProps As Object
Dim propIndices(high) As String
Dim propValue(high) As String
Dim typeObjProp As Type
Dim typeObjProps As Type
Dim wmiObjectSet As SWbemObjectSet
Dim wmiServices As SWbemServices
propIndices(props.ComponentId) = "ComponentId"
propIndices(props.LocalServer32) = "LocalServer32"
propIndices(props.ProgId) = "ProgId"
propIndices(props.VersionIndependentProgId) = "VersionIndependentProgId"
wmiServices = DirectCast(GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2"), SWbemServices)
wmiObjectSet = wmiServices.ExecQuery _
("Select * from Win32_ClassicCOMClassSetting WHERE " _
& "VersionIndependentProgId=Mozilla.Browser" _
& "Or VersionIndependentProgId=InternetExplorer.Application" _
& "Or VersionIndependentProgId=Excel.Application" _
& "Or VersionIndependentProgId=Word.Application")
Note: The following runs slowly. Give it a minute or so.


Following is not correct
For Each objProps In wmiObjectSet
typeObjProps = objProps.GetType()
For i = 0 To propIndices.Length - 1
Try
objProp = typeObjProps.InvokeMember("Item", _
BindingFlags.Default Or BindingFlags.GetProperty, Nothing, _
objProps, New Object() {propIndices(i)})
If objProp Is Nothing Then
propValue(i) = ""
Else
typeObjProp = objProp.GetType()
propValue(i) = typeObjProp.InvokeMember("Value", _
BindingFlags.Default Or BindingFlags.GetProperty, Nothing, _
objProp, New Object() {}).ToString()
End If
Catch ex As Exception
propValue(i) = ""
MessageBox.Show(ex.Message, propIndices(i))
End Try
Next i
With ListBox1
With .Items
.Add("ProgId = " & propValue(props.ProgId))
. Add("VersionIndependentProgId = " &
propValue(props.VersionIndependentProgId))
.Add("ComponentId = " & propValue(props.ComponentId))
.Add("LocalServer32 = " & 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.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
 
In the MSFT newsgroups, it was pointed out that I missed a level of the object hierarchy, i.e., what I am calling objProps is not a PropertySet, rather it is just an SWbemObject, so I need to obtain its Properties_ property and then run the query on that.

However, the individual who informed me of the above found that he could not get InvokeMember("Item" ...) to work on a PropertySet object, either theres a bug in .NET or some parameter is not being used correctly.

So, this will require a bit more investigation.

Or, does somebody know if this is a known bug with InvokeMember?
 
Back
Top