473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Option Strict On and InvokeMember and WMI

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
Nov 21 '05 #1
4 3014

Howard Kaikow wrote:
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?
I would love to be able to say I had solved all your problems but I ran
into problems of my own.

First off: The problem in your code is that you have missed out a level
of the object hierarchy: what you call objProps is not a PropertySet,
but just a SWbemObject, so you would need to obtain it's Properties_
property (which IS a PropertySet) and then interrogate that.

However, I tried that, and I just COULD NOT get InvokeMember("I tem"...)
to work on a PropertySet object. InvokeMember("C ount"...) worked, so
the object itself was fine - it's just that somewhere in the
combination of Interop, Reflection, the optional parameter (maybe), and
the indexed property, something goes wrong.

HOWEVER

While I applaud your efforts to understand and get to grips with
InvokeMember, I must take issue with this:
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.


It looks to me like it's entirely possible to use WMI with Strict On.
This code snippet does what yours wants to do, with no InvokeMember and
all strongly typed:

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

'These are the objects we will use as we dig down the object
hierarchy
Dim wmiServices As SWbemServices
Dim wmiObjectSet As SWbemObjectSet
Dim owmiObject As Object 'a SWbemObject in the above
collection
Dim wmiObject As SWbemObject 'the above, strongly typed
Dim owmiPropertySet As Object 'the SWbemPropertySe t that is
the Properties_ property of the above
Dim wmiPropertySet As SWbemPropertySe t 'the above, strongly
typed
Dim wmiProperty As SWbemProperty 'a SWbemProperty in the
above collection

'the properties we obtain
Dim hshProperties As Hashtable

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'")

'wmiObjectSet is a SWbemObjectSet
For Each owmiObject In wmiObjectSet
hshProperties = New Hashtable

If TypeOf owmiObject Is SWbemObject Then
wmiObject = CType(owmiObjec t, SWbemObject)

'get the properties collection of this SWbemObject
owmiPropertySet = wmiObject.Prope rties_
If TypeOf owmiPropertySet Is SWbemPropertySe t Then
wmiPropertySet = CType(owmiPrope rtySet,
SWbemPropertySe t)

For Each wmiProperty In wmiPropertySet
hshProperties.A dd(wmiProperty. Name,
wmiProperty.Val ue)
Next

'now we have the properties we desire, show in list
box
With ListBox1
With .Items
Dim DesiredProperty As String
For Each DesiredProperty In
System.Enum.Get Names(GetType(p rops))
.Add(DesiredPro perty & " = " &
hshProperties(D esiredProperty) .ToString)
Next
.Add("--")
End With
.Update()
End With
End If
End If
Next

End Sub

--
Larry Lard
Replies to group please

Nov 21 '05 #2
Thanx.

I'll take a look at your code.
Nov 21 '05 #3
With your code, I now have 3 different ways to use Option Strict On with
WMI, without using InvokeMember.

1. Your method with hashtables.
2. StringReader on the wmiObject.GetOb jectText_
3. GetProperties on each object in the result of the ExecQuery.

It seems that there has to be a way to use InvokeMember, otherwise there'a a
bug in .NET.
Nov 21 '05 #4
The following seems to be better alternative.

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 propIndices(hig h) As String
Dim propValue(high) As String
Dim wmiObject As SWbemObject
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'")
For Each wmiObject In wmiObjectSet
With wmiObject
With .Properties_
For i = 0 To high
propValue(i) = .Item(propIndic es(i)).Value.To String()
Next i
End With
With ListBox1
With .Items
..Add("ProgId = " & propValue(props .ProgId))
..Add("VersionI ndependentProgI d = " &
propValue(props .VersionIndepen dentProgId))
..Add("Componen tId = " & propValue(props .ComponentId))
..Add("LocalSer ver32 = " & propValue(props .LocalServer32) )
End With
..Update()
End With
End With
Next wmiObject
wmiObjectSet = Nothing
wmiObject = Nothing
wmiServices = Nothing
End Sub

--
http://www.standards.com/; See Howard Kaikow's web site.

"Howard Kaikow" <ka****@standar ds.com> wrote in message
news:Od******** *****@tk2msftng p13.phx.gbl...
With your code, I now have 3 different ways to use Option Strict On with
WMI, without using InvokeMember.

1. Your method with hashtables.
2. StringReader on the wmiObject.GetOb jectText_
3. GetProperties on each object in the result of the ExecQuery.

It seems that there has to be a way to use InvokeMember, otherwise there'a a bug in .NET.

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2147
by: Microsoft News | last post by:
I have a project that was created all with Option Strict OFF. Works great, not a problem with it. But if I turn Option Strict ON then I get a LOT of errors. My question, should I even care about Option Strict? What advantages do I get with Option Strict On? Does better type statement make my code run faster? If anyone knows THE ANSWERS! please fill me in. I have ideas and belief but I would once and for all like to know what the...
11
2120
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad. if i have class CCar and interface ICar when im doing this : ICar = new CCar
8
1833
by: Rich | last post by:
Hello, If I leave Option Strict Off I can use the following syntax to read data from a Lotus Notes application (a NotesViewEntry object represents a row of data from a Lotus Notes View - like a record in a sql Server view) .... Dim entry As Domino.NotesViewEntry Dim obj As Object str1 = entry.ColumnValues(0)
17
4496
by: David | last post by:
Hi all, I have the following problem: my program works fine, but when I add option strict at the top of the form, the following sub fails with an error that option strict does not allow late binding. What should I do? Public Sub MyMnuHandler(ByVal sender As Object, ByVal e As System.EventArgs) If sender.checked = True Then sender.checked = False Else sender.checked = True
15
1551
by: guy | last post by:
when i first started using .net (beta 1) i came across option strict and thought hey this could be really good, and since then have always turned it on, most people here seem to agree that this is a good thing. i have now been asked to debug a vb2005 web app for 3 weeks and there is no mention of option strict in it, (there are also no classes defined, just a couple of structures) everything is define as 'as object', data coming back...
13
2329
by: C. Moya | last post by:
I fully expected the lack of a way to set Option Strict globally to be fixed in SP1. I can't seem to figure out if it has been fixed or not. It still seems we have to add the declaration at the top of each and every single code module. Am I missing something here? -- -C. Moya www.cmoya.com
1
3142
by: Jerad Rose | last post by:
I believe this issue is specific to ASP.NET. Why does VB.NET (2.0) ignore the project-level setting for Option Strict? I have the setting turned on in web.config: <compilation debug="true" strict="true" explicit="true" urlLinePragmas="true"/> And also in the preferences:
8
2429
by: Rory Becker | last post by:
A wise man once said: "Never put off until runtime what you can fix at compile time." Actually I think he said it about 10 minutes before I started this post. I am a firm believer, like the man in question, in "Option Strict On" by default. Actually I don't believe I have code where this is not the case.
8
3898
by: =?Utf-8?B?R3JlZw==?= | last post by:
We have an application in our office that has the Option Strict option set to off right now. I do understand it should be set to ON, but right now, I'm just going to continue with it this way since I do not have the time to fix everything to set it to ON. Anyway, my question is if its set to OFF why is it it keeps getting set back to ON, everytime we move the package from one machine to another. It's the Option Strict option specific to...
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.