| 1 |
Attribute VB_Name = "RasEntry" |
| 2 |
' RasConnect |
| 3 |
' http://www.mentalis.org/apilist/RasDial.shtml |
| 4 |
' example created by Daniel Kaufmann (daniel@i.com.uy) |
| 5 |
|
| 6 |
' RasDisconnect |
| 7 |
' http://www.activevb.de/tipps/vb6tipps/tipp0009.html |
| 8 |
|
| 9 |
|
| 10 |
Option Explicit |
| 11 |
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, ByVal pSrc As String, ByVal ByteLen As Long) |
| 12 |
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long) |
| 13 |
|
| 14 |
Const RAS95_MaxEntryName = 256 |
| 15 |
Const RAS_MaxPhoneNumber = 128 |
| 16 |
Const RAS_MaxCallbackNumber = RAS_MaxPhoneNumber |
| 17 |
|
| 18 |
Const UNLEN = 256 |
| 19 |
Const PWLEN = 256 |
| 20 |
Const DNLEN = 12 |
| 21 |
|
| 22 |
Private Type RASDIALPARAMS |
| 23 |
dwSize As Long ' 1052 |
| 24 |
szEntryName(RAS95_MaxEntryName) As Byte |
| 25 |
szPhoneNumber(RAS_MaxPhoneNumber) As Byte |
| 26 |
szCallbackNumber(RAS_MaxCallbackNumber) As Byte |
| 27 |
szUserName(UNLEN) As Byte |
| 28 |
szPassword(PWLEN) As Byte |
| 29 |
szDomain(DNLEN) As Byte |
| 30 |
End Type |
| 31 |
|
| 32 |
Private Type RASENTRYNAME95 |
| 33 |
'set dwsize to 264 |
| 34 |
dwSize As Long |
| 35 |
szEntryName(RAS95_MaxEntryName) As Byte |
| 36 |
End Type |
| 37 |
|
| 38 |
|
| 39 |
Private Declare Function RasDial Lib "rasapi32.dll" Alias "RasDialA" _ |
| 40 |
(ByVal lprasdialextensions As Long, ByVal lpcstr As String, _ |
| 41 |
ByRef lprasdialparamsa As RASDIALPARAMS, ByVal dword As Long, _ |
| 42 |
lpvoid As Any, ByRef lphrasconn As Long) As Long |
| 43 |
|
| 44 |
Private Declare Function RasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntriesA" _ |
| 45 |
(ByVal reserved As String, ByVal lpszPhonebook As String, lprasentryname As Any, _ |
| 46 |
lpcb As Long, lpcEntries As Long) As Long |
| 47 |
|
| 48 |
Private Declare Function RasGetEntryDialParams Lib "rasapi32.dll" Alias "RasGetEntryDialParamsA" _ |
| 49 |
(ByVal lpcstr As String, ByRef lprasdialparamsa As RASDIALPARAMS, ByRef lpbool As Long) As Long |
| 50 |
|
| 51 |
Private Declare Function RasSetEntryDialParams _ |
| 52 |
Lib "rasapi32.dll" Alias "RasSetEntryDialParamsA" _ |
| 53 |
(ByVal lpszPhonebook As String, _ |
| 54 |
lpRasDialParams As RASDIALPARAMS, _ |
| 55 |
ByVal blnRemovePassword As Long) As Long |
| 56 |
|
| 57 |
Private Function Dial(ByVal Connection As String, ByVal phoneBook As String, ByVal username As String, ByVal password As String) As Integer |
| 58 |
|
| 59 |
Dim rp As RASDIALPARAMS, h As Long, resp As Long |
| 60 |
rp.dwSize = Len(rp) + 6 |
| 61 |
ChangeBytes Connection, rp.szEntryName |
| 62 |
ChangeBytes "", rp.szPhoneNumber 'Phone number stored for the connection |
| 63 |
ChangeBytes "*", rp.szCallbackNumber 'Callback number stored for the connection |
| 64 |
ChangeBytes username, rp.szUserName |
| 65 |
ChangeBytes password, rp.szPassword |
| 66 |
ChangeBytes "*", rp.szDomain 'Domain stored for the connection |
| 67 |
|
| 68 |
'Dial |
| 69 |
resp = RasDial(ByVal 0, ByVal phoneBook, rp, 0, ByVal 0, h) 'AddressOf RasDialFunc |
| 70 |
Dial = resp |
| 71 |
|
| 72 |
End Function |
| 73 |
|
| 74 |
Private Function ChangeToStringUni(Bytes() As Byte) As String |
| 75 |
'Changes an byte array to a Visual Basic unicode string |
| 76 |
Dim temp As String |
| 77 |
temp = StrConv(Bytes, vbUnicode) |
| 78 |
ChangeToStringUni = Left(temp, InStr(temp, Chr(0)) - 1) |
| 79 |
End Function |
| 80 |
|
| 81 |
Private Function ChangeBytes(ByVal str As String, ByRef Bytes() As Byte) As Boolean |
| 82 |
'Changes a Visual Basic unicode string to an byte array |
| 83 |
'Returns True if it truncates str |
| 84 |
Dim lenBs As Long 'length of the byte array |
| 85 |
Dim lenStr As Long 'length of the string |
| 86 |
lenBs = UBound(Bytes) - LBound(Bytes) |
| 87 |
lenStr = LenB(StrConv(str, vbFromUnicode)) |
| 88 |
If lenBs > lenStr Then |
| 89 |
CopyMemory Bytes(0), str, lenStr |
| 90 |
ZeroMemory Bytes(lenStr), lenBs - lenStr |
| 91 |
ElseIf lenBs = lenStr Then |
| 92 |
CopyMemory Bytes(0), str, lenStr |
| 93 |
Else |
| 94 |
CopyMemory Bytes(0), str, lenBs 'Queda truncado |
| 95 |
ChangeBytes = True |
| 96 |
End If |
| 97 |
End Function |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
Public Function RasConnect(conName As String, phoneBook As String) As Boolean |
| 102 |
|
| 103 |
Dim rdp As RASDIALPARAMS, t As Long |
| 104 |
Dim username As String, password As String |
| 105 |
|
| 106 |
rdp.dwSize = Len(rdp) + 6 |
| 107 |
ChangeBytes conName, rdp.szEntryName |
| 108 |
|
| 109 |
' Get User name and password for the connection |
| 110 |
t = RasGetEntryDialParams(phoneBook, rdp, 0) |
| 111 |
If t = 0 Then |
| 112 |
username = ChangeToStringUni(rdp.szUserName) |
| 113 |
password = ChangeToStringUni(rdp.szPassword) |
| 114 |
End If |
| 115 |
|
| 116 |
Dim errorCode As Integer |
| 117 |
errorCode = Dial(conName, phoneBook, username, password) |
| 118 |
|
| 119 |
If (errorCode <> 0) Then |
| 120 |
If errorCode = 691 Then |
| 121 |
MsgBox "Fehler 691: Der Zugriff wurde verweigert, weil Benutzername und/oder Kennwort ungültig ist/sind." |
| 122 |
Else |
| 123 |
MsgBox "Fehler-Code: " & errorCode & vbCrLf & "http://www.admins-tipps.net/glossar/ras_errorcode.htm" |
| 124 |
End If |
| 125 |
End If |
| 126 |
|
| 127 |
RasConnect = (errorCode = 0) |
| 128 |
|
| 129 |
End Function |
| 130 |
|
| 131 |
' VpnDial.exe --setup {connection} --user {username} --pass {password} |
| 132 |
Public Function SetupRasEntry(conName As String, phoneBook As String, username As String, password As String) |
| 133 |
|
| 134 |
Dim rdp As RASDIALPARAMS |
| 135 |
Dim result As Long |
| 136 |
|
| 137 |
rdp.dwSize = Len(rdp) + 6 |
| 138 |
ChangeBytes conName, rdp.szEntryName |
| 139 |
|
| 140 |
ChangeBytes username, rdp.szUserName |
| 141 |
ChangeBytes password, rdp.szPassword |
| 142 |
|
| 143 |
' Set User name and password for the connection |
| 144 |
RasSetEntryDialParams phoneBook, rdp, 0 |
| 145 |
|
| 146 |
End Function |