Jump to content

Recommended Posts

Posted

Hi all,

Bit of a fun one with querying WMI objects and setting errors within a function.  Below is example code that I've used from JSThePatriot previous UDFs but the example would apply to many other scenarios.  Basically the SetError should be called if $colItems is not an object as a result of the WMI query.  This is then used to create a corresponding error message.

I've tried disabling WMIC thus forcing it to not be an object, but rather than catching the error and telling me about it, the code simply fails therefore surely making the purpose of the logging useless??  I've experienced this before but in other languages such as java i would use a try catch etc and get around this....any suggestions for its use in AutoIT?


Thanks

 

 

#region Header
#comments-start
    Title:          Computer Information Automation UDF Library for AutoIt3 - EXAMPLES
    Filename:       CompInfoExamples.au3
    Description:    Examples using the UDF's from CompInfo.au3
    Author:         Jarvis J. Stubblefield (JSThePatriot) http://www.vortexrevolutions.com/
    Version:        00.03.08
    Last Update:    11.09.06
    Requirements:   AutoIt v3.2 +, Developed/Tested on WindowsXP Pro Service Pack 2
    Notes:          Errors associated with incorrect objects will be common user errors. AutoIt beta 3.1.1.63 has added an ObjName()
    function that will be used to trap and report most of these errors.

    Special thanks to Firestorm (Testing, Use), Koala (Testing, Bug Fix), and everyone else that has helped in the creation of this Example File.
#comments-end
#endregion Header

#region Global Variables and Constants
If Not(IsDeclared("$cI_CompName")) Then
    Global  $cI_CompName = @ComputerName
EndIf
Global Const $cI_VersionInfo        = "00.03.08"
Global Const $cI_aName              = 0, _
             $cI_aDesc              = 4
Global  $wbemFlagReturnImmediately  = 0x10, _   ;DO NOT CHANGE
$wbemFlagForwardOnly        = 0x20              ;DO NOT CHANGE
Global  $ERR_NO_INFO                = "Array contains no information", _
        $ERR_NOT_OBJ                = "$colItems isnt an object"
#endregion Global Variables and Constants

#Region Boot Configuration
Dim $BootConfig
_ComputerGetBootConfig($BootConfig)

If @error Then
    $error = @error
    $extended = @extended

    Switch $extended
        Case 1
            _ErrorMsg($ERR_NO_INFO)
        Case 2
            _ErrorMsg($ERR_NOT_OBJ)
    EndSwitch
EndIf

For $i = 1 To $BootConfig[0][0] Step 1
    MsgBox(0, "Test _ComputerGetBootConfig", "Name: " & $BootConfig[$i][0] & @CRLF & _
            "Boot Directory: " & $BootConfig[$i][1] & @CRLF & _
            "Configuration Path: " & $BootConfig[$i][2] & @CRLF & _
            "Last Drive: " & $BootConfig[$i][3] & @CRLF & _
            "Description: " & $BootConfig[$i][4] & @CRLF & _
            "Scratch Directory: " & $BootConfig[$i][5] & @CRLF & _
            "Setting ID: " & $BootConfig[$i][6] & @CRLF & _
            "Temp Directory: " & $BootConfig[$i][7])
Next
#endregion Boot Configuration

#region ---- Internal Functions
Func _ErrorMsg($message, $time = 0)
    MsgBox(48 + 262144, "Error!", $message, $time)
    ConsoleWrite("Error!" & $message & $time & @CRLF)
EndFunc
#endregion Internal Functions

Func _ComputerGetBootConfig(ByRef $aBootConfigInfo)
    Local $colItems, $objWMIService, $objItem
    Dim $aBootConfigInfo[1][8], $i = 1

    $objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BootConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            ReDim $aBootConfigInfo[UBound($aBootConfigInfo) + 1][8]
            $aBootConfigInfo[$i][0]  = $objItem.Name
            $aBootConfigInfo[$i][1]  = $objItem.BootDirectory
            $aBootConfigInfo[$i][2]  = $objItem.ConfigurationPath
            $aBootConfigInfo[$i][3]  = $objItem.LastDrive
            $aBootConfigInfo[$i][4]  = $objItem.Description
            $aBootConfigInfo[$i][5]  = $objItem.ScratchDirectory
            $aBootConfigInfo[$i][6]  = $objItem.SettingID
            $aBootConfigInfo[$i][7]  = $objItem.TempDirectory
            $i += 1
        Next
        $aBootConfigInfo[0][0] = UBound($aBootConfigInfo) - 1
        If $aBootConfigInfo[0][0] < 1 Then
            SetError(1, 1, 0)
        EndIf
    Else
        SetError(1, 2, 0)
    EndIf
EndFunc ;_ComputerGetBootConfig

 

Posted

There is no try/catch or OnError in AutoIt, although there is a way to catch Object errors using ObjEvent and "AutoIt.Error". Here's how you can do it with the existing code you have.

#Region Header
#comments-start
    Title:          Computer Information Automation UDF Library for AutoIt3 - EXAMPLES
    Filename:       CompInfoExamples.au3
    Description:    Examples using the UDF's from CompInfo.au3
    Author:         Jarvis J. Stubblefield (JSThePatriot) http://www.vortexrevolutions.com/
    Version:        00.03.08
    Last Update:    11.09.06
    Requirements:   AutoIt v3.2 +, Developed/Tested on WindowsXP Pro Service Pack 2
    Notes:          Errors associated with incorrect objects will be common user errors. AutoIt beta 3.1.1.63 has added an ObjName()
    function that will be used to trap and report most of these errors.

    Special thanks to Firestorm (Testing, Use), Koala (Testing, Bug Fix), and everyone else that has helped in the creation of this Example File.
#comments-end
#EndRegion Header

#Region Global Variables and Constants
If Not (IsDeclared("$cI_CompName")) Then
    Global $cI_CompName = @ComputerName
EndIf
Global Const $cI_VersionInfo = "00.03.08"
Global Const $cI_aName = 0, _
        $cI_aDesc = 4
Global $wbemFlagReturnImmediately = 0x10, _   ;DO NOT CHANGE
        $wbemFlagForwardOnly = 0x20 ;DO NOT CHANGE
Global $ERR_NO_INFO = "Array contains no information", _
        $ERR_NOT_OBJ = "$colItems isnt an object"
#EndRegion Global Variables and Constants

#Region Boot Configuration
Dim $BootConfig
_ComputerGetBootConfig($BootConfig)

If @error Then
    $error = @error
    $extended = @extended

    Switch $extended
        Case 1
            _ErrorMsg($ERR_NO_INFO)
        Case 2
            _ErrorMsg($ERR_NOT_OBJ)
    EndSwitch
EndIf

For $i = 1 To $BootConfig[0][0] Step 1
    MsgBox(0, "Test _ComputerGetBootConfig", "Name: " & $BootConfig[$i][0] & @CRLF & _
            "Boot Directory: " & $BootConfig[$i][1] & @CRLF & _
            "Configuration Path: " & $BootConfig[$i][2] & @CRLF & _
            "Last Drive: " & $BootConfig[$i][3] & @CRLF & _
            "Description: " & $BootConfig[$i][4] & @CRLF & _
            "Scratch Directory: " & $BootConfig[$i][5] & @CRLF & _
            "Setting ID: " & $BootConfig[$i][6] & @CRLF & _
            "Temp Directory: " & $BootConfig[$i][7])
Next
#EndRegion Boot Configuration

#Region ---- Internal Functions
Func _ErrorMsg($message, $time = 0)
    MsgBox(48 + 262144, "Error!", $message, $time)
    ConsoleWrite("Error!" & $message & $time & @CRLF)
EndFunc   ;==>_ErrorMsg
#EndRegion ---- Internal Functions
Func _COMErrFunc() ; <<<<<<<<<<<<<<<<<<<<<<<<
    _ErrorMsg($ERR_NOT_OBJ)
EndFunc   ;==>_COMErrFunc

Func _ComputerGetBootConfig(ByRef $aBootConfigInfo)
    Local $oError = ObjEvent("AutoIt.Error", "_COMErrFunc") ; <<<<<<<<<<<<<<<<<<<
    Local $colItems, $objWMIService, $objItem
    Dim $aBootConfigInfo[1][8], $i = 1

    $objWMIService = ObjGet("winmgmts:\\" & $cI_CompName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BootConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
        For $objItem In $colItems
            ReDim $aBootConfigInfo[UBound($aBootConfigInfo) + 1][8]
            $aBootConfigInfo[$i][0] = $objItem.Name
            $aBootConfigInfo[$i][1] = $objItem.BootDirectory
            $aBootConfigInfo[$i][2] = $objItem.ConfigurationPath
            $aBootConfigInfo[$i][3] = $objItem.LastDrive
            $aBootConfigInfo[$i][4] = $objItem.Description
            $aBootConfigInfo[$i][5] = $objItem.ScratchDirectory
            $aBootConfigInfo[$i][6] = $objItem.SettingID
            $aBootConfigInfo[$i][7] = $objItem.TempDirectory
            $i += 1
        Next
        $aBootConfigInfo[0][0] = UBound($aBootConfigInfo) - 1
        If $aBootConfigInfo[0][0] < 1 Then
            SetError(1, 1, 0)
        EndIf
    Else
        SetError(1, 2, 0)
    EndIf
EndFunc   ;==>_ComputerGetBootConfig

Although, I'd write your code this way to check to see if the ObjGet worked first, before checking for WMI values.

; What you have
    $objWMIService = ObjGet("winmgmts:\\" & $cI_CompName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BootConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

    If IsObj($colItems) Then
; another way of doing it.
    $objWMIService = ObjGet("winmgmts:\\" & $cI_CompName & "\root\CIMV2")

    If IsObj($objWMIService) Then ; <<<<<<<<<<<<<<<<< Note change of variable name
        $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BootConfiguration", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

Awesome! Never knew you could do that.  I completely understand your final remark about checking the ObjGet worked first, that should be a simple change for my code, but as for the ObjEvent("AutoIt.Error","_COMErrFunc") I'm not quite there yet!

Does that mean I can still keep my SetErrors and all of that functionality (where applicable) within my code and this extra bit will catch those critical AutoIT errors that prevent it running then?  I'll have to dig through the related help file and get my head around it! :)

Thanks very much!

Edited by RC86
Posted

The ObjEvent only catches COM errors.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted
3 minutes ago, BrewManNH said:

The ObjEvent only catches COM errors.

Gotcha, very handy! THanks for the help!

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
×
×
  • Create New...