Jump to content

Cannot define default string value in Func()?


Recommended Posts

Good evening night coders!

I just stumbled on an unexpected situation here. 

I'm converting the registry path of 7-zip into a usable path here and I want to send a generic message if the path can't be found.

I figured I'd be smart and create a function to handle the message and bake in a default value to it. If I use the default message, then it comes out as a blank string. I was not expecting that. 

Is it by design?

Here's my code (it's part of a bigger program)

Func _7zip() ; Returns path and filename to 7-zip or shoots error
    Local $sReg
    If @OSArch = "X86" Then ; Windows 32 bits
        $sReg = RegRead("HKLM\SOFTWARE\7-Zip", "Path") ; Read registry value
        If @error Then ; Not found!
            _7zip_WEB("") ; This function will write a generic message and invite user to visite https://7zip.org
        EndIf
    Else ; It's a 64 bit OS
        $sReg = RegRead("HKLM64\SOFTWARE\7-Zip", "Path") ; Read registry value
        _7zip_WEB("")     ; This function will write a generic message and invite user to visite https://7zip.org
    EndIf

    $sReg &= "7z.exe" ; Append executable filename

    $sReg = FileGetShortName($sReg) ; Get short name so you don't need quotes to executable on the command line
    If @error Then ; 7z.exe cannot be found or another error
        _7zip_WEB("Cannot find 7z.exe on this computer.") ; This function will write a generic message and invite user to visite https://7zip.org
    EndIf

    Return $sReg ; Return proper path and filename
EndFunc   ;==>_7zip

Func _7zip_WEB($sMess = "Cannot find 7zip on this computer.") ; Generic 7-zip missing message
    Local $i

    $i = MsgBox(308, "Error _7zip()", $sMess & @CRLF & "Would you like to visit https://7-zip.org?")

    If $i = 6 Then RunWait(@ComSpec & " /c " & "start https://7-zip.org", "", @SW_MAXIMIZE)

    Exit 14 ; Exit program with error code 14
EndFunc   ;==>_7zip_WEB

When calling "_7zip_WEB() with an empty quote, I get a blank line in the resulting message box.

Since I wrote "$sMess = "Cannot find 7zip on this computer"" as one of the optional parameters I was expecting $sMess to pick up the default value.

I read the documentation really closely (and many times!) and there's no mention of any limitation to the default values --> https://www.autoitscript.com/autoit3/docs/keywords/Func.htm

Calling a message box as the first line of _7Zip_WEB() and printing out $sMess displays an empty string so something is happening at the AutoIt level

The rest works as expected.

Any input appreciated!

 

Help a newbie, comment your code!

Link to comment
Share on other sites

You're calling the function with:

_7zip_WEB("")

Which will return ""

To return the default use _7zip_WEB()

Also if you want to get the path just use RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe", "Path") you don't need to query both 32/64 bit hives.

Edited by Subz
Link to comment
Share on other sites

Hey thanks for the quick reply!

Will try _7zip_WEB() ... amazing if it works :)

For the registry key, I read a long time ago "SOFTWARE\7-zip" was the way to go. Oh well ... 

As long as it works

Note: There's a few mistakes in my code but I can't edit my posts for the moment. Will post the final code when I get everything fixed AND get the right to edit them :)

 

Help a newbie, comment your code!

Link to comment
Share on other sites

You can use both, also to open your url you could use ShellExecute, example:

Func _7zip() ; Returns path and filename to 7-zip or shoots error
    Local $sHKLM = @OSArch = "x64" ? "HKLM64" : "HKLM"
    Local $sReg = RegRead($sHKLM & "\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe", "Path")
    If @error Then
        $sReg = RegRead($sHKLM & "\SOFTWARE\7-Zip", "Path") ; Read registry value
        If @error Then ; Not found!
            _7zip_WEB() ; This function will write a generic message and invite user to visite https://7zip.org
        EndIf
    EndIf
    $sReg &= "7z.exe" ; Append executable filename
    $sReg = FileGetShortName($sReg) ; Get short name so you don't need quotes to executable on the command line
    If @error Then ; 7z.exe cannot be found or another error
        _7zip_WEB() ; This function will write a generic message and invite user to visite https://7zip.org
    EndIf

    Return $sReg ; Return proper path and filename
EndFunc   ;==>_7zip

Func _7zip_WEB($sMess = "Cannot find 7zip on this computer.") ; Generic 7-zip missing message
    Local $iMsgBox = MsgBox(308, "Error _7zip()", $sMess & @CRLF & "Would you like to visit https://7-zip.org?")
    If $iMsgBox = 6 Then ShellExecute("https://7-zip.org", "", "", "", @SW_MAXIMIZE)
    Exit 14 ; Exit program with error code 14
EndFunc   ;==>_7zip_WEB

 

Link to comment
Share on other sites

1 hour ago, obiwanceleri said:

There's a few mistakes in my code but I can't edit my posts for the moment.

As far as I know, you can't edit your posts as long as you still have the status "new member". 24 hours after registration you will get the status "member" which allows you to edit your posts (there might be also an additional minimum of 5 postings).

1 hour ago, obiwanceleri said:

Will try _7zip_WEB() ... amazing if it works :)

An optional parameter does not need to be specified, not even with "" (as @Subz has already described).
However, if your self-written function allows two or more optional parameters and you want to pass the second one, you can do it like this :

; use optional parameters as declared in the function header :
_ExampleWithMultipleOptParams()

; set first parameter :
_ExampleWithMultipleOptParams("set inidividual Param1")

; set second parameter (first parameter remains as default) :
_ExampleWithMultipleOptParams(Default, "set inidividual Param2")

Func _ExampleWithMultipleOptParams($sOptParam1 = "default Param1", $sOptParam2 = "default Param2")
    If $sOptParam1 = Default Then $sOptParam1 = "default Param1"
    If $sOptParam2 = Default Then $sOptParam2 = "default Param2"

    MsgBox(BitOR(4096, 64), "Optional Parameters :", _
                    "OptParam1 = " & $sOptParam1 & @CRLF & _
                    "OptParam2 = " & $sOptParam2 & @CRLF)
EndFunc   ;==>_ExampleWithMultipleOptParams

 

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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...