Jump to content

Script continues after selections.


Recommended Posts

I having a hard time will the script flow. The user is required to input specific information. I want the script to then continue and do the functions listed in the _Main function. The _Main function does call the _DiscRAS function but does not return to finish the remained of the steps. What would you recommend I use to make this occur??

CODE
#Include <GUIConstants.au3>

#include <Process.au3>

Opt('GUIOnEventMode','1')

HotKeySet("^x", "MyExit")

; ================================>

; Ver 0.90.1 Initial version

; Ver 0.90.2 disconnect only selected ras connection. Original script killed all ras sessions.

;

; To Do

; After successful connection determine the Server RAS ip address and telnet to it automatically

; vars

$my_ver = "0.90.2"

$my_port = ""

$login = ""

$passwd = ""

Global $Width = ('400')

Global $Height = ('200')

GUICreate("Connect Tool " & $my_ver, $Width, $Height)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

$Context_Menu = GUICtrlCreateContextMenu ()

$Item_1 = GUICtrlCreateMenuItem ("About", $Context_Menu)

GuiCtrlCreateMenuitem ("", $Context_Menu)

$Item_2 = GUICtrlCreateMenuItem ("Exit", $Context_Menu)

GUICtrlSetOnEvent ($Item_2, "_Exit")

GUICtrlCreateLabel ("PORT :",60,154)

$Input_1 = GUICtrlCreateInput ("",104,152,25,20)

GUICtrlSetLimit ($Input_1, '2','2')

GUICtrlSetState ($Input_1, $GUI_DISABLE)

GUICtrlCreateGroup ("",45,20,100,115)

$Radio_1 = GUICtrlCreateRadio("C1", 75, 40, 45)

GUICtrlSetOnEvent ($Radio_1, "_C1")

$Radio_2 = GUICtrlCreateRadio("C2", 75, 60, 45)

GUICtrlSetOnEvent ($Radio_2, "_C2")

$Radio_3 = GUICtrlCreateRadio("C3", 75, 80, 45)

GUICtrlSetOnEvent ($Radio_3, "_C3")

$Radio_4 = GUICtrlCreateRadio("C4", 75, 100, 45)

GUICtrlSetOnEvent ($Radio_4, "_C4")

GUICtrlCreateLabel ("Telephone Number",238,5)

$Input_3 = GUICtrlCreateInput ("",210,20,150,20,$ES_NUMBER)

GUICtrlSetLimit ($Input_3, '23','23')

GUICtrlCreateLabel ("RAS Login",255,45)

$Input_4 = GUICtrlCreateInput ("",210,60,150,20)

GUICtrlSetLimit ($Input_4, '23','23')

GUICtrlCreateLabel ("RAS Password",245,85)

$Input_5 = GUICtrlCreateInput ("",210,100,150,20)

GUICtrlSetLimit ($Input_5, '23','23')

$Button_1 = GUICtrlCreateButton ("Dial RAS",295,170,60,25)

GUICtrlSetOnEvent ($Button_1, "_CheckSelections")

$Button_2 = GUICtrlCreateButton ("Show IP",215,170,60,25)

GUICtrlSetOnEvent ($Button_2, "_ShowIP")

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Sleep (250)

EndSelect

WEnd

Func _CheckSelections()

$my_port = GUICtrlRead($Input_1)

$login = GUICtrlRead($Input_4)

$passwd = GUICtrlRead($Input_5)

Select

Case GUICtrlRead($Input_1) = ""

MsgBox(0, "Notice - Error", "Please select a port and Retry", 0)

Case GUICtrlRead($Input_3) = ""

MsgBox(0, "Notice - Error", "Please enter a telephone number and Retry", 0)

Case GUICtrlRead($Input_4) = ""

MsgBox(0, "Notice - Error", "Please enter a RAS login and Retry", 0)

Case GUICtrlRead($Input_5) = ""

MsgBox(0, "Notice - Error", "Please enter a RAS password and Retry", 0)

EndSelect

; _Main()

EndFunc ;==>_CheckSelections

Func _Main()

_DiscRAS()

_CreateSCP()

_DialRAS()

;Do

; MsgBox(0, "Error", "Please select a port", 3)

;Until GUICtrlRead($Input_1) <> ""

EndFunc ;==> _Main

Func _C1 ()

GUICtrlSetData ($Input_1, 'C1')

EndFunc

Func _C2 ()

GUICtrlSetData ($Input_1, 'C2')

EndFunc

Func _C3 ()

GUICtrlSetData ($Input_1, 'C3')

EndFunc

Func _C4 ()

GUICtrlSetData ($Input_1, 'C4')

EndFunc

Func _DialRAS()

MsgBox(0, "Info", "You will call using port " & $my_port & " and will login using " & $login & " with a password of " & $passwd,4)

$RET = _RunDOS("rasdial " & GUICtrlRead($Input_1) & " /phone:" & GUICtrlRead($Input_3))

Switch $RET

Case 720

MsgBox(16, "RAS Error", "Login script error", 3)

Case 680

MsgBox(16, "RAS Error", "No dialtone", 3)

Case 678

MsgBox(16, "RAS Error", "There was no answer", 3)

Case 677

MsgBox(16, "RAS Error", "A person answered instead of a modem", 3)

Case 676

MsgBox(16, "RAS Error", "Line is busy", 3)

Case 0

MsgBox(32, "RAS Info", "The RAS connection was successful", 3)

Case Else

MsgBox(16, "RAS Error", "An unknown error has occured", 3)

EndSwitch

EndFunc ;==>_DialRAS

Func _CreateSCP()

$rasscript = "c:\windows\system32\ras\test.scp"

$script = "test.scp"

$rasscriptpath = "c:\windows\system32\ras\"

$file = FileOpen($rasscript, 10) ; Check if file is opened for writing successfully

If $file = -1 Then

MsgBox(0, "Error", "Unable to create the script file." & $file)

Exit

EndIf

FileWrite($file, " ; Connect Tool Version " & $my_Ver & @CRLF)

FileWrite($file, " ; Automated Logon Tool" & @CRLF)

FileWrite($file, "proc main" & @CRLF)

FileWrite($file, ' transmit "^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "login: " '& @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_4) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Password: " ' & @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_5) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Start PPP now!" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' transmit "/go:pppconnect^M" '& @CRLF)

FileWrite($file, "endproc" & @CRLF)

FileClose($file)

Exit

EndFunc ;==>_CreatSCP

Func _ShowIP()

; show ip addresses

MsgBox(0, "IP Info", " Your IP addresses are " & @IPAddress1 & " and " & @IPAddress2 & " and " & @IPAddress3 & " and " & @IPAddress4, 6)

EndFunc ;==>_ShowIP

Func _DiscRAS()

MsgBox(0, "Info", "I will disconnect " & $my_port, 4)

run("rasdial " & $my_port & " /disconnect")

EndFunc ;==>_DiscRAS

Func _TelnetRAS()

; Run("c:\windows\system32\telnet.exe")

; WinWaitActive("c:\windows\system32\telnet.exe")

; Sleep(3000)

; Replace xxx.xxx.xxx.xxx with the ip of your server

; Send("op xxx.xxx.xxx.xxx{ENTER}")

; WinWaitActive("Telnet 10.1.2.3")

EndFunc ;==>_TelnetRAS

Func MyExit()

MsgBox(4096,"", "Goodbye!")

Exit

EndFunc ;==>MyExit

Func _Exit ()

MsgBox(4096,"", "Goodbye!")

Exit

EndFunc ;==> Exit

Thanks

CC

Edited by IvanCodin
Link to comment
Share on other sites

I had left the _main() function in the script while I was testing. The new code does not have it included. I gather from you description I should not mix the message loop and event methods. I read the help file for both methods and neither seems to be an advantage over the ohter one. What is your preferred method?

What I am strugglin with is this ... I want the user to populate all of the selections. I then want to check them and provide the use a message if they didn't make entires. When I use the OnEvent mode for the selections I don't get the results I am looking for. Before the user enters the information I get the message box telling me the entry is empty.

I was using the _checkselections function to validate user input. It does give the empty feedback message box but then continues on. I only want the _checkselections to continue if I have entered the required fields.

Here is the the code I am referring to:

CODE
#Include <GUIConstants.au3>

#include <Process.au3>

Opt('GUIOnEventMode','1')

GUISetState(@SW_SHOW)

; ================================>

; Ver 0.90.1 Initial version

; Ver 0.90.2 disconnect only selected ras connection. Original code killed all ras connected sessions.

;

; To Do

; After successful connection determine the Server RAS ip address and telnet to it automatically

; vars

$my_ver = "0.90.2"

$my_port = ""

Global $Width = ('400')

Global $Height = ('200')

GUICreate("RAS Connect Tool " & $my_ver, $Width, $Height)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit')

$Context_Menu = GUICtrlCreateContextMenu ()

$Item_1 = GUICtrlCreateMenuItem ("About", $Context_Menu)

GuiCtrlCreateMenuitem ("", $Context_Menu)

$Item_2 = GUICtrlCreateMenuItem ("Exit", $Context_Menu)

GUICtrlSetOnEvent ($Item_2, "_Exit")

GUICtrlCreateLabel ("PORT :",60,154)

$Input_1 = GUICtrlCreateInput ("",104,152,25,20)

GUICtrlSetLimit ($Input_1, '2','2')

GUICtrlSetState ($Input_1, $GUI_DISABLE)

GUICtrlCreateGroup ("",45,20,100,115)

$Radio_1 = GUICtrlCreateRadio("C1", 75, 40, 45)

GUICtrlSetOnEvent ($Radio_1, "_C1")

$Radio_2 = GUICtrlCreateRadio("C2", 75, 60, 45)

GUICtrlSetOnEvent ($Radio_2, "_C2")

$Radio_3 = GUICtrlCreateRadio("C3", 75, 80, 45)

GUICtrlSetOnEvent ($Radio_3, "_C3")

$Radio_4 = GUICtrlCreateRadio("C4", 75, 100, 45)

GUICtrlSetOnEvent ($Radio_4, "_C4")

GUICtrlCreateLabel ("Telephone Number",238,5)

$Input_3 = GUICtrlCreateInput ("",210,20,150,20,$ES_NUMBER)

GUICtrlSetLimit ($Input_3, '23','23')

GUICtrlCreateLabel ("RAS Login",255,45)

$Input_4 = GUICtrlCreateInput ("",210,60,150,20)

GUICtrlSetLimit ($Input_4, '23','23')

GUICtrlCreateLabel ("RAS Password",245,85)

$Input_5 = GUICtrlCreateInput ("",210,100,150,20)

GUICtrlSetLimit ($Input_5, '23','23')

$Button_1 = GUICtrlCreateButton ("Dial RAS",295,170,60,25)

GUICtrlSetOnEvent ($Button_1, "_CheckSelections")

$Button_2 = GUICtrlCreateButton ("Show IP",215,170,60,25)

GUICtrlSetOnEvent ($Button_2, "_ShowIP")

GUISetState (@SW_SHOW)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Sleep (250)

EndSelect

WEnd

Func _CheckSelections()

$my_port = GUICtrlRead($Input_1)

$login = GUICtrlRead($Input_4)

$passwd = GUICtrlRead($Input_5)

If GUICtrlRead($Input_1) = "" or GUICtrlRead($Input_4) = "" or GUICtrlRead($Input_5) = "" Then MsgBox(0, "Notice - Error", "Please make entry and Retry", 0)

_CreateSCP()

EndFunc ;==>_CheckPort

Func _C1 ()

GUICtrlSetData ($Input_1, 'C1')

EndFunc

Func _C2 ()

GUICtrlSetData ($Input_1, 'C2')

EndFunc

Func _C3 ()

GUICtrlSetData ($Input_1, 'C3')

EndFunc

Func _C4 ()

GUICtrlSetData ($Input_1, 'C4')

EndFunc

Func _DialRAS()

$RET = _RunDOS("rasdial " & GUICtrlRead($Input_1) & " /phone:" & GUICtrlRead($Input_3))

Switch $RET

Case 720

MsgBox(16, "RAS Error", "Login script error", 3)

Case 680

MsgBox(16, "RAS Error", "No dialtone", 3)

Case 678

MsgBox(16, "RAS Error", "There was no answer", 3)

Case 677

MsgBox(16, "RAS Error", "A person answered instead of a modem", 3)

Case 676

MsgBox(16, "RAS Error", "Line is busy", 3)

Case 0

MsgBox(32, "RAS Info", "The RAS connection was successful", 3)

Case Else

MsgBox(16, "RAS Error", "An unknown error has occured", 3)

EndSwitch

_TelnetRAS()

EndFunc ;==>_DialRAS

Func _CreateSCP()

$script = "c:\windows\system32\ras\test.scp"

$rasscriptpath = "c:\windows\system32\ras\"

$file = FileOpen($script, 10) ; Check if file is opened for writing successfully

If $file = -1 Then

MsgBox(0, "Error", "Unable to create the script file." & $file)

Exit

EndIf

FileWrite($file, " ; Connect Tool Version " & $my_Ver & @CRLF)

FileWrite($file, " ; Automated Logon Tool" & @CRLF)

FileWrite($file, "proc main" & @CRLF)

FileWrite($file, ' transmit "^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "login: " '& @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_4) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Password: " ' & @CRLF)

FileWrite($file, ' transmit "' & GUICtrlRead($Input_5) &'^M" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' waitfor "Start PPP now!" '& @CRLF)

FileWrite($file, @CRLF)

FileWrite($file, ' transmit "/go:pppconnect^M" '& @CRLF)

FileWrite($file, "endproc" & @CRLF)

FileClose($file)

_DiscRAS()

; Exit

EndFunc ;==>_CreatSCP

Func _ShowIP()

; show ip addresses

MsgBox(0, "IP Info", " Your IP addresses are " & @IPAddress1 & " and " & @IPAddress2 & " and " & @IPAddress3 & " and " & @IPAddress4, 6)

EndFunc ;==>_ShowIP

Func _DiscRAS()

MsgBox(0, "Info", "I will disconnect " & $my_port, 6)

run("rasdial " & $my_port & " /disconnect")

_DialRAS()

EndFunc ;==>_DiscRAS

Func _TelnetRAS()

; Run("c:\windows\system32\telnet.exe")

; WinWaitActive("c:\windows\system32\telnet.exe")

; Sleep(3000)

; Replace XXXXX with the name of your server

; Send("op xxxx.xxxx.xxxx.xxxx{ENTER}")

; WinWaitActive("Telnet xxxx.xxxx.xxxx.xxxx")

MsgBox(0,"Info", "telnet will go here", 3)

EndFunc ;==>_TelnetRAS

Func GoodBye()

MsgBox(4096,"", "Goodbye!")

Exit

EndFunc ;==>GoodBye

Func _Exit ()

Exit

EndFunc ;==> Exit

You suggestions would be appreciated.

CC

Link to comment
Share on other sites

I prefer OnEvent mode because I think you get a cleaner and more structed source. It's also nice to be able to sleep the main function as long (or short) as you like :)

To exit a function in the middle of it just use Return, like this:

Func _CheckSelections()
$my_port = GUICtrlRead($Input_1)
$login = GUICtrlRead($Input_4)
$passwd = GUICtrlRead($Input_5)

If GUICtrlRead($Input_1) = "" or GUICtrlRead($Input_4) = "" or GUICtrlRead($Input_5) = ""  Then 
MsgBox(0, "Notice - Error", "Please make entry and Retry", 0)
Return
EndIf

_CreateSCP()
EndFunc ;==>_CheckPort

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Thanks for the feedback! I tried both methods and it now seems very obvious the advantages of the onevent method. In this script a function calls a function.. is this considerd a bad practice? I assume error checking should be handeled in the function it's self. Opinions?

CC

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