Jump to content

_UpdateRegKey and Error Checking


Recommended Posts

Good day,

The following script...for all intents and purposes, appears to be working:

; -----------------------------------------------
_UpdateRegKey()
; -----------------------------------------------
Func _UpdateRegKey()
    ; Source data
    Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "InstallVST64Dir"
    Local $sRegSZ = "REG_SZ"
    Local $sString = "C:\RML\SAC\VST_PlugIns\Ampsim\"
    ; -----------------
    Local $iRegWrite = RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    ; -----------------------------------------------
    If @error = 1 Then
        SplashTextOn("NOTICE!!", "The Reg String " & $sString & " was not succesfully updated!", 650, 50, -1, -1)
        Sleep(3000)
        SplashOff()
    Else
        SplashTextOn("NOTICE!!", "The Reg String " & $sString & " was succesfully updated!", 650, 50, -1, -1)
        Sleep(3000)
        SplashOff()
    EndIf
EndFunc   ;==>_UpdateRegKey
; -----------------------------------------------

The assigned string is updated as noted ...with the subsequent notification, ""The Reg String C:\RML\SAC\VST_PlugIns\Ampsim\ was successfully updated!" ...being displayed accordingly.

My questions is, "When-and_how is the first SplashTextOn output reached or outputted?"

Any assistance here would be greatly appreciated!

Link to comment
Share on other sites

If you look in the help file for RegWrite there are following lines:

Return Value
Success:    1.
Failure:    0 and sets the @error flag to non-zero if error writing registry key or value.
@error:    1 = unable to open requested key
 2 = unable to open requested main key
 3 = unable to remote connect to the registry
-1 = unable to open requested value
-2 = value type not supported.

So if @error=1 then if for any reason RegWrite files to open key you will get that first splash message 

"NOTICE!!", "The Reg String " & $sString & " was not succesfully updated!"

If you want that message to be shown for each error that is listed above then you should write: "If @error Then" which means "if @error is anything except 0 then".

When there is not an error, when everything goes well, then @error has value of 0. 

Link to comment
Share on other sites

6 minutes ago, ahmet said:

When there is not an error, when everything goes well, then @error has value of 0.

Thanks for the response...appreciated!

Do you NOT mean to state, "When there is not an error, when everything goes well, then @error has value of 1?"

Edited by mr-es335
Link to comment
Share on other sites

Well, of course 1 is a success, and of course @error=0 means there were not any errors and @error=1 means there was some error. You are referring to the return value of the function. If that particular function succeeds then the value of $iRegWrite will be equal to 1. If that function fails then the value of $iRegWrite will be 0. You could write your code like this

If $iRegWrite=1 Then
;what to do if RegWrite succeeds
Else
;what to do if $iRegWrite has any other value (only zero possible)
EndIf

Inside "Else" statement you could check for specific error using values of @error.

Link to comment
Share on other sites

1 hour ago, mr-es335 said:

If so, then THIS explanation is very confusing!!

Return Value
Success:    1.
Failure:    0 and sets the @error flag to non-zero if error writing registry key or value.

The help file exactly describes how each function returns values that inform you about success or failure.

  • When everything worked as expected ("Success") you get a return value of 1.
  • When there was a problem ("Failure") the function sets the return value to 0 and to give you more detailed information what went wrong Macro @error is set to one of the described values.

Be aware that "returnvalue" is not equal to "error flag"!

If you still have problems please describe what "confuses" you. Or - even better - see the help file that describes how functions work.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

 

; https://www.autoitscript.com/forum/topic/212300-_updateregkey-and-error-checking/#comment-1537070
; -----------------------------------------------
Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
Local $sValuename = "InstallVST64Dir"
Local $sRegSZ = "REG_SZ"
Local $sString = "C:\RML\SAC\VST_PlugIns\Ampsim\"

Local $Result = _UpdateRegKey($sKeyname, $sValuename, $sRegSZ, $sString)
;~ Success: 1.
;~ Failure: 0 and sets the @error flag to non-zero if error writing registry key or value.
ConsoleWrite("$Result=" & $Result & @CRLF)
; -----------------------------------------------
Func _UpdateRegKey($sKeyname, $sValuename, $sRegSZ, $sString)
;~  ; Source data
;~  Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
;~  Local $sValuename = "InstallVST64Dir"
;~  Local $sRegSZ = "REG_SZ"
;~  Local $sString = "C:\RML\SAC\VST_PlugIns\Ampsim\"
;~  ; -----------------
    Local $iRegWrite = RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)


;~      RegWrite ( "keyname" [, "valuename", "type", value] )
;~          Success: 1.
;~          Failure: 0 and sets the @error flag to non-zero if error writing registry key or value.
;~          @error:
;~           1 = unable to open requested key
;~           2 = unable to open requested main key
;~           3 = unable to remote connect to the registry
;~          -1 = unable to open requested value
;~          -2 = value type not supported

    ; -----------------------------------------------
    If $iRegWrite = 1 Then ;Success: 1.
        SplashTextOn("NOTICE!!", "The Reg String " & $sString & " was succesfully updated!", 650, 70, -1, -1)
    Else ;Failure: 0 and sets the @error flag to non-zero if error writing registry key or value.
        Local $Msg
        Switch @error ; @error:
            Case 1
                $Msg = "unable To open requested key"
            Case 2
                $Msg = "unable To open requested main key"
            Case 3
                $Msg = "unable To remote connect To the registry"
            Case -1
                $Msg = "unable To open requested value"
            Case -2
                $Msg = "value type Not supported"
            Case Else
                $Msg = "💀 you are in trouble) "

        EndSwitch

        SplashTextOn("NOTICE!!", "The Reg String " & $sString & " was not updated!" & @CRLF & $Msg, 650, 70, -1, -1)
    EndIf

    Sleep(3000)
    SplashOff()
    Return $iRegWrite
EndFunc   ;==>_UpdateRegKey
; -----------------------------------------------

 

Edited by ioa747
Update

I know that I know nothing

Link to comment
Share on other sites

water,

Being what I would refer to as a "migrator scripter", it would appear that i do make assumption, and, as a result, the "a##" in me has been, shall we say, "exposed".

This being said, my overall scripting requirements are rather simple ones. Thus, the need for "error checking" has never, ever been an issues. This "non-issue mentality" however, is now being altered  - observing the need for such "coding practices".

The provided link regarding "Functions" has been a real "eye opener" ...so I just want to extend a heart-felt sense of gratitude to you for for that suggestion.

Edited by mr-es335
Link to comment
Share on other sites

Good day,

Can someone please assist me in getting the second response to be output on a subsequent execution of the script:

; -----------------------------------------------
_DeleteRegKey()
; -----------------------------------------------
Func _DeleteRegKey()
    ; Source data
    Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "InstallVSTDir"
    ; -----------------
    Local $iRegDelete = RegDelete($sKeyname, $sValuename)
    ; -----------------------------------------------
    If @error = 0 Then
        SplashTextOn("NOTICE!!", "The Reg Value...[" & $sValuename & "]...was succesfully deleted!", 450, 50, -1, -1)
        Sleep(3000)
        SplashOff()
    Else
        SplashTextOn("NOTICE!!", "The Reg KeyValue...[" & $sKeyname & $sValuename & "]...does not exist!", 850, 50, -1, -1)
        Sleep(3000)
        SplashOff()
    EndIf
EndFunc   ;==>_DeleteRegKey
; -----------------------------------------------

For example, on the first execution of the script...

If @error = 0 Then
        SplashTextOn("NOTICE!!", "The Reg Value...[" & $sValuename & "]...was succesfully deleted!", 450, 50, -1, -1)
        Sleep(3000)
        SplashOff()

...and on the second execution of the script...

Else
        SplashTextOn("NOTICE!!", "The Reg KeyValue...[" & $sKeyname & $sValuename & "]...does not exist!", 850, 50, -1, -1)
        Sleep(3000)
        SplashOff()
    EndIf

Again, any assistance in this matter would be greatly appreciated!

Link to comment
Share on other sites

It looks like you are confusing return value of function with @error value. RegDelete can return 3 possible values: 1, 0 and 2 (your variable $iRegDelete). If a key is deleted then the value of $iRegDelete will be 1. If there is no key then the value of $iRegDelete will be 0. If there is an error deleting a key then the value of $iRegDelete will be 2 and value of @ error will tell you what kind of error happened in this particular case for value of 2. So for this function if the key does not exists it is when the function executes it is considered as a success - no @error code is set.

Edited by ahmet
Link to comment
Share on other sites

I'd script it like this (tested)

_DeleteRegKey()
; -----------------------------------------------
Func _DeleteRegKey()
    ; Source data
    Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "InstallVSTDir"
    ; -----------------
    Local $iReturn = RegDelete($sKeyname, $sValuename)
    ; -----------------------------------------------    
    Local $iError = @error ; keep it HERE and NOW (just after the function was called +++) if you intend to test its value later    
    ConsoleWrite("$iReturn = " & $iReturn & "   $iError = " & $iError & @crlf)
    ; -----------------------------------------------   
    Switch $iReturn ; can be 0, 1, 2 (help file +++)

        Case 1 ; "success"
            SplashTextOn("RegDelete", "Reg Value...[" & $sValuename & "]...was succesfully deleted!", 450, 50)

        Case Else ; 0 or 2 (failure)
            Switch $iError
                Case 0
                    SplashTextOn("RegDelete", "Reg KeyValue...[" & $sKeyname & $sValuename & "]...does not exist!", 900, 50)
                Case 1, 2, 3, -1, -2
                    SplashTextOn("RegDelete", "An error occured . error #" & $iError & " detailed in help file", 450, 50)
            EndSwitch
    
    EndSwitch

    Sleep(5000)
    SplashOff()
EndFunc   ;==>_DeleteRegKey
; -----------------------------------------------

 

Link to comment
Share on other sites

Good day,

This day may be a revelation day...and...maybe NOT!?

As a mechanic, I have a toolbox full of various tools  - with some tools that I employ ever day, and with some of those tools being very specific - thus only being deployed when required.

The point here is - is that I should never use a hammer as  saw...and vice-versa!!

Take "Registry Manipulation" as an example here. There are five tools at our disposal, RegRead, RegenumKey, RegEnumVal, RegDelete and RegWrite.

I have asked numerous question regarding how to make a specific tool do a particular job, when maybe...just maybe, that tool was not really-and-truly designed to do that particular job in the first place. Thus, the need for "other" tools!

For example...a mock-up here...

; -----------------------------------------------
_DeleteRegKey()
; -----------------------------------------------
Func _DeleteRegKey()
    ; Source data
    Local $sKeyname = "HKEY_LOCAL_MACHINE\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "InstallVSTDir"
    ; -----------------
    ; Deploy the RegDelete tool to allow that tool to perform that tools partcular job!!!
    ; You can verify the results here as well...if you like...probably to the console.
        ; -----------------------------------------------
    ; Then, deploy the RegRead tool to allow that tool to perform that tools partcular job!!!
 EndFunc   ;==>_DeleteRegKey
; -----------------------------------------------

The question that I have now is, "May I ask why no one has made this suggestion to me in the past?"
• For some of us, we are  on a rather limited time-frame and thus, learning a thing "en masse" does ten to exacerbate things just a bit!

Just curious?

Oh! And by-the-way, I tend to refer to such times as these - that is, when I simply do not seem to get it" - as "cranial depravity"!

I hope that some will find this information to be of some practicable use?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...