Sign in to follow this  
Followers 0
Vinu

Line -1: Error: Subscript used with non-Array variable.

5 posts in this topic

I am trying to write an automation program which connects to a vpn server but this code is really troublesome.

it works in one place and fails in another place. The failure is independent of the OS and happens even on admin accounts with the error when I click the logon button.

Line -1: Error: Subscript used with non-Array variable.

Any idea where this error comes from on some machines ?? .. Please help ???

;GUI Headers
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <IPAddressConstants.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiIPAddress.au3>
;$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

;Func MyErrFunc()
    
;EndFunc

        

;Program headers
Global Const $PbkPath=@AppDataDir & "\Microsoft\Network\Connections\Pbk\rasphone.pbk"
#include <GUIConstantsEx.au3>
#include <Constants.au3>
#include <IE.au3>
#include <GuiTab.au3>
#Include <Misc.au3>
#include <GUIComboBox.au3>
#Include <String.au3>
#Include <Date.au3>


;Globals Variables
Global Const $entry="ScotH"
Global $Connected=False
Global $ServerIP="0.0.0.0"
Global $Curprot,$Curuname,$Curpw,$Cursrv
Global Const $RegPtr="HKCU\Software\ScotH"

Const $PWD="$c0tHos7!" ;used for encryption of username pasowrd
Global $AutoCon=False
Global $ConStat ;to identify its is auto disconencted or user disc
If $cmdline[0]<> 0 And ($cmdline[1]=="-u" Or $cmdline[1]=="-U") Then
    IniDelete($PbkPath,"ScotH")
    RegDelete($RegPtr)
    Exit(0)
EndIf
;Setup Tray Menu
Opt("GUICloseOnESC",0)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1) ; no default menu
Opt("TrayOnEventMode",1) ;onevent mode for tray
TraySetClick(16) ; only secondary/right click and release activates menu
TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE,"OpenGUI")
TraySetOnEvent($TRAY_EVENT_SECONDARYDOWN,"SetTrayMenu")

$TShow=TrayCreateItem("Open Dialer")
TrayItemSetOnEvent(-1,"OpenGUI")

$TCommand=TrayCreateItem("Connect")
TrayItemSetOnEvent(-1,"Dialout")

$TAbout=TrayCreateItem("Visit ScotHosts.com Website")
TrayItemSetOnEvent(-1,"About")

$TExit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"UnloadME")
;
Func UnloadME()
;Exit function
    If Msgbox(4+32+256+8192,"Warning","You are about to close the VPN dialer and any connection made with it" & @CRLF & "Please make sure all data is saved" & @CRLF & @CRLF & "Are you sure you want to quit ?") == 6 Then
        
        ShellExecuteWait("Rasdial.exe"," ScotH /DISCONNECT","","open",@SW_HIDE)

        Exit(0)
    EndIf
EndFunc


Func _RunReadStd()
    ;If still connected ret true otherwise false must be altered to specifically chk ScotH
    Local $output
    RunWait(@ComSpec & " /C rasdial.exe > "& @TempDir &"\rs.txt",@TempDir, @SW_HIDE)
    If FileExists(@TempDir & "\rs.txt") Then
        $output=FileRead(@TempDir & "\rs.txt")
        $output=StringStripCR(StringStripWS(StringReplace($output,"Command completed successfully.",""),1+2))
        FileDelete(@TempDir & "\rs.txt")
        If StringInStr($output,"ScotH") <> 0 Then
            $Connected=True
            Return True
            
        Else
            $Connected=False
            Return False
        EndIf
    Else
        Return False
    EndIf
EndFunc




Func About()
    ;About window to be displayed
    
EndFunc


;;GUI Initializing Code


Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=I:\vpnproj\workroom\frmMain.kxf
;$frmMain = GUICreate("ScotHosts VPN Dialer", 338, 582, 249, 133, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS), BitOR($WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
$frmMain = GUICreate("Scothosts.com VPN Dialler", 338, 582, 249, 133, BitOR($WS_CAPTION,$WS_POPUP,$WS_BORDER,$WS_CLIPSIBLINGS), $WS_EX_WINDOWEDGE)

GUISetBkColor(0xFFFFFF)
GUISetOnEvent($GUI_EVENT_CLOSE, "frmMainClose")
$picLogo = GUICtrlCreatePic("", 0, 0, 330, 140, BitOR($SS_NOTIFY,$WS_GROUP,$WS_CLIPSIBLINGS))
GUICtrlSetOnEvent(-1, "picLogoClick")
$Tab1 = GUICtrlCreateTab(0, 144, 337, 417, $TCS_FLATBUTTONS)
GUICtrlSetResizing(-1, $GUI_DOCKWIDTH+$GUI_DOCKHEIGHT)
$TabSheet1 = GUICtrlCreateTabItem("Login")
GUICtrlSetState(-1,$GUI_SHOW)
$Group1 = GUICtrlCreateGroup("", 8, 184, 313, 257)
$Username = GUICtrlCreateLabel("Username", 16, 232, 60, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0xFFFFFF)
$Label1 = GUICtrlCreateLabel("Password", 16, 280, 58, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0xFFFFFF)
$txtUName = GUICtrlCreateInput("", 80, 224, 217, 21)
GUICtrlSetTip(-1, "Username")
$txtPW = GUICtrlCreateInput("", 80, 272, 217, 21, BitOR($ES_PASSWORD,$ES_AUTOHSCROLL))
GUICtrlSetTip(-1, "Password for the connection")
$Label2 = GUICtrlCreateLabel("Server", 16, 360, 41, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0xFFFFFF)
$cmbServer = GUICtrlCreateCombo("", 80, 352, 217, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetTip(-1, "Select the VPN Server here")
$Label3 = GUICtrlCreateLabel("Protocol", 16, 328, 51, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetBkColor(-1, 0xFFFFFF)
$cmbProt = GUICtrlCreateCombo("", 80, 320, 217, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
If @OSVersion=="WIN_XP" Or @OSType <> "WIN32_NT" Then
    ;old verions
    GUICtrlSetData(-1, "Automatic|PPTP|L2TP IPSec","Automatic")
Else
    GUICtrlSetData(-1, "Automatic|PPTP|L2TP IPSec|SSTP","Automatic")
EndIf


GUICtrlSetTip(-1, "VPN Protocol to be used")
$chkDO = GUICtrlCreateCheckbox("Save as my default settings", 24, 400, 273, 25)
GUICtrlSetOnEvent(-1, "chkDOClick")
GUICtrlSetTip(-1, "Remember these settings will be used for autoconnection")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$btnLogin = GUICtrlCreateButton("L&ogin", 8, 472, 105, 65, 0)
GUICtrlSetOnEvent(-1, "btnLoginClick")
GUICtrlSetTip(-1, "Login to VPN Server")
GUICtrlSetState(-1, $GUI_DISABLE)
$btnMini = GUICtrlCreateButton("Min&imize", 120, 472, 105, 65, 0)
GUICtrlSetOnEvent(-1, "btnMiniClick")
GUICtrlSetTip(-1, "Minimize to tray")
$btnExit = GUICtrlCreateButton("E&xit", 232, 472, 97, 65, 0)
GUICtrlSetOnEvent(-1, "btnExitClick")
$TabSheet2 = GUICtrlCreateTabItem("Config")

$Group2 = GUICtrlCreateGroup("PPP Options", 8, 176, 313, 129)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$chkLCP = GUICtrlCreateCheckbox("Enable LCP Extensions", 32, 200, 145, 17)
GUICtrlSetState(-1,$GUI_CHECKED) ;default
GUICtrlSetTip(-1, "Recommended")
$chkCompress = GUICtrlCreateCheckbox("Enable Software Compression", 32, 232, 169, 17)
GUICtrlSetState(-1,$GUI_CHECKED)
$chkMulti = GUICtrlCreateCheckbox("Negotiate Multilink for single link connections", 32, 264, 249, 17)
GUICtrlSetTip(-1, "Multilink settings")
$btnSavePPPO = GUICtrlCreateButton("SAVE", 256, 192, 57, 17, 0)
GUICtrlSetTip(-1, "Save this setting")
GUICtrlSetOnEvent(-1, "btnSavePPPOClick")
$btnRstPPPO = GUICtrlCreateButton("RESET", 256, 216, 57, 17, 0)
GUICtrlSetOnEvent(-1, "btnRstPPPOClick")
GUICtrlSetTip(-1, "Reset to program defaults")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Program Options", 8, 312, 313, 113)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$chkBootUp = GUICtrlCreateCheckbox("Start with Windows automatically", 32, 336, 177, 17)
GUICtrlSetTip(-1, "Start VPN Dialer automatically when windows start")
$chkAuto = GUICtrlCreateCheckbox("Connect to VPN Server on program start", 32, 368, 209, 17)
GUICtrlSetState(-1,$GUI_DISABLE) ;only to be set once password is saved
GUICtrlSetTip(-1, "Automatically connect to VPN on program start")
$chkRC = GUICtrlCreateCheckbox("Attempt Auto-Reconnection on disconnect", 32, 400, 249, 17)
GUICtrlSetTip(-1, "If VPN gets disconnected try to automatically reconnect")
$btnSavePO = GUICtrlCreateButton("SAVE", 256, 328, 57, 17, 0)
GUICtrlSetTip(-1, "Save this setting for future connections")
GUICtrlSetOnEvent(-1, "btnSavePOClick")
$btnRstPO = GUICtrlCreateButton("RESET", 256, 352, 57, 17, 0)
GUICtrlSetOnEvent(-1, "btnRstPOClick")
GUICtrlSetTip(-1, "Reset to program defaults")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$grpVPN = GUICtrlCreateGroup("VPN Servers", 8, 432, 313, 121)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$Label4 = GUICtrlCreateLabel("Name", 16, 456, 32, 17)
$txtSName = GUICtrlCreateInput("", 72, 448, 169, 21)
GUICtrlSetTip(-1, "An identification for the server ")
$Label5 = GUICtrlCreateLabel("IP Address", 16, 480, 55, 17)
$txtIP1 = GUICtrlCreateInput("0", 72, 472, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "txtIPChange")
GUICtrlSetTip(-1, "IPAddress")
$txtIP2 = GUICtrlCreateInput("0", 111, 472, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "txtIPChange")
GUICtrlSetTip(-1, "IPAddress")
$txtIP3 = GUICtrlCreateInput("0", 152, 472, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "txtIPChange")
GUICtrlSetTip(-1, "IPAddress")
$txtIP4 = GUICtrlCreateInput("0", 192, 472, 33, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL,$ES_NUMBER))
GUICtrlSetLimit(-1, 3)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "txtIPChange")
GUICtrlSetTip(-1, "IPAddress")

$btnSave = GUICtrlCreateButton("ADD", 248, 448, 65, 49, 0)
GUICtrlSetOnEvent(-1, "btnSaveClick")
GUICtrlSetTip(-1, "Save Server details")
$cmbSrv2 = GUICtrlCreateCombo("", 72, 512, 169, 25, BitOR($CBS_DROPDOWNLIST,$CBS_AUTOHSCROLL))
GUICtrlSetState(-1, $GUI_DISABLE)
$Label6 = GUICtrlCreateLabel("Servers", 16, 520, 40, 17)
$btnDelSrv = GUICtrlCreateButton("DELETE", 248, 511, 65, 25, 0)
GUICtrlSetState(-1,$GUI_DISABLE)

GUICtrlSetOnEvent(-1, "btnDelSrvClick")
GUICtrlSetTip(-1, "Delete the selected Server")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateTabItem("")
$mnuFile = GUICtrlCreateMenu("&File")
$mnuExit = GUICtrlCreateMenuItem("E&xit", $mnuFile)
GUICtrlSetOnEvent(-1, "UnloadME")
$mnuHelp = GUICtrlCreateMenu("&Help")
$mnuHel = GUICtrlCreateMenuItem("&About", $mnuHelp)
GUICtrlSetOnEvent(-1, "mnuHelpClick")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
SrvRefresh()
LoadSettings()
ShowTips()

Func ShowTips()
    If _GUICtrlComboBox_GetCount($cmbServer) < 1  Then
        $wx=WinGetPos("Scothosts.com VPN Dialler")
        ToolTip("Please click the config tab above to add your VPN servers",$wx[0]+75,$wx[1]+200,"No VPN Servers Added",1,1)
        Sleep(2500)
        ToolTip("")
    EndIf
EndFunc

IniReadSection($PbkPath,"ScotH")
If @error Then
    PbkSet() ;create connection (Install)
EndIf
While 1
Sleep(100)
WEnd
Func cmbGet($cmb,$IPFlag=False)
    $ips=StringInStr($cmb," [")
    ;values are stored here
    
    $nm=StringMid($cmb,1,$ips)
    
    $ipr=StringMid($cmb,$ips+2)
    $ipr=StringStripWS(StringReplace($ipr,"]",""),1+2)
    If $IPFlag Then
        Return($ipr)
    Else
        Return($nm)
    EndIf
    
EndFunc



Func Dialout()
    AdlibDisable()
    If TrayItemGetText($TCommand)=="Connect" Then
        
        ;if Connection successful
        If GUICtrlRead($txtUName) <> "" And GUICtrlRead($txtPW) <> "" And GUICtrlRead($cmbProt) <> "" And GUICtrlRead($cmbServer) <> "" Then
            $Curuname=GUICtrlRead($txtUName)
            $Curpw=GUICtrlRead($txtPW)
            $Cursrv=cmbGet(GUICtrlRead($cmbServer),True)
            Switch GUICtrlRead($cmbProt)
                Case "Automatic" 
                    $Curprot=2
                Case "PPTP"
                    $Curprot=1
                Case "L2TP IPSec"
                    $Curprot=3
                Case "SSTP"
                    $Curprot=5
            EndSwitch           
            VPNDial("ScotH",$Curuname,$Curpw)
            If $Connected Then

                $ConStat=True
                GUISetState(@SW_HIDE,$frmMain)
                
                TraySetIcon()
            Else
                TraySetIcon("warning")
                
            EndIf
            
        Else
            GUISetState(@SW_SHOW,$frmMain)  
        EndIf
    Else
        If MsgBox(32+4+256+8192,"Warning","You are about to disconnect VPN" & @CRLF & "All resources in the connected network will be unavailable" & @CRLF & "Are you sure you want to continue ?",10)==6 Then
            ShellExecuteWait("Rasdial.exe"," " & $entry & " /DISCONNECT","","open", @SW_HIDE)
            ;disconnect function
            TrayTip("VPN Status","Server Disconnected",5,1)
            $ConStat=False
            $Connected=False
            TraySetIcon("stop")
            TrayItemSetText($TCommand,"Connect")
            TrayItemSetState($TShow,$TRAY_ENABLE)
        EndIf
    EndIf

EndFunc

Func chkConn()
    ;dialout checking function
    AdlibDisable()
    
    If Not _RunReadStd() Then ;disconnected
        TrayItemSetState($TShow,$TRAY_ENABLE)
        $Connected=False
        TrayItemSetText($TCommand,"Connect")
        If GUICtrlRead($chkRC)=$GUI_CHECKED Then
            ;if autoredial is enabled
            TraySetIcon("warning")
            ;Dialout()
            VPNDial("ScotH",$Curuname,$Curpw)
        Else
            TraySetIcon("stop")
            OpenGUI()
        EndIf
        Return
    EndIf
    AdlibEnable("chkConn",3000)
EndFunc


Func VPNDial($entry,$user,$pw,$retries=3)
    AdlibDisable()
    $cmd = "Rasdial.exe"
    $i=0
    
    RunWait("rasdial.exe ScotH /DISCONNECT") ;disconnect first
    While $i < $retries
        
        TrayTip("Please wait..", "Attempting connection to ScotHosts.com VPN", 1,1)
        $xc=GetExitNo($cmd & " " & $entry & " " & $user & " " & $pw)
        
        If $xc == 0 Then
            TrayTip("Success!", "Connected to ScotHosts.com VPN", 5,1)
            $Connected=True
            TraySetIcon()
            TrayItemSetText($TCommand,"DisConnect")
            TrayItemSetState($TShow,$TRAY_DISABLE)
            Sleep(3000)
            AdlibEnable("chkConn",2500)
           ExitLoop
        Else
            TrayTip("Retrying...", "Retrying connection to ScotHosts.com VPN", Default,2)        
            RunWait($cmd & " ScotH /disconnect", "", @SW_HIDE)
            TraySetIcon("stop")
            $Connected=False
            Sleep(2500)
            TrayTip("","",1)
            If Ping("www.yahoo.com")==0 Then
                TrayTip("No Internet", "Please connect to internet before connecting to VPN" & $entry, Default,3)
                ExitLoop
            EndIf
        EndIf
        $i=$i + 1
    WEnd
    If Int($xc) <> 0 Then
        TrayTip("Error","Failed to make VPN Connection" & @CRLF & "RAS Error Code :" & $xc[0] ,5,3)
        TraySetIcon("warning")
        Return False
        
    EndIf   
EndFunc

Func btnDelSrvClick()
;delete selected from both combos
     
    Local $DText
    If RegRead($RegPtr,"srv")==_GUICtrlComboBox_GetLBText($cmbSrv2, _GUICtrlComboBox_GetCurSel($cmbSrv2),$DText) Then
        If Msgbox(1+48+256,"Warning","You are about to delete the default server" & @CRLF & "The saved login settings will be lost if you continue" & @CRLF & "Do you want to continue ?")==2 Then
            Return
        EndIf
        RegDelete($RegPtr,"SaveSet")
        RegDelete($RegPtr,"uname")
        RegDelete($RegPtr,"VPNProt")
        RegDelete($RegPtr,"srv")
        RegDelete($RegPtr,"hash")       
    EndIf
    _GUICtrlComboBox_GetLBText($cmbSrv2, _GUICtrlComboBox_GetCurSel($cmbSrv2),$DText)
    RegDelete($RegPtr & "\Server",StringStripWS(cmbGet($DText),1+2))
    SrvRefresh()
EndFunc
Func btnExitClick()
    UnloadME()
EndFunc
Func btnLoginClick()

    If GUICtrlRead($txtUName)=="" Then
        MsgBox(48,"Invalid Username","Please specify a username",5)
        GUICtrlSetState($txtUName,$GUI_FOCUS)
        Return
    EndIf
    
    If GUICtrlRead($txtPW)=="" Then
        MsgBox(48,"Invalid Password","Password cannot be blank",5)
        GUICtrlSetState($txtPW,$GUI_FOCUS)  
        Return
    EndIf
    If _GUICtrlComboBox_GetCount($cmbServer) < 1  Then
        MsgBox(48,"No VPN Servers","You need to add some VPN servers to connect ",5)
        ;_GUICtrlTab_ClickTab($TabSheet2,1)
        GUICtrlSetState($txtSName,$GUI_FOCUS)
        Return
    Else
            If GUICtrlRead($cmbServer)=="" Then
                MsgBox(48,"More detail please..","Please select which VPN Server should I connect to ? ",5)
                GUICtrlSetState($cmbServer,$GUI_FOCUS)
                Return
            EndIf
    EndIf
    If GUICtrlRead($cmbProt)=="" Then
        MsgBox(48,"VPN Protocol","Please select protocol for VPN Connection" & @CRLF & "You can select Automatic if you are not sure" ,5)
        GUICtrlSetState($cmbProt,$GUI_FOCUS)
        Return
    EndIf
    ;store values for reconnect
    $Curuname=GUICtrlRead($txtUName)
    $Curpw=GUICtrlRead($txtPW)
    $Cursrv=GUICtrlRead($cmbServer)
    Switch GUICtrlRead($cmbProt)
        Case "Automatic" 
            $Curprot=2
        Case "PPTP"
            $Curprot=1
        Case "L2TP IPSec"
            $Curprot=3
        Case "SSTP"
            $Curprot=5
    EndSwitch
    
    ;;Connection function here
    ;;freshly set pbk file based on current settings
    PbkSet($Curprot,_Iif(GUICtrlRead($chkLCP)==$GUI_CHECKED,"1","0"),_Iif(GUICtrlRead($chkCompress)==$GUI_CHECKED,"1","0"),_Iif(GUICtrlRead($chkMulti)==$GUI_CHECKED,"1","0"),cmbGet(GUICtrlRead($cmbServer),True),_Iif(GUICtrlRead($chkRC)==$GUI_CHECKED,"1","0"))
    Dialout()
    ;;if connection success
    If GUICtrlRead($chkDO)==$GUI_CHECKED Then
        ;If $Connected Then
            RegWrite($RegPtr,"uname","REG_SZ",GUICtrlRead($txtUName))
            RegWrite($RegPtr,"SaveSet","REG_SZ","1")
            RegWrite($RegPtr,"VPNProt","REG_SZ",GUICtrlRead($cmbProt))
            RegWrite($RegPtr,"srv","REG_SZ",GUICtrlRead($cmbServer))
            RegWrite($RegPtr,"hash","REG_SZ",_StringToHex(_StringEncrypt(1, GUICtrlRead($txtPW),$PWD)))
        ;Else
            ;RegDelete($RegPtr,"SaveSet")
            ;RegDelete($RegPtr,"uname")
            ;RegDelete($RegPtr,"VPNProt")
            ;RegDelete($RegPtr,"srv")
            ;RegDelete($RegPtr,"hash")
            ;RegWrite($RegPtr,"SaveSet","REG_SZ","0")
        ;EndIf
    Else
        RegWrite($RegPtr,"SaveSet","REG_SZ","0")
        ;we don't delete already saved details
    EndIf
    If $Connected Then
        GUISetState(@SW_HIDE,$frmMain)
    EndIf
    
EndFunc
Func btnMiniClick()
    GUISetState(@SW_HIDE,$frmMain)
EndFunc
Func btnRstPOClick()
    GUICtrlSetState($chkBootUp,$GUI_UNCHECKED)
    GUICtrlSetState($chkAuto,$GUI_UNCHECKED)
    GUICtrlSetState($chkRC,$GUI_UNCHECKED)
EndFunc
Func btnRstPPPOClick()
    GUICtrlSetState($chkLCP,$GUI_CHECKED)
    GUICtrlSetState($chkCompress,$GUI_CHECKED)
    GUICtrlSetState($chkMulti,$GUI_UNCHECKED)
EndFunc
Func btnSaveClick()
;add vpn servers
    $ServerIP=ValidateIP()
    $SName=GUICtrlRead($txtSName)
    If $ServerIP=="" Then
        MsgBox(48,"Invalid IP Address","The VPN Server could not be added because the specified IP is invalid",10)
        GUICtrlSetState($txtIP1,$GUI_FOCUS)
        Return
    EndIf
    If $SName=="" Then
        Msgbox(48,"No Name specified","Please specify a VPN Server name")
        GUICtrlSetState($txtSName,$GUI_FOCUS)
        Return
    EndIf
    RegWrite($RegPtr & "\Server",$SName,"REG_SZ",$ServerIP)
    SrvRefresh()
    _GUICtrlComboBox_SelectString($cmbSrv2,$SName & " [" & $ServerIP & "]")
    Msgbox(64,"Success","Server Successfully added to list")
EndFunc
Func SrvRefresh()
;add values from REgistry
    $i=1
    GUICtrlSetState($cmbSrv2,$GUI_ENABLE)
    GUICtrlSetState($cmbServer,$GUI_ENABLE)
    GUICtrlSetState($btnDelSrv,$GUI_ENABLE)
    GUICtrlSetState($btnLogin,$GUI_ENABLE)
    _GUICtrlComboBox_ResetContent($cmbSrv2)
    _GUICtrlComboBox_ResetContent($cmbServer)
    While 1
        $keyname=RegEnumVal($RegPtr & "\Server", $i)
        If @error Then 
            ExitLoop
        EndIf
        ;update server list for both combos
        _GUICtrlComboBox_AddString($cmbSrv2,$keyname & " [" & RegRead($RegPtr & "\Server",$keyname) & "]")
    
    
        _GUICtrlComboBox_AddString($cmbServer,$keyname & " [" & RegRead($RegPtr & "\Server",$keyname) & "]")
        $i=$i +1
    WEnd
    
    If _GUICtrlComboBox_GetCount($cmbServer) < 1 Then
        GUICtrlSetState($cmbSrv2,$GUI_DISABLE)
        GUICtrlSetState($btnDelSrv,$GUI_DISABLE)
        GUICtrlSetState($cmbServer,$GUI_DISABLE)
        GUICtrlSetState($btnLogin,$GUI_DISABLE)
        
    EndIf
EndFunc

Func btnSavePOClick()
    $r=False
    If RegWrite($RegPtr,"AutoC","REG_SZ",_Iif(GUICtrlRead($chkAuto)==$GUI_CHECKED,"1","0")) Then
        $r=True
    EndIf
    
    If RegWrite($RegPtr,"Redial","REG_SZ",_Iif(GUICtrlRead($chkRC)==$GUI_CHECKED,"1","0")) Then
        $r=True
    EndIf
    
    If GUICtrlRead($chkBootUp)==$GUI_CHECKED Then
        
        If FileCreateShortcut(@ScriptFullPath,@StartupDir & "\ScotH.lnk") Then
            $r=True
        EndIf
    Else
        If FileExists(@StartupDir & "\ScotH.lnk") Then 
            If FileDelete(@StartupDir & "\ScotH.lnk") Then
                $r=True
            EndIf
        Else
            $r=True
        EndIf
    EndIf   
    If $r Then 
        MsgBox(64,"Success","Program Settings Saved. Please restart for settings to take effect",10)
    Else
        Msgbox(48,"Access Denied","Changes could not be saved")
    EndIf
    ;Call refresh function here
EndFunc

Func btnSavePPPOClick()
    RegWrite($RegPtr,"LCP","REG_SZ",_Iif(GUICtrlRead($chkLCP)==$GUI_CHECKED,"1","0"))
    RegWrite($RegPtr,"Compress","REG_SZ",_Iif(GUICtrlRead($chkCompress)==$GUI_CHECKED,"1","0"))
    RegWrite($RegPtr,"Multi","REG_SZ",_Iif(GUICtrlRead($chkMulti)==$GUI_CHECKED,"1","0"))
    If Not @error Then
        MsgBox(64,"Data Saved","PPP Settings Saved")
    Else
        MsgBox(48,"Access Denied","Settings Could not be saved")
    EndIf
    ;;Call refersh function here
    
EndFunc
Func chkDOClick()

EndFunc
Func frmMainClose()
;nothing to do here ..no close button
EndFunc
Func mnuHelpClick()
    
EndFunc
Func picLogoClick()
    
EndFunc
Func txtIPChange()
;validate for ranges
    $ip1=Int(GUICtrlRead($txtIP1))
    $ip2=Int(GUICtrlRead($txtIP2))
    $ip3=Int(GUICtrlRead($txtIP3))
    $ip4=Int(GUICtrlRead($txtIP4))
    If ($ip1 > 255) Then
        GUICtrlSetData($txtIP1,"255")
    EndIf
    If ($ip2 > 255) Then
        GUICtrlSetData($txtIP2,"255")
    EndIf
    If ($ip3 > 255) Then
        GUICtrlSetData($txtIP3,"255")
    EndIf   
    If ($ip4 > 255) Then
        GUICtrlSetData($txtIP4,"255")
    EndIf
EndFunc

Func ValidateIP()
    ;returns IPaddress or blank string on failure
    $ip1=Int(GUICtrlRead($txtIP1))
    $ip2=Int(GUICtrlRead($txtIP2))
    $ip3=Int(GUICtrlRead($txtIP3))
    $ip4=Int(GUICtrlRead($txtIP4))
    If ($ip1==0 And $ip2==0 And $ip3==0 And $ip4==0) Or ($ip1==255 And $ip2==255 And $ip3==255 And $ip4==255) Then
        Return("")
    EndIf
    If ($ip1 > 255) Or ($ip2 > 255) Or ($ip3 > 255) Or ($ip4 > 255) Then
        Return ""
    EndIf
    Return String($ip1 & "." & $ip2 & "." & $ip3 & "." & $ip4)
EndFunc
Func OpenGUI()
    ;show GUI
    If Not $Connected Then
        GUISetState(@SW_SHOW,$frmMain)
    EndIf
EndFunc


Func SetRASKey()
    $RASCREDENTIALS=DllStructCreate("dword dwSize;dword dwMask;char szUserName[257];char szPassword[257];char szDomain[16]")
    If @error Then
        Return(False)
    EndIf
    DllStructSetData($RASCREDENTIALS,"dwMask",16)
    DllStructSetData($RASCREDENTIALS,"szUserName","")
    DllStructSetData($RASCREDENTIALS,"szPassword","123456" & Chr(0)) ; this key is just a dummy key
    DllStructSetData($RASCREDENTIALS,"szDomain","")
    DllStructSetData($RASCREDENTIALS,"dwSize",540)
    $ret=DllCall("rasapi32.dll","dword","RasSetCredentialsA","str",$PbkPath,"str","ScotH","ptr",DllStructGetPtr($RASCREDENTIALS),"long",0)
    ;msgbox(0,"",$ret[0])
    If Not @error Then
        If $ret[0] <> 0 Then
            Return(False)
        Else
            Return(True)    
        EndIf
    Else
        Return(False)
    EndIf
EndFunc


Func PbkSet($vpntype=-1,$lcp=-1,$compress=-1,$multilink=-1,$phno="0.0.0.0",$autorecon=-1)
    IniWrite($PbkPath,"ScotH","Encoding","1")
    IniWrite($PbkPath,"ScotH","Type","2")
    IniWrite($PbkPath,"ScotH","AutoLogon","0")
    IniWrite($PbkPath,"ScotH","UseRasCredentials","1")
    IniWrite($PbkPath,"ScotH","DialParamsUID","5063745")
    IniWrite($PbkPath,"ScotH","Guid","64F72012016E0D42A9B62ABA5703FB6C")
    IniWrite($PbkPath,"ScotH","BaseProtocol","1")
    If $vpntype <> -1 Then
        IniWrite($PbkPath,"ScotH","VpnStrategy",String($vpntype))

    EndIf
    IniWrite($PbkPath,"ScotH","ExcludedProtocols","10")
    If $lcp <> -1 Then
        IniWrite($PbkPath,"ScotH","LcpExtensions",String($lcp))
    EndIf
    IniWrite($PbkPath,"ScotH","DataEncryption","256")
    If $compress <> -1 Then
        IniWrite($PbkPath,"ScotH","SwCompression",String($compress))
    EndIf
    If $multilink <> -1 Then
        IniWrite($PbkPath,"ScotH","NegotiateMultilinkAlways",String($multilink))
    EndIf
    IniWrite($PbkPath,"ScotH","SkipNwcWarning","0")
    IniWrite($PbkPath,"ScotH","SkipDownLevelDialog","0")
    IniWrite($PbkPath,"ScotH","SkipDoubleDialDialog","0")
    IniWrite($PbkPath,"ScotH","DialMode","0")
    IniWrite($PbkPath,"ScotH","DialPercent","75")
    IniWrite($PbkPath,"ScotH","DialSeconds","120")
    IniWrite($PbkPath,"ScotH","HangUpPercent","10")
    IniWrite($PbkPath,"ScotH","HangUpSeconds","120")
    IniWrite($PbkPath,"ScotH","OverridePref","15")
    IniWrite($PbkPath,"ScotH","RedialAttempts","3")
    IniWrite($PbkPath,"ScotH","RedialSeconds","60")
    IniWrite($PbkPath,"ScotH","IdleDisconnectSeconds","0")
    If $autorecon <> -1 Then
        IniWrite($PbkPath,"ScotH","RedialOnLinkFailure",String($autorecon))
    EndIf
    IniWrite($PbkPath,"ScotH","CallbackMode","0")
    IniWrite($PbkPath,"ScotH","CustomDialDll","")
    IniWrite($PbkPath,"ScotH","CustomDialFunc","")
    IniWrite($PbkPath,"ScotH","CustomRasDialDll","")
    IniWrite($PbkPath,"ScotH","AuthenticateServer","0")
    IniWrite($PbkPath,"ScotH","ShareMsFilePrint","1")
    IniWrite($PbkPath,"ScotH","BindMsNetClient","1")
    IniWrite($PbkPath,"ScotH","SharedPhoneNumbers","0")
    IniWrite($PbkPath,"ScotH","GlobalDeviceSettings","0")
    IniWrite($PbkPath,"ScotH","PrerequisiteEntry","")
    IniWrite($PbkPath,"ScotH","PrerequisitePbk","")
    IniWrite($PbkPath,"ScotH","PreferredPort","VPN0-0")
    IniWrite($PbkPath,"ScotH","PreferredDevice","WAN Miniport (L2TP)")
    IniWrite($PbkPath,"ScotH","PreferredBps","0")
    IniWrite($PbkPath,"ScotH","PreferredHwFlow","1")
    IniWrite($PbkPath,"ScotH","PreferredProtocol","1")
    IniWrite($PbkPath,"ScotH","PreferredCompression","1")
    IniWrite($PbkPath,"ScotH","PreferredSpeaker","1")
    IniWrite($PbkPath,"ScotH","PreferredMdmProtocol","0")
    IniWrite($PbkPath,"ScotH","PreviewUserPw","1")
    IniWrite($PbkPath,"ScotH","PreviewDomain","1")
    IniWrite($PbkPath,"ScotH","PreviewPhoneNumber","0")
    IniWrite($PbkPath,"ScotH","ShowDialingProgress","1")
    IniWrite($PbkPath,"ScotH","ShowMonitorIconInTaskBar","1")
    IniWrite($PbkPath,"ScotH","CustomAuthKey","-1")
    
    IniWrite($PbkPath,"ScotH","TypicalAuth","2")
    IniWrite($PbkPath,"ScotH","IpPrioritizeRemote","1")
    IniWrite($PbkPath,"ScotH","IpHeaderCompression","0")
    IniWrite($PbkPath,"ScotH","IpAddress","0.0.0.0")
    IniWrite($PbkPath,"ScotH","IpDnsAddress","0.0.0.0")
    IniWrite($PbkPath,"ScotH","IpDns2Address","0.0.0.0")
    IniWrite($PbkPath,"ScotH","IpWinsAddress","0.0.0.0")
    IniWrite($PbkPath,"ScotH","IpWins2Address","0.0.0.0")
    IniWrite($PbkPath,"ScotH","IpAssign","1")
    IniWrite($PbkPath,"ScotH","IpNameAssign","1")
    IniWrite($PbkPath,"ScotH","IpFrameSize","1006")
    IniWrite($PbkPath,"ScotH","IpDnsFlags","0")
    IniWrite($PbkPath,"ScotH","IpNBTFlags","1")
    IniWrite($PbkPath,"ScotH","TcpWindowSize","0")
    IniWrite($PbkPath,"ScotH","UseFlags","0")

    
    IniWrite($PbkPath,"ScotH","IpDnsSuffix","")

    IniWrite($PbkPath,"ScotH","NETCOMPONENTS","")
    IniWrite($PbkPath,"ScotH","ms_msclient","1")
    IniWrite($PbkPath,"ScotH","ms_server","1")
    IniWrite($PbkPath,"ScotH","ms_pacer","1")
    IniWrite($PbkPath,"ScotH","dni_dne","1")
    IniWrite($PbkPath,"ScotH","ms_psched","1")

    IniWrite($PbkPath,"ScotH","MEDIA","rastapi")
    IniWrite($PbkPath,"ScotH","Port","VPN4-0")
    IniWrite($PbkPath,"ScotH","Device ","WAN Miniport (L2TP)")

    IniWrite($PbkPath,"ScotH","DEVICE","vpn")
    If $phno<> "0.0.0.0" Then
        IniWrite($PbkPath,"ScotH","PhoneNumber",String($phno))
    EndIf
    IniWrite($PbkPath,"ScotH","AreaCode","")
    IniWrite($PbkPath,"ScotH","CountryCode","1")
    IniWrite($PbkPath,"ScotH","CountryID","1")
    IniWrite($PbkPath,"ScotH","UseDialingRules","0")
    IniWrite($PbkPath,"ScotH","Comment","")
    IniWrite($PbkPath,"ScotH","LastSelectedPhone","0")
    IniWrite($PbkPath,"ScotH","PromoteAlternates","0")
    IniWrite($PbkPath,"ScotH","TryNextAlternateOnFail","1")
    If $vpntype <> -1 Then
        If $vpntype==2 Or $vpntype==3  Then ;l2tp automatic
            IniWrite($PbkPath,"ScotH","AuthRestrictions","608")
            IniWrite($PbkPath,"ScotH","IpSecFlags","1")
            SetRASKey()
            
        Else
            IniWrite($PbkPath,"ScotH","AuthRestrictions","576")
            IniWrite($PbkPath,"ScotH","IpSecFlags","0")
        EndIf
    EndIf
EndFunc

Func LoadSettings()
     
    GUICtrlSetState($chkRC,_Iif(RegRead($RegPtr,"Redial")=="1",$GUI_CHECKED,$GUI_UNCHECKED))
    GUICtrlSetState($chkMulti,_Iif(RegRead($RegPtr,"Multi")=="1",$GUI_CHECKED,$GUI_UNCHECKED))
    GUICtrlSetState($chkCompress,_Iif(RegRead($RegPtr,"Compress")=="0",$GUI_UNCHECKED,$GUI_CHECKED))
    GUICtrlSetState($chkLCP,_Iif(RegRead($RegPtr,"LCP")=="0",$GUI_UNCHECKED,$GUI_CHECKED))
    GUICtrlSetState($chkAuto,_Iif(RegRead($RegPtr,"AutoC")=="1",$GUI_CHECKED,$GUI_UNCHECKED))
    If RegRead($RegPtr,"AutoC")=="1" Then
        $AutoCon=True
    Else
        $AutoCon=False
    EndIf
    
    If FileExists(@StartupDir & "\ScotH.lnk") Then
        GUICtrlSetState($chkBootUp,$GUI_CHECKED)
    Else
        GUICtrlSetState($chkBootUp,$GUI_UNCHECKED)
    EndIf
    
    If RegRead($RegPtr,"SaveSet")=="1" Then
    ;username password
        GUICtrlSetData($txtUName,RegRead($RegPtr,"uname"))
        GUICtrlSetData($txtPW,_StringEncrypt(0,_HexToString(RegRead($RegPtr,"hash")),$PWD))
        GUICtrlSetState($chkDO,$GUI_CHECKED)
        _GUICtrlComboBox_SetCurSel($cmbProt,_GUICtrlComboBox_FindStringExact($cmbProt,RegRead($RegPtr,"VPNProt")))
        _GUICtrlComboBox_SetCurSel($cmbServer,_GUICtrlComboBox_FindStringExact($cmbServer,RegRead($RegPtr,"srv")))
        GUICtrlSetState($chkAuto,$GUI_ENABLE)
        $Curuname=GUICtrlRead($txtUName)
        $Curpw=GUICtrlRead($txtPW)
        $Cursrv=cmbGet(GUICtrlRead($cmbServer),True)
        Switch GUICtrlRead($cmbProt)
            Case "Automatic" 
                $Curprot=2
            Case "PPTP"
                $Curprot=1
            Case "L2TP IPSec"
                $Curprot=3
            Case "SSTP"
                $Curprot=5
        EndSwitch           
        If Not $Connected And $AutoCon Then
            PbkSet($Curprot,_Iif(RegRead($RegPtr,"LCP")=="0","1","0"),_Iif(RegRead($RegPtr,"Compress")=="0","0","1"),_Iif(RegRead($RegPtr,"Multi")=="1","1","0"),$Cursrv,_Iif(RegRead($RegPtr,"AutoC")=="1","1","0"))
            VPNDial("ScotH",$Curuname,$Curpw)
        EndIf
    Else
        GUICtrlSetData($txtUName,"")
        GUICtrlSetData($txtPW,"")
        GUICtrlSetState($chkDO,$GUI_UNCHECKED)
        GUICtrlSetState($chkAuto,$GUI_UNCHECKED)
        GUICtrlSetState($chkAuto,$GUI_DISABLE)
        RegWrite($RegPtr,"AutoC","REG_SZ","0") ;extra precaution
        $AutoCon=False
    EndIf

;remember to enable autoconnect option here
EndFunc

func GetExitNo($doscmd,$timeoutSeconds=0,$workingdir=@ScriptDir,$flag=@SW_HIDE)
    local $aReturn,$i_Pid,$h_Process,$i_ExitCode,$sStdOut,$sStdErr,$runTimer
    dim $aReturn
    
    ; run process with StdErr and StdOut flags
    $runTimer = TimerInit()
    $i_Pid = Run($doscmd, $workingdir, $flag, 6) ; 6 = $STDERR_CHILD+$STDOUT_CHILD
    
    ; Get process handle
    sleep(100) ; or DllCall may fail - experimental
    $h_Process = DllCall('kernel32.dll','ptr', 'OpenProcess','int', 0x400,'int', 0,'int', $i_Pid)
    
    ; create tab delimited string containing StdOut text from process
 
    if $timeoutSeconds > 0 Then
        if TimerDiff($runTimer)/1000 > $timeoutSeconds Then
            ProcessClose($i_Pid)
        EndIf
    EndIf

    ; fetch exit code and close process handle
    If IsArray($h_Process) Then
        Sleep(100) ; or DllCall may fail - experimental
        $i_ExitCode = DllCall('kernel32.dll','ptr', 'GetExitCodeProcess','ptr', $h_Process[0],'int*', 0)
        if IsArray($i_ExitCode) Then
            $aReturn = $i_ExitCode[2]
        Else
            $aReturn = -1
        EndIf
        Sleep(100) ; or DllCall may fail - experimental
        DllCall('kernel32.dll','ptr', 'CloseHandle','ptr', $h_Process[0])
    Else
        $aReturn = -2
    EndIf

    return $aReturn
EndFunc

Func SetTrayMenu()
    ;sets tray menu items
    _RunReadStd()
    If $Connected Then
        TrayItemSetState($TShow,$TRAY_DISABLE)
        TrayItemSetText($TCommand,"DisConnect")
        TraySetIcon()
    Else
        TrayItemSetState($TShow,$TRAY_ENABLE)
        TrayItemSetText($TCommand,"Connect")
        TraySetIcon("stop")     
    EndIf
    
EndFunc

Share this post


Link to post
Share on other sites



Run this code directly into the SciTE (F5), and look at what line an error occurs. I do not see the other way.

Thanks Yashied,

But the problem here is that the users connect to this vpn from far and end and running autoitscript and troubleshooting in their system(since its not happening everywhere

) could be really tough :)

Its a user interface that can be used for any generic vpn connection. There is nothing proprietary. I wish there was some way to pullout correct line numbers in their sys :) tem

Share this post


Link to post
Share on other sites

Maybe this second line where you refer to $xc as array?

If Int($xc) <> 0 Then       
    TrayTip("Error", "Failed to make VPN Connection" & @CRLF & "RAS Error Code :" & $xc[0], 5, 3)

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thank you so much Jos,

Bulls eye ! :)

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