Sign in to follow this  
Followers 0
BlackHoleSun

WMIService saying it is an object when it's not

4 posts in this topic

I'm writing a program that will list the IP addresses for the active network adapters on a computer. If there are none active (wired and wireless) I would like it to reflect this, but it is going into the loop as if "If IsObj($colItems)" is true. Below is the query I'm running.

#include <Array.au3>
#include <Constants.au3>
 
Global $bMACID
Dim $aStatus[2]
Dim $a = 0
 
$bMACID = False
 
_GetNetworkInfo()
 
Func _GetNetworkInfo()
    Local $wbemFlagForwardOnly = 0x20
    Local $wbemFlagReturnImmediately = 0x10
    Local $colItems, $objWMIService, $objItem
    Dim $NetworkInfoEx[3][13], $aNetInfo[30][3], $i = 1, $b = 0
 
$a = 0
 
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly) ;
    If IsObj($colItems) Then
        For $objItem In $colItems
            ReDim $NetworkInfoEx[UBound($NetworkInfoEx) + 1][13]
  
   Local $ipAdd = $objItem.IPAddress
  
            $NetworkInfoEx[$i][0] = $objItem.Caption
            $NetworkInfoEx[$i][1] = $objItem.description
            $NetworkInfoEx[$i][2] = $ipAdd[0]
            $NetworkInfoEx[$i][3] = $objItem.IPSubnet
            $NetworkInfoEx[$i][4] = $objItem.DefaultIPGateway
            $NetworkInfoEx[$i][5] = $objItem.DHCPEnabled
            $NetworkInfoEx[$i][6] = $objItem.DHCPServer
            $NetworkInfoEx[$i][7] = $objItem.DNSDomain
            $NetworkInfoEx[$i][8] = $objItem.DNSHostName
            $NetworkInfoEx[$i][9] = $objItem.DNSServerSearchOrder
            $NetworkInfoEx[$i][10] = $objItem.MACAddress
            $NetworkInfoEx[$i][11] = $objItem.WINSPrimaryServer
            $NetworkInfoEx[$i][12] = $objItem.WINSSecondaryServer
  
        ConsoleWrite($NetworkInfoEx[$i][1] & @CRLF)
        ConsoleWrite($NetworkInfoEx[$i][2] & @CRLF)
  
            $i += 1
   $a += 1
        Next
  Do
   $b += 1
   $aNetInfo[$b][0] = $NetworkInfoEx[$b][1]
   $aNetInfo[$b][1] = $NetworkInfoEx[$b][10]
   $aNetInfo[$b][2] = $NetworkInfoEx[$b][2]
   MsgBox(0, $NetworkInfoEx[$b][1], $NetworkInfoEx[$b][1] & @CRLF & _
    "MAC ID: " & $NetworkInfoEx[$b][10] & @CRLF & _
    "IP Address: " & $NetworkInfoEx[$b][2])
  Until $b = $a
        $NetworkInfoEx[0][0] = UBound($NetworkInfoEx) - 1
        If $NetworkInfoEx[0][0] < 1 Then
            SetError(1, 1, 0)
        EndIf
    Else
        SetError(1, 2, 0)
  $aNetInfo[0][0] = ""
  $aNetInfo[0][1] = ""
  $aNetInfo[0][2] = ""
    EndIf
Return $aNetInfo
;Return $NetworkInfoEx
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

What do you mean with "...I would like it to reflect this..."?

Do you want to display also non active network interfaces?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

What do you mean with "...I would like it to reflect this..."?

Do you want to display also non active network interfaces?

Br,

UEZ

I don't want to report non-active ones, but I would like it to say "No active adapters found" or something similar.

Share this post


Link to post
Share on other sites

Try this:

#include <Array.au3>
#include <Constants.au3>
 
Global $oErrorHandler = ObjEvent("AutoIt.Error", "ObjErrorHandler")
 
$aResult = _GetNetworkInfo()
If Not @error Then
_ArrayDisplay($aResult)
ElseIf @error = 1 Then
MsgBox(0, "Information", "No adapter found")
EndIf
 
 
Func _GetNetworkInfo()
$objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 0x30) ;
If IsObj($colItems) Then
Local $NetworkInfoEx[1][13]
Local $i = 0
For $objItem In $colItems
ReDim $NetworkInfoEx[$i + 1][13]
$NetworkInfoEx[$i][0] = $objItem.Caption
$NetworkInfoEx[$i][1] = $objItem.Description
Local $ipAdd = $objItem.IPAddress
$NetworkInfoEx[$i][2] = $ipAdd[0]
$NetworkInfoEx[$i][3] = $objItem.IPSubnet
$NetworkInfoEx[$i][4] = $objItem.DefaultIPGateway
$NetworkInfoEx[$i][5] = $objItem.DHCPEnabled
$NetworkInfoEx[$i][6] = $objItem.DHCPServer
$NetworkInfoEx[$i][7] = $objItem.DNSDomain
$NetworkInfoEx[$i][8] = $objItem.DNSHostName
$NetworkInfoEx[$i][9] = $objItem.DNSServerSearchOrder
$NetworkInfoEx[$i][10] = $objItem.MACAddress
$NetworkInfoEx[$i][11] = $objItem.WINSPrimaryServer
$NetworkInfoEx[$i][12] = $objItem.WINSSecondaryServer
$i += 1
Next
If Not $i Then Return SetError(1, 0, 0)
Return $NetworkInfoEx
Else
Return SetError(2, 0, 0)
EndIf
Return SetError(3, 0, 0)
EndFunc   ;==>_GetNetworkInfo
 
Func ObjErrorHandler()
ConsoleWrite( "A COM Error has occured!" & @CRLF  & @CRLF & _
"err.description is: "    & @TAB & $oErrorHandler.description    & @CRLF & _
"err.windescription:"     & @TAB & $oErrorHandler & @CRLF & _
"err.number is: "         & @TAB & Hex($oErrorHandler.number, 8)  & @CRLF & _
"err.lastdllerror is: "   & @TAB & $oErrorHandler.lastdllerror   & @CRLF & _
"err.scriptline is: "     & @TAB & $oErrorHandler.scriptline     & @CRLF & _
"err.source is: "         & @TAB & $oErrorHandler.source         & @CRLF & _
"err.helpfile is: "       & @TAB & $oErrorHandler.helpfile       & @CRLF & _
"err.helpcontext is: "    & @TAB & $oErrorHandler.helpcontext & @CRLF _
)
EndFunc

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0