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 |
|
52 |
Private Function Dial(ByVal Connection As String, ByVal phoneBook As String, ByVal username As String, ByVal password As String) As Integer |
53 |
|
54 |
Dim rp As RASDIALPARAMS, h As Long, resp As Long |
55 |
rp.dwSize = Len(rp) + 6 |
56 |
ChangeBytes Connection, rp.szEntryName |
57 |
ChangeBytes "", rp.szPhoneNumber 'Phone number stored for the connection |
58 |
ChangeBytes "*", rp.szCallbackNumber 'Callback number stored for the connection |
59 |
ChangeBytes username, rp.szUserName |
60 |
ChangeBytes password, rp.szPassword |
61 |
ChangeBytes "*", rp.szDomain 'Domain stored for the connection |
62 |
|
63 |
'Dial |
64 |
resp = RasDial(ByVal 0, ByVal phoneBook, rp, 0, ByVal 0, h) 'AddressOf RasDialFunc |
65 |
Dial = resp |
66 |
|
67 |
End Function |
68 |
|
69 |
Private Function ChangeToStringUni(Bytes() As Byte) As String |
70 |
'Changes an byte array to a Visual Basic unicode string |
71 |
Dim temp As String |
72 |
temp = StrConv(Bytes, vbUnicode) |
73 |
ChangeToStringUni = Left(temp, InStr(temp, Chr(0)) - 1) |
74 |
End Function |
75 |
|
76 |
Private Function ChangeBytes(ByVal str As String, ByRef Bytes() As Byte) As Boolean |
77 |
'Changes a Visual Basic unicode string to an byte array |
78 |
'Returns True if it truncates str |
79 |
Dim lenBs As Long 'length of the byte array |
80 |
Dim lenStr As Long 'length of the string |
81 |
lenBs = UBound(Bytes) - LBound(Bytes) |
82 |
lenStr = LenB(StrConv(str, vbFromUnicode)) |
83 |
If lenBs > lenStr Then |
84 |
CopyMemory Bytes(0), str, lenStr |
85 |
ZeroMemory Bytes(lenStr), lenBs - lenStr |
86 |
ElseIf lenBs = lenStr Then |
87 |
CopyMemory Bytes(0), str, lenStr |
88 |
Else |
89 |
CopyMemory Bytes(0), str, lenBs 'Queda truncado |
90 |
ChangeBytes = True |
91 |
End If |
92 |
End Function |
93 |
|
94 |
|
95 |
|
96 |
Public Function RasConnect(conName As String, phoneBook As String) As Boolean |
97 |
|
98 |
Dim rdp As RASDIALPARAMS, t As Long |
99 |
Dim username As String, password As String |
100 |
|
101 |
rdp.dwSize = Len(rdp) + 6 |
102 |
ChangeBytes conName, rdp.szEntryName |
103 |
|
104 |
' Get User name and password for the connection |
105 |
t = RasGetEntryDialParams(phoneBook, rdp, 0) |
106 |
If t = 0 Then |
107 |
username = ChangeToStringUni(rdp.szUserName) |
108 |
password = ChangeToStringUni(rdp.szPassword) |
109 |
End If |
110 |
|
111 |
Dim errorCode As Integer |
112 |
errorCode = Dial(conName, phoneBook, username, password) |
113 |
|
114 |
If (errorCode <> 0) Then |
115 |
MsgBox "Error-Code: " & errorCode & vbCrLf & "http://www.admins-tipps.net/glossar/ras_errorcode.htm" |
116 |
End If |
117 |
|
118 |
RasConnect = (errorCode = 0) |
119 |
|
120 |
End Function |
121 |
|