Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

I'm 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

Posted

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 there's 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?

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...