Dim I as integer
Dim N as long
Option explicit
'Dial-up/network disconnection
Private declare function internetdial lib "wininet. dll" (byval hwndparent as long, byval lpszconnectoid as string, byval dwflags as long, lpdwconnection as long, byval dwreserved as long) as long
Private declare function internethangup lib "wininet. dll" (byval dwconnection as long, byval dwreserved as long) as long
Private declare function internetautodial lib "wininet. dll" (byval dwflags as long, byval dwreserved as long) as long
Private declare function internetautodialhangup lib "wininet. dll" (byval dwreserved as long) as long
Private const internet_dialstate_disconnected = 1
Private const internet_autodial_force_online = 1
Private const internet_autodial_force_unattended = 2
Private const internet_dial_unattended = & h8000
Private handle as long
Private type guid
Data1 as long
Data2 as integer
Data3 as integer
Data4 (7) as byte
End type
Private type rasipaddr
A As byte
B As byte
C As byte
D As byte
End type
Private type rasentry
Dwsize as long
Dwfoptions as long
Dwcountryid as long
Dwcountrycode as long
Szareacode (10) as byte
Szlocalphonenumber (128) as byte
Dwalternateoffset as long
Ipaddr as rasipaddr
Ipaddrdns as rasipaddr
Ipaddrdnsalt as rasipaddr
Ipaddrwins as rasipaddr
Ipaddrwinsalt as rasipaddr
Dwframesize as long
Dwfnetprotocols as long
Dwframingprotocol as long
Szscript (259) as byte
Szautodialdll (259) as byte
Szautodialfunc (259) as byte
Szdevicetype (16) As byte
Szergonomic ename (128) as byte
Szx25padtype (32) as byte
Szx25address (200) as byte
Szx25facilities (200) as byte
Szx25userdata (200) as byte
Dwchannels as long
Dwreserved1 as long
Dwreserved2 as long
Dwsubentries as long
Dwdialmode as long
Dwdialextrapercent as long
Dwdialextrasampleseconds as long
Dwhangupextrapercent as long
Dwhangupextrasampleseconds as long
Dwidledisconnectseconds as long
Dwtype as long
Dwencryptiontype as long
Dwcustomauthkey as long
Guidid as guid
Szcustomdialdll (259) as byte
Dwvpnstrategy as long
Dwfoptions2 as long
Dwfoptions3 as long
Szdnssuffix (255) as byte
Dwtcpwindowsize as long
Szprerequisitepbk (259) as byte
Szprerequisiteentry (256) as byte
Dwredialcount as long
Dwredialpause as long
End type
Private type rascredentials
Dwsize as long
Dwmask as long
Szusername as string * 257 'unlen + 1
Szpassword as string * 257 'pwlen + 1
Szdomain as string * 16 'dnlen + 1
End type
Public Enum rascredmask
Rascm_username = & H1 &
Rascm_password = & H2 & 'pre-shared key password
Rascm_domain = & H4 &
Rascm_defaultcreds = & H8 &
Rascm_presharedkey = & H10 & 'pre-shared key attributes
Rascm_serverpresharedkey = & H20 &
Rascm_ddmpresharedkey = & h40 &
End Enum
Public Enum rasentryoptions2
Raseo2_securefileandprint = & H1
Raseo2_secureclientformsnet = & H2
Raseo2_dontnegotiatemultilink = & h4
Raseo2_dontuserascredentials = & H8
Raseo2_usepresharedkey = & h10' use the pre-shared key for authentication
Raseo2_internet = & H20
Raseo2_disablenbtoverip = & h40
Raseo2_useglobaldevicesettings = & h80
Raseo2_reconnectifdropped = & h100
Raseo2_sharephonenumbers = & h200
Raseo2_secureroutingcompartment = & H400
Raseo2_4256specificnameserver = & h800
Raseo2_00006remotedefagateway gateway = & h1000
Raseo2_registeripwithdns = & h2000
Raseo2_usednssuffixforregistration = & h4000
Raseo2_00004explicitmetric = & h8000
Raseo2_00006explicitmetric = & h10000
Raseo2_disableikenameekucheck = & h20000
End Enum
Private const et_none as long = 0' no encryption
Private const et_require as long = 1 'require Encryption
Private const et_requiremax as long = 2 'require Max Encryption
Private const et_optional as long = 3 'do encryption if possible. None OK.
Private const vs_default as long = 0 'default (PPTP for now)
Private const vs_pptponly as long = 1' only PPTP is attempted.
Private const vs_pptpfirst as long = 2 'pptp is tried first.
Private const vs_l2tponly as long = 3' only L2TP is attempted.
Private const vs_l2tpfirst as long = 4 'l2tp is tried first.
Private const raset_phone as long = 1 'phone lines: Modem, ISDN, X.25, etc
Private const raset_vpn as long = 2 'virtual Private Network
Private const raset_direct as long = 3 'direct CONNECT: serial, parallel
Private const raset_internet as long = 4' Basecamp Internet
Private const raset_broadband as long = 5' broadband
Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)
Private declare function rassetentryproperties lib "rasapi32" alias "alias" (byval encoded as string, byval lpszentry as string, lprasentry as rasentry, byval dwentryinfosize as long, byval lpbdeviceinfo as long, byval dwdeviceinfosize as long) as long
Private declare function rassetcredentials lib "rasapi32" alias "login" (byval lpszphonebook as string, byval lpszentry as string, lpcredentials as rascredentials, byval fclearcredentials as long) as long
Private sub commandementclick ()
Dim sentryname as string, susername as string, spassword as string
Dim sserver as string
Sentryname = text1.text
Sserver = text2.text
Susername = text3.text
Spassword = text4.text
If create_vpn_connection (sentryname, sserver, susername, spassword) then
Msgbox "VPN connection established successfully! "
Else
Msgbox "VPN connection setup failed! "
End if
End sub
Function create_vpn_connection (byval sentryname as string, byval sserver as string, byval susername as string, byval spassword as string) as Boolean
Create_vpn_connection = false
Dim re as rasentry
Dim sdevicename as string, sdevicetype as string
Sdevicename = "Wan micro port (L2TP )"
Sdevicetype = "VPN"
With re
. Dwsize = lenb (re)
. Dwcountrycode = 86
. Dwcountryid = 86
. Dwdialextrapercent = 75
. Dwdialextrasampleseconds = 120
. Dwdialmode = 1
. Dwfnetprotocols = 4
. DwF Options = 1024262928
'. Dwfoptions2 = 367
. Dwfoptions2 = rasentryoptions2.raseo2 _ usepresharedkey ': Set the attribute of dwfoptions2 to raseo2_usepresharedkey.
. Dwframingprotocol = 1
. Dwhangupextrapercent = 10
. Dwhangupextrasampleseconds = 120
. Dwredialcount = 3
. Dwredialpause = 60
. Dwtype = raset_vpn
Copymemory. szdevicename (0), byval sdevicename, Len (sdevicename)
Copymemory. szdevicetype (0), byval sdevicetype, Len (sdevicetype)
Copymemory. szlocalphonenumber (0), byval sserver, Len (sserver) 'server address
. Dwvpnstrategy = vs_l2tponly 'vpn type
. Dwencryptiontype = et_require 'data encryption type
End
Dim RC as rascredentials
With RC
Mid (. szpassword, 1, Len ("xxxxxx") = "xxxxxx" 'xxxxxx is the pre-shared key
. Wdwsize = 540
. Dwmask = rascredmask. rascm_presharedkey
End
Dim RTN as long
If rassetentryproperties (vbnullstring, sentryname, re, lenb (RE), 0, 0) = 0 then
If rassetcredentials (vbnullstring, sentryname, RC, 0) = 0 then
Create_vpn_connection = true
End if
End if
End Function
'Dialing
Function dialup (linkname as string) as Boolean
Internetdial 0, linkname, internet_autodial_force_unattended, handle, 0
Dialup = (handle <> 0)
End Function
Private sub command3_click ()
Unload me
End sub
Private sub form_load ()
Me. timer1.enabled = false
Text1.text = "VPN connection"
Text2.text = "111.111.111.111" 'server address
Text3.text = "xxxxxx"
Text4.text = "xxxxxx"
End sub
Reference:
Http://www.vbmonster.com/Uwe/Forum.aspx/vb-networks/564/RasSetCredentials-VB6-Help
Http://www.vbforums.com/archive/index.php/t-451832.html
Thanks Randy, Johno, robdog888, logophobic