Sign in to follow this  
Followers 0
Meerecat

Using functions more than once

6 posts in this topic

Hello

I am new to AutoIT and so apologise for what is a very basic question. Can I have a global function, in the same way I can a global variable?

For example, I have a function:

Func _ChangePW()
    If $new=0 Then
        $bLoop=1
        While $bLoop=1
            $text=InputBox("Folder Lock","Please type your password and click OK.","","*")
            If @error=1 Then Exit
            If $text<>IniRead($ini,"Password","Password","failed") Then
                MsgBox(4096,"Error","Incorrect Password - try again!")
            Else
                $bLoop=0    
            EndIf
        WEnd
    EndIf
    $pw1=InputBox("Folder Lock","Please create a new password and click OK.","","*")
    If @error=1 Then Exit
    $pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
    If @error=1 Then Exit
    While $pw1<>$pw2
        $pw1=InputBox("Folder Lock","Passwords do not match or are blank. Please try again.","","*")
        If @error=1 Then Exit
        $pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
        If @error=1 Then Exit
        If $pw2="" Then $pw2="ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini,"Password","Password",$pw2)
    MsgBox(0+64,"Folder Lock","Your new password has been set.")
EndFunc

If I call this function using

GUICtrlSetOnEvent($chgpass,"_ChangePW")
before the function is declared it works. However if I try to call the function again in the same way, later in the script, it does not work.

Does that make sense?

Many Thanks

M


Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites



Does that make sense?

It does not make sense, because the main reason for using functions is to be able to re-use the code, rather than having identical code in different places of your script.

The reason it might not be working for you could be one, or more of the following:

* The part of the code calling the function is never reached.

* The part of the code setting the event that triggers the function is never reached.

* The event that should trigger the function is never triggered.

* The event that should trigger the function is triggered, but the script is stuck in another event-triggered function and as such never gets to handling the next event.

* The variables used by the functions are global, and do not have the proper initial values at the point you call the function for the second time.

That's all I can think of for now. If you'd post an example that demonstrates the problem (something we can run the way you post it) we'd be able to give more concrete advice.

Share this post


Link to post
Share on other sites

* The event that should trigger the function is triggered, but the script is stuck in another event-triggered function and as such never gets to handling the next event

I think this may be the problem. Many thanks, if I can't work it out I'll post my code.

Thanks again

M


Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Ok I have included a sample of my code below.

Exit works in the first dialogue, but if you press Advanced Options it does not work on the 2nd dialogue.

Can anubody tell me why?

Many thanks in advance

M

#include <GUIConstantsEx.au3>

 Global $ini=@AppDataDir & "\Meerecat\FolderLock\FldLock.ini" 
 Global $dir="c:\confidential"

If Not FileExists($ini) Then
    $hnd=FileOpen($ini,9) 
    FileWrite($hnd,"[Password]" & @CRLF)
    FileWriteLine($hnd,"Password=")
    FileClose($hnd)
    MsgBox(0+64,"Meerecat Folder Lock","This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
    $new=1
    _ChangePW()
EndIf
If Not FileExists($dir) Then
    DirCreate ($dir)
    MsgBox(1,"Meerecat Folder Lock","Your confidential folder has been created:"& @LF & @LF & "c:\confidential")
EndIf
    $bLoop=1
    While $bLoop=1
        $text=InputBox("Meerecat Folder Lock","Please type your password and click OK.","","*")
        If @error=1 Then Exit
        ; They clicked OK, but did they type the right thing?
        If $text<>IniRead($ini,"Password","Password","failed") Then
            MsgBox(4096,"Error","Incorrect Password - try again!")
        Else
            $bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
        EndIf
    WEnd
$new=0

func _main()
Opt("GUIOnEventMode",1)
$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
GUICtrlCreateLabel("Welcome to Meerecat Folder Lock", 208, 24, 246, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreateLabel("Please choose an option below", 216, 48, 223, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
GUISetBkColor(0xFFFFFF)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
$lock= GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
$unlock= GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
$advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
$exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
GUICtrlSetOnEvent($lock,"_Lock")
GUICtrlSetOnEvent($unlock,"_Unlock")
GUICtrlSetOnEvent($advanced,"_Advanced")
GUICtrlSetOnEvent($exit,"_Exit")
GUISetState(@SW_SHOW)
While 1
    Sleep(1000)
WEnd
EndFunc



Func _Advanced()
    Opt("GUIOnEventMode",1)
$prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
GUICtrlCreateLabel("Meerecat Folder Lock - Advanced Options", 208, 24, 246, 24)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
GUISetBkColor(0xFFFFFF)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
$lock= GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
$unlock= GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
$advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
$exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
GUICtrlSetOnEvent($lock,"_Lock")
GUICtrlSetOnEvent($unlock,"_Unlock")
GUICtrlSetOnEvent($advanced,"_Advanced")
GUICtrlSetOnEvent($exit,"_Exit")
GUISetState(@SW_SHOW)
While 1
    Sleep(1000)
WEnd
EndFunc

Func _Exit()
        Exit
EndFunc

Func _ChangePW()
    If $new=0 Then
        $bLoop=1
        While $bLoop=1
            $text=InputBox("Folder Lock","Please type your password and click OK.","","*")
            If @error=1 Then Exit
            ; They clicked OK, but did they type the right thing?
            If $text<>IniRead($ini,"Password","Password","failed") Then
                MsgBox(4096,"Error","Incorrect Password - try again!")
            Else
                $bLoop=0    ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1=InputBox("Folder Lock","Please create a new password and click OK.","","*")
    If @error=1 Then Exit
    $pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
    If @error=1 Then Exit
    While $pw1<>$pw2
        $pw1=InputBox("Folder Lock","Passwords do not match or are blank. Please try again.","","*")
        If @error=1 Then Exit
        $pw2=InputBox("Folder Lock","Please confirm your new password and click OK.","","*")
        If @error=1 Then Exit
        If $pw2="" Then $pw2="ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini,"Password","Password",$pw2)
    MsgBox(0+64,"Folder Lock","Your new password has been set.")
EndFunc

_main()
Edited by Meerecat

Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
Share on other sites

Untested, commented changes.

#include <GUIConstantsEx.au3>

Global $ini = @AppDataDir & "\Meerecat\FolderLock\FldLock.ini"
Global $dir = "c:\confidential"
Opt("GUIOnEventMode", 1); =========================================================option only needs to be here
If Not FileExists($ini) Then
    $hnd = FileOpen($ini, 9)
    FileWrite($hnd, "[Password]" & @CRLF)
    FileWriteLine($hnd, "Password=")
    FileClose($hnd)
    MsgBox(0 + 64, "Meerecat Folder Lock", "This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
    $new = 1
    _ChangePW()
EndIf
If Not FileExists($dir) Then
    DirCreate($dir)
    MsgBox(1, "Meerecat Folder Lock", "Your confidential folder has been created:" & @LF & @LF & "c:\confidential")
EndIf
$bLoop = 1
While $bLoop = 1
    $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
    If @error = 1 Then Exit
    ; They clicked OK, but did they type the right thing?
    If $text <> IniRead($ini, "Password", "Password", "failed") Then
        MsgBox(4096, "Error", "Incorrect Password - try again!")
    Else
        $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
    EndIf
WEnd
$new = 0

_main(); Moved this, autoit3 checks all functions before anything else, (they are declared first noi matter where they are)

While 1
    Sleep(1000) ; =======================================================================while loop here.
WEnd

Func _main()
    ; ==================================================================================== removed option
    $prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
    GUICtrlCreateLabel("Welcome to Meerecat Folder Lock", 208, 24, 246, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Please choose an option below", 216, 48, 223, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFFFFFF)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
    GUICtrlSetOnEvent($lock, "_Lock")
    GUICtrlSetOnEvent($unlock, "_Unlock")
    GUICtrlSetOnEvent($advanced, "_Advanced")
    GUICtrlSetOnEvent($exit, "_Exit")
    GUISetState(@SW_SHOW)
    ;==================================================================================== moved while loop
EndFunc   ;==>_main



Func _Advanced()
    ; ==================================================================================== removed option
    $prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
    GUICtrlCreateLabel("Meerecat Folder Lock - Advanced Options", 208, 24, 246, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFFFFFF)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
    GUICtrlSetOnEvent($lock, "_Lock")
    GUICtrlSetOnEvent($unlock, "_Unlock")
    GUICtrlSetOnEvent($advanced, "_Advanced")
    GUICtrlSetOnEvent($exit, "_Exit")
    GUISetState(@SW_SHOW)
    ;============================================== Removed while loop
EndFunc   ;==>_Advanced

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then Exit
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then Exit
        $pw2 = InputBox("Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then Exit
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Folder Lock", "Your new password has been set.")
EndFunc   ;==>_ChangePW

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Untested, commented changes.

#include <GUIConstantsEx.au3>

Global $ini = @AppDataDir & "\Meerecat\FolderLock\FldLock.ini"
Global $dir = "c:\confidential"
Opt("GUIOnEventMode", 1); =========================================================option only needs to be here
If Not FileExists($ini) Then
    $hnd = FileOpen($ini, 9)
    FileWrite($hnd, "[Password]" & @CRLF)
    FileWriteLine($hnd, "Password=")
    FileClose($hnd)
    MsgBox(0 + 64, "Meerecat Folder Lock", "This is your first time running Folder Lock on this PC" & @LF & @LF & "You will now be prompted to create a password.")
    $new = 1
    _ChangePW()
EndIf
If Not FileExists($dir) Then
    DirCreate($dir)
    MsgBox(1, "Meerecat Folder Lock", "Your confidential folder has been created:" & @LF & @LF & "c:\confidential")
EndIf
$bLoop = 1
While $bLoop = 1
    $text = InputBox("Meerecat Folder Lock", "Please type your password and click OK.", "", "*")
    If @error = 1 Then Exit
    ; They clicked OK, but did they type the right thing?
    If $text <> IniRead($ini, "Password", "Password", "failed") Then
        MsgBox(4096, "Error", "Incorrect Password - try again!")
    Else
        $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
    EndIf
WEnd
$new = 0

_main(); Moved this, autoit3 checks all functions before anything else, (they are declared first noi matter where they are)

While 1
    Sleep(1000) ; =======================================================================while loop here.
WEnd

Func _main()
    ; ==================================================================================== removed option
    $prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
    GUICtrlCreateLabel("Welcome to Meerecat Folder Lock", 208, 24, 246, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreateLabel("Please choose an option below", 216, 48, 223, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFFFFFF)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
    GUICtrlSetOnEvent($lock, "_Lock")
    GUICtrlSetOnEvent($unlock, "_Unlock")
    GUICtrlSetOnEvent($advanced, "_Advanced")
    GUICtrlSetOnEvent($exit, "_Exit")
    GUISetState(@SW_SHOW)
    ;==================================================================================== moved while loop
EndFunc   ;==>_main



Func _Advanced()
    ; ==================================================================================== removed option
    $prompt = GUICreate("Meerecat Folder Lock", 581, 222, 192, 124)
    GUICtrlCreateLabel("Copyright 2011 Meerecat Simple Software", 152, 200, 202, 17)
    GUICtrlCreateLabel("Meerecat Folder Lock - Advanced Options", 208, 24, 246, 24)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlCreatePic("C:\Users\Sienna X\Desktop\AutoIT Scripts\meerekat.jpg", 0, 0, 137, 217)
    GUISetBkColor(0xFFFFFF)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    $lock = GUICtrlCreateButton("Lock Folder", 152, 112, 120, 33)
    $unlock = GUICtrlCreateButton("Unlock Folder", 296, 112, 120, 33)
    $advanced = GUICtrlCreateButton("Advanced Options", 440, 112, 120, 33)
    $exit = GUICtrlCreateButton("Exit Program", 440, 168, 120, 33)
    GUICtrlSetOnEvent($lock, "_Lock")
    GUICtrlSetOnEvent($unlock, "_Unlock")
    GUICtrlSetOnEvent($advanced, "_Advanced")
    GUICtrlSetOnEvent($exit, "_Exit")
    GUISetState(@SW_SHOW)
    ;============================================== Removed while loop
EndFunc   ;==>_Advanced

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func _ChangePW()
    If $new = 0 Then
        $bLoop = 1
        While $bLoop = 1
            $text = InputBox("Folder Lock", "Please type your password and click OK.", "", "*")
            If @error = 1 Then Exit
            ; They clicked OK, but did they type the right thing?
            If $text <> IniRead($ini, "Password", "Password", "failed") Then
                MsgBox(4096, "Error", "Incorrect Password - try again!")
            Else
                $bLoop = 0 ; Exit the loop - ExitLoop would have been an alternative too :)
            EndIf
        WEnd
    EndIf
    $pw1 = InputBox("Folder Lock", "Please create a new password and click OK.", "", "*")
    If @error = 1 Then Exit
    $pw2 = InputBox("Folder Lock", "Please confirm your new password and click OK.", "", "*")
    If @error = 1 Then Exit
    While $pw1 <> $pw2
        $pw1 = InputBox("Folder Lock", "Passwords do not match or are blank. Please try again.", "", "*")
        If @error = 1 Then Exit
        $pw2 = InputBox("Folder Lock", "Please confirm your new password and click OK.", "", "*")
        If @error = 1 Then Exit
        If $pw2 = "" Then $pw2 = "ThisPreventsBlankPasswords"
    WEnd
    IniWrite($ini, "Password", "Password", $pw2)
    MsgBox(0 + 64, "Folder Lock", "Your new password has been set.")
EndFunc   ;==>_ChangePW

That's perfect. Many thanks for the changes, I can now expand on that.

Also thank you for commenting the changes you made, I find this really useful for spotting my mistakes.

M


Lack of planning on your part does not constitute an emergency on my part.-The biggest idiot can ask questions the smartest man cannot answer.

Share this post


Link to post
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
Sign in to follow this  
Followers 0