Connecting Tech Pros Worldwide Forums | Help | Site Map

WMI in VB.Net

zurg
Guest
 
Posts: n/a
#1: Nov 20 '05
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



Ken Tucker [MVP]
Guest
 
Posts: n/a
#2: Nov 20 '05

re: WMI in VB.Net


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

--------------------------

"zurg" <zurg@wp.pl> wrote in message
news:%23r2ahDosDHA.2224@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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
>
>[/color]


One Handed Man
Guest
 
Posts: n/a
#3: Nov 20 '05

re: WMI in VB.Net


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

zurg wrote:[color=blue]
> 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[/color]


zurg
Guest
 
Posts: n/a
#4: Nov 20 '05

re: WMI in VB.Net


Hi!
It works perfectly - thanks Ken, you saved me;)
zurg


Closed Thread