Jump to content

[SOLVED] Return value to running func?


 Share

Recommended Posts

How do i return a value to a running function?

The following example (not a real code) shows a function that checks for a need for password input. If needed, it will open a gui password input. If the password is correctly inserted then it should return the 'uneeded password input' value and continue the previous function.

Did i complicate to much!? Its because i'm sleepy. :)

How can i solve this?

CheckForPassword()
RunFunc()

Func CheckForPassword()
If $password=1 Then
$Locked=1
ElseIf $password=0 Then
$Locked=0
EndIf
EndFunc

Func RunFunc()
CheckForPassword()
If $Locked=1 Then
GUISetState(@SW_SHOW,$FormPass)
ElseIf $Locked=0 Then
DoSomething()
EndIf
EndFunc

Func SubmitPassButton()
If Guictrlread($PassInput)="Password" Then
Return $Locked=0 ; ??? NO? :)
EndFunc

My main problem is that i have tens of functions or else i would just call back the function RunFunc() after correct pass input...

Edited by telmob
Link to comment
Share on other sites

telmob,

Not sure what you are doing, but, assumming that $locked is "global" in scope this might work.

;
;

RunFunc()

Func RunFunc()
    If $Locked = 1 Then
        GUISetState(@SW_SHOW, $FormPass)
    ElseIf $Locked = 0 Then
        DoSomething()
    EndIf
EndFunc   ;==>RunFunc

Func SubmitPassButton()
    If GUICtrlRead($PassInput) = "Password" Then
        $Locked = 0
    Else
        $Locked = 1
    endif
EndFunc   ;==>SubmitPassButton

Not tested, of course.

kylomas

edit: even simpler example eliminating variables

RunFunc()

Func RunFunc()
    If SubmitPassButton() Then
        GUISetState(@SW_SHOW, $FormPass)
    Else
        DoSomething()
    EndIf
EndFunc   ;==>RunFunc

Func SubmitPassButton()
    If GUICtrlRead($PassInput) = "Password" Then
        return true
    Else
        return false
    endif
EndFunc   ;==>SubmitPassButton
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

telmob,

Not sure what you are doing, but, assumming that $locked is "global" in scope this might work.

RunFunc()

Func RunFunc()
    If $Locked = 1 Then
        GUISetState(@SW_SHOW, $FormPass)
    ElseIf $Locked = 0 Then
        DoSomething()
    EndIf
EndFunc ;==>RunFunc

Func SubmitPassButton()
    If GUICtrlRead($PassInput) = "Password" Then $Locked = 0
EndFunc ;==>SubmitPassButton

Not tested, of course.

kylomas

Thanks kylomas, that is basically what i'm doing, but i need to continue the previous function.

use a global variable or use a Return keyword:

Func blblablbla
.
.
.
return $result
EndFunc

That is exactly what i'm trying to achieve, but.....:

Func RunFunc()
CheckForPassword()
If $Locked=1 Then
GUISetState(@SW_SHOW,$FormPass)
ElseIf $Locked=0 Then
DoSomething()
EndIf
EndFunc

...... after i return the result, the function doesn't continue, and i need it to continue.

Link to comment
Share on other sites

telmob,

Does this not do what you want? (lsightly modified from above)

RunFunc()

Func RunFunc()
    If SubmitPassButton() Then
        GUISetState(@SW_SHOW, $FormPass)
        DoSomething()
    Else
        DoSomething()
    EndIf
EndFunc   ;==>RunFunc

Func SubmitPassButton()
    If GUICtrlRead($PassInput) = "Password" Then
        return true
    Else
        return false
    endif
EndFunc   ;==>SubmitPassButton

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Almost, i guess. :)

That assumes that i've already inserted the password in the $formpass input field.

I need it to show the $formpass first, insert the pass and only then return the result to the previous function and continue it.

Interesting aproach btw.

Edited by telmob
Link to comment
Share on other sites

telmob,

That assumes that i've already inserted the password in the $formpass input field.

No, it will return false if the psw does not match, including if it is blank. If you are saying that it does not fit within the flow of your script then you need to post more of the script as you are alluding to parts of the script that we cannot see...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Sorry i took so long to reply. I went to sleep.

It doesn't show $FormPass.

I immediately get the 'Wrong Password' message. And even after that, the GUI opens and it shouldn't.

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "On_DblClick")
Func On_DblClick()
If SubmitPass() Then
GUISetState(@SW_SHOW,$FormPass)
Else
Local $state = WinGetState("App", "System")
If WinExists("App", "System") Then
GUISetState(@SW_HIDE, $MAINGUI)
ElseIf Not WinExists("App", "System") Then
GUISetState(@SW_SHOW, $MAINGUI)
UpdateGUI()
EndIf
EndFunc

Func SubmitPass(); This function is the submit button in my input password gui.
Local $CheckPassExist=RegRead(*******************************)
Local $PassInputRead=Guictrlread($PassInput)
Local $FFF=_Crypt_HashData($PassInputRead,$CALG_MD5)
If _Crypt_HashData($PassInputRead,$CALG_MD5)=$CheckPassExist Then
GUISetState(@SW_HIDE,$FormPass)
GUICtrlSetData($PassInput, "")
Return True
ElseIf _Crypt_HashData($PassInputRead,$CALG_MD5)<>$CheckPassExist Then
MsgBox(16, "Error", "Wrong password!")
GUISetState(@SW_HIDE,$FormPass)
Return False
EndIf
EndFunc
Link to comment
Share on other sites

telmob,

Again

If you are saying that it does not fit within the flow of your script then you need to post more of the script as you are alluding to parts of the script that we cannot see...

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

I think you should do as he stated several times, show the code you're using in your script, right now we have tiny pieces that aren't connected to anything else and it's extremely hard to figure out what it is you're doing.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Here it is, finally.

Notice how it assumes i've already clicked the submit button when double-clicking the tray to open the gui.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIComboBox.au3>
#include <Constants.au3>
#include <GUIListview.au3>
#include <Array.au3>
#include <File.au3>
#include <Crypt.au3>

Opt("WinTitleMatchMode",2)
opt("TrayAutoPause",0)

TrayMenu("create")

$FormPasswidth = 280
$FormPassheight = 121
$FormPass = GUICreate("fghjfghjfgj", 280, 121, @DesktopWidth/2 - $FormPasswidth/2, @DesktopHeight/2 - $FormPassheight/2, BitOR($WS_SYSMENU, $GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$PassInput = GUICtrlCreateInput("", 8, 29, 257, 21, BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD))
$ButtonsubmitPass = GUICtrlCreateButton("&OK", 110, 58, 75, 28, $BS_NOTIFY)
$ButtonCancelPass = GUICtrlCreateButton("&Cancel", 191, 58, 75, 28, $BS_NOTIFY)
$EnterPassLabel = GUICtrlCreateLabel("Enter password:", 8, 12, 130, 17, 0)
#EndRegion ### END Koda GUI section ###
;~ GUISetState(@SW_HIDE)

$SRPEGUI = GUICreate("App", 200, 200, 500, 500, BitOR($WS_EX_ACCEPTFILES, $WS_EX_WINDOWEDGE, $GUI_SS_DEFAULT_GUI))
$ButtonLockSystem = GUICtrlCreateButton("Lock System", 54, 80, 91, 28)
GUISetState(@SW_HIDE)
;~ GUISetState()
TraySetClick(8)

TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "On_DblClick"); ----<-----<-----<-----<-----<-----<-----<-----<----
Func On_DblClick()
If SubmitPass() Then
GUISetState(@SW_SHOW,$FormPass)
Else
GUISetState(@SW_SHOW, $SRPEGUI)
EndIf
EndFunc

While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $ButtonsubmitPass
SubmitPass()
EndSwitch
WEnd

Func SubmitPass(); ----<-----<-----<-----<-----<-----<-----<-----<----
Local $Pass=1
Local $PassInputRead=Guictrlread($PassInput)
If $PassInputRead=$Pass Then
GUISetState(@SW_HIDE,$FormPass)
GUICtrlSetData($PassInput, "")
Return True
ElseIf $PassInputRead<>$Pass Then
MsgBox(16, "Error", "Wrong password!")
GUISetState(@SW_HIDE,$FormPass)
Return False
EndIf
EndFunc

func TrayMenu($tAction="create")
Opt("TrayMenuMode",11)
TrayItemSetOnEvent(-1,"TrayClick")

$t_settings=TrayCreateItem("Configure")
TrayItemSetOnEvent(-1,"TrayClick")
TrayCreateItem("")

$t_exit=TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"TrayClick")

Opt("TrayIconHide",0)
opt("TrayOnEventMode",1)

TraySetState()
endfunc

func TrayClick()
$t_msg=TrayItemGetText(@tray_id)
Select
Case $t_msg = "Configure"
Local $state = WinGetState("App", "")
If WinExists("App", "") Then
GUISetState(@SW_HIDE, $SRPEGUI)
ElseIf Not WinExists("App", "") Then
GUISetState(@SW_SHOW, $SRPEGUI)
EndIf
Case $t_msg = "Exit"
Exit
EndSelect
EndFunc
Edited by telmob
Link to comment
Share on other sites

Just a quick comment. I would change the elseif statements into simple else statements. I don't see any value in testing for more than the "true' condition and re-evaluating might cause undesired performance issues.

i.e.

ElseIf $PassInputRead<>$Pass Then

Else

ElseIf Not WinExists("App", "") Then

Else

Edited by spudw2k
Link to comment
Share on other sites

In some cases you'd prob want to evaluate multiple conditions, but in others (like checking for a correct password) it's only important to eval if it's true because any other condition would be false.

I wanted to throw out my comment when I saw in your earlier posting where you used if _Crypt... elseif _Crypt...

Link to comment
Share on other sites

telmob,

Ran your reproducer. Clicking on the "configure" option does'nt do anything. Looked at your code and am not sure what you are trying to do. My impression is that you want a tray menu to do various things after pasword validation. Is this correct?

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

No, the 'configure' option is just to open the gui, but i've included it in the reproducer above just for completeness sake.

The only important example in the code above is in the double click tray action which also just opens/closes the gui but it has the code you've introduced.

I've put some '<-----<-----<' in the code for you to find it easily. If the code works for that function, i will implement it in the other important functions that require a password to get accessed.

Edited by telmob
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...