RC86

Error Logging Problem

5 posts in this topic

#1 ·  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

 

Share this post


Link to post
Share on other sites



#2 ·  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)

 

1 person likes this

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

Share this post


Link to post
Share on other sites

#3 ·  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

Share this post


Link to post
Share on other sites

#4 ·  Posted

The ObjEvent only catches COM errors.

1 person likes this

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

Share this post


Link to post
Share on other sites

#5 ·  Posted

3 minutes ago, BrewManNH said:

The ObjEvent only catches COM errors.

Gotcha, very handy! THanks for the help!

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

  • Similar Content

    • TheDcoder
      By TheDcoder
      Hello, I recently opened a bug report without reading the Helpfile... My bad . After @Melba23's gentle reminder, I was curious about why it was like that.
      It is about SetError's behaviour. This is the example from the bug report:
      Example() If @error Then ConsoleWrite("Error" & @CRLF) Else ConsoleWrite("No Error" & @CRLF) EndIf Func Example() SetError(1) Sleep(1000) EndFunc What I tried to do is set Example's (my user defined function's) @error value to 1... but the value set by SetError is cleared after calling a function, I wonder why? Why should calling to an external function effect my function's @error which is set when my function returns.
      Setting the error of a UDF in advance by using SetError makes sense... but I cannot find a reason why calling a function should clear it? Please note that I am not talking about @error, I am talking about the @error set by my function when it ends/returns!
      I hope someone can enlighten me, thanks for the answers in advance!
      P.S I tried to explain my best but my English is not very good and I didn't feel like I did a good job explaining today, so please pardon any mistakes that I have made
    • PEscobar
      By PEscobar
      So I made a loop beginning with while $e = 1. It opens a file and searches for a colour in the file, if the colour isn't there $e = 2 so the loop ends and restarts. Instead of that, the loop runs once and then just stops completely, even if the colour is there. 
      Here is what it looks like basically.
      While $e = 1 
          OpenPng()
            $aCoord = PixelSearch ($left, $top, $right, $bottom, 0x3B5E05, 0)
            If @error Then $e = 2
      WEnd
      How do I make it so the loop restarts if the colour isn't there, but it continues normally if the colour is there?
       
       
    • kparker
      By kparker
      I did look at the help file and did not see an indicator of the answer.  The @error variable that inherits a value when a function produces an error; is this value overwritten as success if a function right below it is successful?   It seems that I could possibly check the error value at the end of a UDF if it always maintains an error value once generated, but if its overwritten by successful functions, I would have to check every function call.  I find this more relevant for browser automation.  i.e. checking every time I navigate.
       
    • Reptos
      By Reptos
      How should i go about repeating this section if@error? I want it to try again if it can not find the PixelSearch then move on to the next Search.
      While 1
          $cords = PixelSearch(564, 188,710, 350,0x380030)
         If Not (@error) Then
            MouseClick("Left",$cords[0],$cords[1],1,1)
            EndIf
            If (@error) Then
              ??????
            EndIf
    • TheDcoder
      By TheDcoder
      Hello , I was thinking of a situation where a key in a ini file can contain anything, If we were to know if the key does not exists using IniRead, Its not possible to do it without compromising a single possibility... I was thinking that if IniRead were to set @error when a key does not exist, it solves the problem which I mentioned before
       
      What do you think? Would you like this feature?
      TD