2 posts in this topic
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?
#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 188.8.131.52 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 Step 1 MsgBox(0, "Test _ComputerGetBootConfig", "Name: " & $BootConfig[$i] & @CRLF & _ "Boot Directory: " & $BootConfig[$i] & @CRLF & _ "Configuration Path: " & $BootConfig[$i] & @CRLF & _ "Last Drive: " & $BootConfig[$i] & @CRLF & _ "Description: " & $BootConfig[$i] & @CRLF & _ "Scratch Directory: " & $BootConfig[$i] & @CRLF & _ "Setting ID: " & $BootConfig[$i] & @CRLF & _ "Temp Directory: " & $BootConfig[$i]) 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, $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] $aBootConfigInfo[$i] = $objItem.Name $aBootConfigInfo[$i] = $objItem.BootDirectory $aBootConfigInfo[$i] = $objItem.ConfigurationPath $aBootConfigInfo[$i] = $objItem.LastDrive $aBootConfigInfo[$i] = $objItem.Description $aBootConfigInfo[$i] = $objItem.ScratchDirectory $aBootConfigInfo[$i] = $objItem.SettingID $aBootConfigInfo[$i] = $objItem.TempDirectory $i += 1 Next $aBootConfigInfo = UBound($aBootConfigInfo) - 1 If $aBootConfigInfo < 1 Then SetError(1, 1, 0) EndIf Else SetError(1, 2, 0) EndIf EndFunc ;_ComputerGetBootConfig
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
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
$aCoord = PixelSearch ($left, $top, $right, $bottom, 0x3B5E05, 0)
If @error Then $e = 2
How do I make it so the loop restarts if the colour isn't there, but it continues normally if the colour is there?
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.
$cords = PixelSearch(564, 188,710, 350,0x380030)
If Not (@error) Then
If (@error) Then
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?