Jump to content

Recommended Posts

Posted (edited)

Good day,

I would greatly appreciate ti, if someone would be so kind as to check the following Registry function scripts? There are eleven [11] such scripts in total.
• I have been endeavouring to "get my head around" error checking...and I do hope that these provided scripts demonstrate that - at least in some small way, I AM making progress!!

The data is as follows:

Spoiler

Local $sKeyname = "HKEY_CURRENT_USER\Software\First"
Local $sValuename = "Second"
Local $sRegSZ = "REG_SZ"
Local $sString = "Fourth"

Link: [Click_Me]

As always...any assistance in this matter would be greatly appreciated!

Edited by mr-es335
Posted

Hello,

In the following snippet, what would trigger @error=1? At this point, I see that this error check does absolutely nothing!!

; -----------------------------------------------
AddRegKeyHKCUValue()
; -----------------------------------------------
Func AddRegKeyHKCUValue()
    Local $sKeyname = "HKEY_CURRENT_USER\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "UserContent"
    Local $sRegSZ = "REG_SZ"
    Local $sString = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"
    ; -----------------------------------------------
    RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    ; -----------------------------------------------
    If @error = 1 Then
        SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS NOT successfully added!", 800, 50, -1, -1)
        Sleep(2000)
        SplashOff()
    Else
        SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS successfully added!", 800, 50, -1, -1)
        Sleep(2000)
        SplashOff()
    EndIf
EndFunc   ;==>AddRegKeyHKCUValue
; -----------------------------------------------

 

  • Solution
Posted

Hi @mr-es335 👋 ,

the @error marco refers to the command before. This means the function RegWrite() could came up with an error (see the help for better understanding). So this error check is valid and good practise. You can also avoid this:

If @error = 1 Then

by using simply this:

If @error Then

My quick suggestion would look like this:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

AddRegKeyHKCUValue()

Func AddRegKeyHKCUValue()
    Local $sKeyname   = "HKEY_CURRENT_USER\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "UserContent"
    Local $sRegSZ     = "REG_SZ"
    Local $sString    = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"

    RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    If @error Then
        SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS NOT successfully added!", 800, 50, -1, -1)
        Sleep(2000)
        SplashOff()
        Return
    EndIf

    SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS successfully added!", 800, 50, -1, -1)
    Sleep(2000)
    SplashOff()
EndFunc

No necessity for if-else when you simple can return early. This concept avoid nesting and potential error prone code can not be entered because the code returns early.

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

Sven,
Thanks for this....appreciated!

However, as with the previous example, may I ask what would "trigger"..."If @error then"...?

Considering this query from another "angle", what Local variable would trigger the error?

At this point, I see that this error check does absolutely nothing!!

Posted

Okay,

I guess..."I kinda fig'urd it out myslf!" Check this out:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

AddRegKeyHKCUValue()

Func AddRegKeyHKCUValue()
    Local $sKeyname = "" ; <====A null value will trigger an error!
    Local $sValuename = "UserContent"
    Local $sRegSZ = "REG_SZ"
    Local $sString = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"

    RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    If @error Then
        SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS NOT successfully added!", 800, 50, -1, -1)
        Sleep(2000)
        SplashOff()
        Return
    EndIf

    SplashTextOn("NOTICE!!", "The HKCU valuename and string WAS successfully added!", 800, 50, -1, -1)
    Sleep(2000)
    SplashOff()
EndFunc   ;==>AddRegKeyHKCUValue
Posted

The following would show you the reason for the error (based on RegWrite return errors)
 

AddRegKeyHKCUValue()

Func AddRegKeyHKCUValue()
    Local $sKeyname = "" ; <====A null value will trigger an error!
    Local $sValuename = "UserContent"
    Local $sRegSZ = "REG_SZ"
    Local $sString = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"
    RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    Switch @error
        Case 0
            $sError = "The HKCU valuename and string WAS successfully added!"
        Case 1
            $sError = "Unable to open requested key"
        Case 2
            $sError = "Unable to open requested main key"
        Case 3
            $sError = "Unable to remote connect to the registry"
        Case -1
            $sError = "Unable to open requested value"
        Case -2
            $sError = "Value type not supported"
    EndSwitch
    SplashTextOn("NOTICE!!", $sError, 800, 50, -1, -1)
    Sleep(2000)
    SplashOff()
EndFunc

 

Posted (edited)

Sven,

Thanks again! And though my original query was not directly resolved, due to your input, I can still mark this post as "Solved!!"

Here is my final example:

; -----------------------------------------------
Opt("MustDeclareVars", 1)
; -----------------------------------------------
AddRegKeyHKCUValue()
; -----------------------------------------------
Func AddRegKeyHKCUValue()
    Local $sKeyname = "HKEY_CURRENT_USER\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "UserContent"
    Local $sRegSZ = "REG_SZ"
    Local $sString = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"
    ; -----------------------------------------------
    Local $iHeight = 135
    Local $sMessage = "Add HKCU data..." & @CRLF & @CRLF
    SplashTextOn("NOTICE!!", $sMessage, 450, $iHeight, -1, -1, 5, "FuturaBQ-DemiBold", 14)
    Sleep(1000)
    ; -----------------------------------------------
    Local $sResult = RegWrite($sKeyname, $sValuename, $sRegSZ, $sString)
    ; -----------------------------------------------
    Switch $sResult
        Case 0
            $sResult = "The HKCU data WAS NOT successfully added!"
        Case 1
            $sResult = "The HKCU data WAS successfully added!"
    EndSwitch
    ; -----------------------------------------------
    $sMessage &= $sResult & @CRLF
    ; -----------------------------------------------
    $sMessage &= @CRLF & "Add HKCU data is now completed..." & @CRLF
    ; -----------------------------------------------
    SplashTextOn("NOTICE!!", $sMessage, 450, $iHeight, -1, -1, 5, "FuturaBQ-DemiBold", 14)
    Sleep(2000)
    SplashOff()
EndFunc   ;==>AddRegKeyHKCUValue

So, "Thank you, Sven"...for both your time and attention to the above. Both are very much appreciated!

Edited by mr-es335
Update to Switch|Case
Posted

How about this little function?

  • Added a function header describing every aspect of the function so you can modify/debug the function even many years after you have written the code.
  • Added the name of the program to be displayed in error messages, so users know where the message came from. I hate anonymous error messages!
  • Added an array holding the full text of possible errors. Makes debugging much easier.
  • Removed $sRegSZ = "REG_SZ" as this is only used once and this literal is required by RegWrite and will not change in the near future.
  • Replaced Splashtext by MsgBox. When there is an error I like to get easy readable/understandable information. No eye candy.

In conclusion: I wouldn't write a separate function here, but carry out the error check directly in the programme. A user function isn't worth the effort!

#include <MsgBoxConstants.au3>

Opt("MustDeclareVars", 1)
Global $sProgramName = "Registry Testprogram"
Global $sRegWriteErrors[] = ["value type not supported", "unable to open requested value", "", "unable to open requested key", "unable to open requested main key", "unable to remote connect to the registry"]

AddRegKeyHKCUValue()
Exit

; #FUNCTION# ====================================================================================================================
; Name...........: AddRegKeyHKCUValue
; Description ...: Writes a success message or an error message with detailed debug information for RegWrite.
; Syntax.........: AddRegKeyHKCUValue()
; Parameters ....: None
; Return values .: None
; Author ........: Jon Doe
; Remarks .......:
; Related .......: 
; Link ..........: 
; ===============================================================================================================================
Func AddRegKeyHKCUValue()
    Local $sKeyname = "HKEY_CURRENT_USER\Software\Native Instruments\Guitar Rig 5"
    Local $sValuename = "UserContent"
    Local $sString = "C:\Program Files\Native Instruments\Reflektor\16 User_Impulses\"
    If RegWrite($sKeyname, $sValuename, "REG_SZ", $sString) = 1 Then
        MsgBox($MB_ICONINFORMATION, $sProgramName, "RegWrite was SUCCESSFUL!", 2)
    Else
        MsgBox($MB_ICONERROR, $sProgramName, "RegWrite has returned ERROR " & @error & "!" & @CRLF & $sRegWriteErrors[@error + 2] & "!")
    EndIf
EndFunc   ;==>AddRegKeyHKCUValue

 

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

 

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
  • Recently Browsing   0 members

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