#NoTrayIcon ;无托盘图标
HotKeySet("{ESC}","quit") ;ESC 退出
Global $TimeStart=TimerInit() ;程序启动时间戳, 用于检测本程序运行时间. 运行时间超过1分钟自动退出.
Global $PbkName=IniRead("Cfg.ini","SET","PbkFileName","") ;以下及句是读取配置文件的.
Global $LinkType=IniRead("Cfg.ini","SET","Linktype","")
Global $User=IniRead("Cfg.ini","SET","User","")
Global $Pass=IniRead("Cfg.ini","SET","Pass","")
$dll = DllOpen("user32.dll") ;以下3句是启动定时器的代码
$Timer = DllCallbackRegister("Timer", "int", "hwnd;uint;uint;dword")
$TimerDLL = DllCall($dll, "uint", "SetTimer", "hwnd", 0, "uint", 0, "int", 200, "ptr", DllCallbackGetPtr($Timer))
RunPbk() ;调用运行PBK文件的函数
While 1
Sleep(100)
WEnd
Func RunPbk()
ShellExecute(@ScriptDir&""&$PbkName)
ProcessWait("rasphone.exe")
$Title=WinGetTitle("网络连接")
;设置连接类型 从下拉列表中选中需要的类型
ControlCommand ($Title,"","ComboBox1","ShowDropDown", "")
Sleep(100)
ControlSetText($Title,"","ComboBox1",$LinkType)
ControlCommand ($Title,"","ComboBox1","HideDropDown", "")
;点连接
ControlClick($Title,"","Button1","Left",1)
;等待连接窗口出现
WinWait("连接")
$LinkTitle=WinGetTitle("连接")
;设置用户名
ControlSetText($LinkTitle,"","Edit1",$User)
;设置密码
ControlSetText($LinkTitle,"","Edit2",$Pass)
ControlClick($LinkTitle,"","Button4","Left",1)
EndFunc
Func quit() ;退出, 关闭定时器, 释放回调函数指针,关闭DLL
ProcessClose("rasphone.exe")
DllCall($dll, "int", "KillTimer", "hwnd", 0, "uint", $TimerDLL)
DllCallbackFree($Timer)
DllClose($dll)
Exit
EndFunc
Func Timer($hWnd, $uiMsg, $idEvent, $dwTime) ;定时器检测错误,对程序进行异常处理
If WinExists("连接到 "&$LinkType&" 时出错","错误 691") Then
MsgBox(16,"错误!","请检查用户名和密码是否正确!")
quit()
EndIf
If WinExists("连接到 "&$LinkType&" 时出错","错误 800") Then
MsgBox(16,"错误!","请检查VPN服务器是否可以到达!")
quit()
EndIf
If WinExists("连接到 "&$LinkType&" 时出错","错误 629") Then
MsgBox(16,"错误!","请检查用户名和密码是否正确!")
quit()
EndIf
;运行时间超过1分钟自动退出
If TimerDiff($TimeStart)>60000 Then quit()
EndFunc
附件中有完整的测试代码和建立的PBK、配置文件。