Jump to content

Dialling a connection


Recommended Posts

I have a script that dials RAS connection to connect to our company. If the RAS connection does not exist, it is created. I have used window manipulation code to achieve this. It works except it is susceptible to user intervention, and I dont want to use BlockInput to keep users from interfering.

Is there a better way to dial a connection and pass username/password to RAS server on the other side. If RAS has to be used, is there a better wasy to create a connectoid if it doesnt already exist?

Here is my code

CODE
Func Connect()

Dim $LastErr, $Items

TrayTip($tTip, "Preparing to dial...", 30, 17)

_FileWriteLog($logFile,"Starting connection")

If WinExists("Connecting " & $SrvName) Then ; already connecting

ElseIf WinExists("Error connecting " & $SrvName) Then ; connection error window > already connecting

Else

If WinExists("Network Connections") Then ; is Network Connections open?

Else

Run("RUNDLL32.EXE SHELL32.DLL,Control_RunDLL ncpa.cpl", "") ; open Network Connections folder

WinWait("Network Connections") ; wait for window to show

EndIf

;WinSetState("Network Connections", "", @SW_MINIMIZE) ; minimise window

;BlockInput(1) ; disable mouse & kbd input

For $Try = 1 To 5 ; try 5 times (10 secs)

TrayTip($tTip, "Searching " & $SrvName & "connectoid...", 30, 17)

WinActivate("Network Connections") ; activate window

If $Try > 1 And $Items = 0 Then ; after creating icon

TrayTip($tTip, "Waiting 2 seconds...", 30, 17) ; wait 5 sec - for slow systems

Sleep(2000) ; wait 2 secs after trying

EndIf

$Items = ControlListView("Network Connections", "", "SysListView321", "GetItemCount") ; count icons in folder

; if icon exists but is not named "DALISU iT", another will be created and named "DALISU iT"

If Not $Items Then ; no icons in Network Connections

CreateDalisu()

Else

WinActivate("Network Connections") ; activate window

$Conn = ControlListView("Network Connections", "", "SysListView321", "FindItem", $SrvName)

If $Conn = -1 Then ; DALISU iT icon not found

CreateDalisu()

Else ; DALISU iT icon exists

TrayTip($tTip, $SrvName & " connectoid found...", 30, 17)

ExitLoop

EndIf

EndIf

Next

If $Conn < 0 Then ; still no icon after 5 attempts

TrayTip($tTip, "Failed to create " & $SrvName & " icon...", 30, 19)

SetError(1)

BlockInput(0) ; enable mouse + kbd

Return

EndIf

; DALISU iT icon exists

TrayTip($tTip, "Dialing " & $SrvName & " connection...", 30, 17)

WinActivate("Network Connections") ; activate window

$var2 = ControlListView("Network Connections", "", "SysListView321", "SelectClear") ; clear any selection

$var2 = ControlListView("Network Connections", "", "SysListView321", "Select", $Conn) ; select DALISU iT icon

Send("{ENTER}") ; dial the connection

WinWait("Connect " & $SrvName)

WinClose("Network Connections", "")

SendKeepActive("Connect " & $SrvName)

ControlSetText ("Connect " & $SrvName, "", 1104, "username")

Send("{TAB}")

ControlSend ("Connect " & $SrvName, "", 1103, "password")

ControlCommand("Connect " & $SrvName, "", "[CLASS:Button; ID:1101; INSTANCE:1]", "UnCheck", "") ; dont save password

Send("{TAB}")

ControlSetText ("Connect " & $SrvName, "", "Edit3", $DialNo) ; tel no to dial

Send("{TAB}")

TrayTip($tTip, "Dialing " & $SrvName & "...", 15, 17)

Send("{ENTER}") ; dial the connection

TrayTip($tTip, "Starting connection timer...", 15, 17)

$ConTimr = TimerInit(); start connection timer

BlockInput(0) ; enable mouse + kbd

TrayTip($tTip, "Waiting for connection...", 240, 17)

_FileWriteLog($logFile,"Dialing " & $SrvName & "...")

EndIf ; no connection window exists

For $tw = 1 To 900 ; 900 x 2s = 1800sec = 30min retry time

TrayTip($tTip, "Checking for connection to " & $SrvName & "... " & $tw, 60, 17)

If FileExists($RemoteFile) Then ; we must be connected now

TrayTip($tTip, "Connected to " & $SrvName & "...", 30, 1)

ExitLoop

EndIf

If WinExists("Network Connections") Then ; dial not successful

TrayTip($tTip, "Unknown Error connecting " & $SrvName & "...", 60, 19)

$lptime = ConnectErr() ; check error and return time spent

If @error Then

SetError(1)

Return

EndIf

$tw += $lptime ; adjust for time spent checking error

EndIf

If WinExists("Error Connecting") Then ; error connecting window

TrayTip($tTip, "Error connecting " & $SrvName & "...", 60, 19)

$lptime = ConnectErr() ; check error and return time spent

$tw += $lptime ; adjust for time spent checking error

EndIf

Sleep(2000) ; wait 2s and check again

Next

$ConTime = TimerDiff($ConTimr) ; stop connection timer

$mins = Floor($ConTime/1000/60)

$secs = Round(Mod($ConTime/1000,60)) & " seconds."

$ConTxt = " Total connect time " & $mins & " mins " & $secs

If FileExists($RemoteFile) Then ; we connected

TrayTip($tTip, "Connected to " & $SrvName & "...", 30, 1)

_FileWriteLog($logFile,"Connected to " & $SrvName & "." & $ConTxt)

SetError(0)

Else

SetError(1)

EndIf

EndFunc

And the function to create connectoid (icon)

CODE
Func CreateDalisu()

;Create DALISU iT dialup connectoid (icon)

TrayTip($tTip, "Creating " & $SrvName & " connectoid...", 30, 17)

_FileWriteLog($logFile,"Creating " & $SrvName & " connectoid.")

If WinExists("New Connection Wizard") Then ; is New Connection Wizard open?

WinActivate("New Connection Wizard") ; activate it

Else

WinActivate("Network Connections")

Send("!fn") ; send Alt+File, New

EndIf

WinWait("New Connection Wizard")

;WinClose("Network Connections")

Send("!n")

Send("!o")

Send("{ENTER}")

Send("!n")

Send("DALISU iT")

Send("!n")

Send("12345678")

Send("!n")

ControlClick("New Connection Wizard", "","[iD:12325]" )

SetError(0)

Return

TrayTip($tTip, "Created " & $SrvName & " connectoid...", 30, 17)

EndFunc

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