WMI in VB.Net

  • Thread starter Thread starter zurg
  • Start date Start date
Z

zurg

Hi!
I want to use a working WMI script in VB.Net but I get an error:
''An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll''

That's script from WMI (it works):
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_NetworkAdapter",,48)
For Each objItem in colItems

Wscript.Echo "Net Connection Status: " & objItem.NetConnectionStatus
Next

and in VB.Net(doesn't work - it crashes on ''For Each...'' line):
Module Module1

Public Sub Main()

Dim strComputer As String

Dim objWMIService, colItems, objItem As Object

strComputer = "."

objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter", ,
48)

For Each objItem In colItems

MsgBox("Net Connection Status: " & objItem.NetConnectionStatus)

Next

End Sub

End Module
 
Hi,

Add a reference to system.management.dll. Here is a quick example.

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_NetworkAdapter")

moReturn = moSearch.Get

Debug.WriteLine("Net Connection Status")

Debug.Indent()

For Each mo In moReturn

Try

Debug.Write(mo("Name").ToString)

Debug.Write(" ")

Debug.Write(mo("NetConnectionStatus").ToString)

Catch

End Try

Debug.WriteLine("")

Next

Debug.Unindent()

Ken
 
Maybe it is not returning an object collection in the line prior to the For
Each statement you could check that in debug or try putting the whole lot in
a Try Catch Statement and then using the exception.ToString() Method to get
a more full description of the failure.

OHM
 
Back
Top