Jump to content

Use a part from a function in function


Mecano
 Share

Recommended Posts

Dear forum members,

Filepath, app name and app version in one funtion, which can be use separately, is this possible?

example

_MyFunction($MyAppPath, $sMyApp, $sMyAppVer); separated to call

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $sMyProgDir
Opt("MustDeclareVars", 1)

Global $ProgramFilesDir = EnvGet('ProgramFiles(x86)') ; for 64bit Win it will return a valid path.
If Not $ProgramFilesDir Then $ProgramFilesDir = @ProgramFilesDir ; for 32bit Win this will "repair" the broken return from above.

Local $Gui = GUICreate("My Program info", 343, 197, 192, 124)
Local $Button1 = GUICtrlCreateButton("show", 91, 62, 161, 73)
GUISetState(@SW_SHOW)

While 1
    Local $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            MsgBox(4096, "My Program overview", Overview()) ; just for testing purposes
    EndSwitch
WEnd

Func Overview()
    Local $sInfo = _
            "OS architecture: " & @OSArch & @CRLF & _
            "OS Build: " & @OSBuild & @CRLF & _
            "My Program directory: " & MyProgDir() & @CRLF & _
            "My Program version: " & MyProgVersion()
    Return $sInfo
EndFunc   ;==>Overview

; Func _MyFunction($MyAppPath, $sMyApp, $sMyAppVer); separate to call
Func MyProgDir()
    Local $sRegKey, $iKey, $sHold, $sMyProgRegKey
    If @OSArch = "X64" Then
        ;x64 Key
        $sRegKey = "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
    Else
        ;x86 Key
        $sRegKey = "HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    EndIf
    $iKey = 1
    While 1
        $sHold = RegEnumKey($sRegKey, $iKey)
        If @error Then ExitLoop
        $iKey += 1
        If RegRead($sRegKey & $sHold, "DisplayName") = 'My Program' Then
            $sMyProgRegKey = RegRead($sRegKey & $sHold, "InstallLocation")
            If Not @error Then
                MsgBox(0, $sHold, $sMyProgRegKey) ;debug
                ExitLoop
            EndIf
        EndIf
    WEnd
    If FileExists($sMyProgRegKey) Then
        $sMyProgDir = StringTrimRight($sMyProgRegKey, 1)
        MsgBox(0, "Found through REGKey", $sMyProgDir) ;installed with a installer
    ElseIf FileExists($ProgramFilesDir & "\My Program") Then
        MsgBox(0, "Found through ProgramFiles Dir", $ProgramFilesDir) ;maybe it's installed with 7-zip, no regkey found
        $sMyProgDir = $ProgramFilesDir & "\My Program"
    ElseIf FileExists(@ScriptDir & "\MyProg.exe") Then
        MsgBox(0, "Found through Script Dir", @ScriptDir) ;maybe it's portable
        $sMyProgDir = @ScriptDir & "\"
    Else
        MsgBox(0, "Not one is true", "What are doing?") ;debug
        $sMyProgDir = False
    EndIf
    ;MsgBox(,"installed:",$sMyProgDir) ; installed location
    Return $sMyProgDir

    ;====================== My Program version =================
;~  If $sMyProgDir = False Then
;~      Return "My Program, not installed"
;~  Else
;~      Local $sMyApp = $sMyProgDir & "\MyProg.exe"
;~      If FileExists($sMyApp) Then
;~          Local $sMyAppVer = StringTrimRight(FileGetVersion($sMyApp), 2)
;~          Return $sMyProgDir & @CRLF & $sMyApp & @CRLF & $sMyAppVer
;~      Else
;~          Return "MyProg.exe not found"
;~      EndIf
;~  EndIf
    ;===========================================================

EndFunc   ;==>_MyProgDir

; separate function for version info

Func MyProgVersion()
    Call("MyProgDir")
    If $sMyProgDir = False Then
        Return "My Program, not installed"
    Else
        Local $sMyApp = $sMyProgDir & "\MyProg.exe"
        If FileExists($sMyApp) Then
            Local $sMyAppVer = StringTrimRight(FileGetVersion($sMyApp), 2)
            Return $sMyAppVer
        Else
            Return "MyProg.exe not found"
        EndIf
    EndIf
EndFunc   ;==>_MyProgVersion

Greetings from Holland

Link to comment
Share on other sites

@wayfarer, do you mean is individual values? I see in your script you placed their values in one variable so I assume that is the case. 

My method may be a bit odd and I don't mind being told so but you could return the values you get within your function using an array.

#include <array.au3>

$arrSysInfo = Overview()
_ArrayDisplay($arrSysInfo)
Func Overview()
    Local $aSysInfo[4]
    $aSysInfo[0] = @OSArch
    $aSysInfo[1] = @OSBuild
    $aSysInfo[2] = MyProgDir()
    $aSysInfo[3] = MyProgVersion()
    Return($aSysInfo)
EndFunc

I found this handy when I collect a number of details related to a specific program, (paths, registry value, etc) and want them available to other functions. If this is not what you are looking for, perhaps it gives you an idea?

Link to comment
Share on other sites

gritts, good idea arrays

But what I mean something like this _MyFunction($MyAppPath, $sMyApp, $sMyAppVer)

Let say I make another function, and I want to have some returns from MyProgDir()

instead of Call("MyProgDir") I use ($MyAppPath, $sMyApp) it's called ByRef keyword I think

Another simple question about parentheses?

You have used Return($aSysInfo) with parentheses and not Return $aSysInfo is there rule for it?

thnx

 

 

Link to comment
Share on other sites

gritts, good idea arrays

But what I mean something like this _MyFunction($MyAppPath, $sMyApp, $sMyAppVer)

Let say I make another function, and I want to have some returns from MyProgDir()

instead of Call("MyProgDir") I use ($MyAppPath, $sMyApp) it's called ByRef keyword I think

Another simple question about parentheses?

You have used Return($aSysInfo) with parentheses and not Return $aSysInfo is there rule for it?

thnx

 

@Mecano, I have to admit, I am not that versed on ByRef and would have to play with its use to understand it further.

I have not used Call in the past so bear with me  :ermm: . I created an example that might answer your question (as well as "edumicate" me). Let me know if this helps...

_MyComputer()

Func _MyCompName($cmpName)
    ConsoleWrite("Computer name: "&$cmpName&@CRLF)
EndFunc

Func _MyComspec($cmspec)
    ConsoleWrite("Comspec value: "&$cmspec&@CRLF)
EndFunc

Func _IReturnStuff($stuffHere)
    Local $retStuff
    $retStuff = "The length of the string you passed is "&StringLen($stuffHere)&" characters."&@CRLF
    Return $retStuff
EndFunc

Func _MyComputer()
    Local $retVal
    Call(_MyCompName,@ComputerName)
    Call(_MyComspec,@ComSpec)
    Call(_MyCompName,"My random stuff")
    $retVal = Call(_IReturnStuff,"My string of words")
    ConsoleWrite(Call(_IReturnStuff,"My string of words"))
    ConsoleWrite("Deja-vu: "&$retVal)
    Call(_MyCompName,Call(_IReturnStuff,"My string of words")) ;Just to make my head spin
EndFunc

As to the Return with the parentheses, I use them out of habit. So far it does not seem to have caused issues in the apps I have developed. I read the help entry for Return (several times over as the caffeine has not yet kicked in) and I may need to change how some of my apps are written. ByRef looks interesting. Perhaps someone can provide another "for instance" to shed more lite on ByRef and Return?

Link to comment
Share on other sites

@gritts, I'm still learning, practicing  AutoIt, (kind off solving puzzles).
If there was a lynda.com AutoIt video tutorial, I would definitely buy it  :ILA:>
Nice example you wrote,  going to study this  :thumbsup:
helps me a lot and thanks for explaining Return with the parentheses
 

ByRef looks interesting. Perhaps someone can provide another "for instance" to shed more lite on ByRef and Return?

I hope, practicing and learning won't harm, keeps your brain in top condition

Again thanks for the answers

Edit:

This is a example what I mean

_ShowBytes($filesize)

instead of call("_ShowBytes")

Func MEM()
    Local $mem = MemGetStats()
    Local $filesize = $mem[1]
    Return "Installed memory: " & _ShowBytes($filesize)
EndFunc


Func _ShowBytes($kbbytes)
    Local $x, $bytes_suffix[5] = [" KB", " MB", " GB", " TB", " PB"]
    While $kbbytes > 1023
        $x += 1
        $kbbytes /= 1024
    WEnd
    Return Round($kbbytes) & $bytes_suffix[$x]
EndFunc
Edited by Mecano
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...