看看这个例子
'Example Name:Dial
'This program let you dial to your dial-up connections using whether
'the stored user name and password or  the ones you specifies
'(It use RasDial for dialing)'You need a form with a list,2 textbox and a command buttonOption Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, ByVal pSrc As String, ByVal ByteLen As Long)
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)Const RAS95_MaxEntryName = 256
Const RAS_MaxPhoneNumber = 128
Const RAS_MaxCallbackNumber = RAS_MaxPhoneNumberConst UNLEN = 256
Const PWLEN = 256
Const DNLEN = 12
Private Type RASDIALPARAMS
   dwSize As Long ' 1052
   szEntryName(RAS95_MaxEntryName) As Byte
   szPhoneNumber(RAS_MaxPhoneNumber) As Byte
   szCallbackNumber(RAS_MaxCallbackNumber) As Byte
   szUserName(UNLEN) As Byte
   szPassword(PWLEN) As Byte
   szDomain(DNLEN) As Byte
End TypePrivate Type RASENTRYNAME95
    'set dwsize to 264
    dwSize As Long
    szEntryName(RAS95_MaxEntryName) As Byte
End TypePrivate Declare Function RasDial Lib "rasapi32.dll" Alias "RasDialA" (ByVal lprasdialextensions As Long, ByVal lpcstr As String, ByRef lprasdialparamsa As RASDIALPARAMS, ByVal dword As Long, lpvoid As Any, ByRef lphrasconn As Long) As Long
Private Declare Function RasEnumEntries Lib "rasapi32.dll" Alias "RasEnumEntriesA" (ByVal reserved As String, ByVal lpszPhonebook As String, lprasentryname As Any, lpcb As Long, lpcEntries As Long) As Long
Private Declare Function RasGetEntryDialParams Lib "rasapi32.dll" Alias "RasGetEntryDialParamsA" (ByVal lpcstr As String, ByRef lprasdialparamsa As RASDIALPARAMS, ByRef lpbool As Long) As LongPrivate Function Dial(ByVal Connection As String, ByVal UserName As String, ByVal Password As String) As Boolean
    Dim rp As RASDIALPARAMS, h As Long, resp As Long
    rp.dwSize = Len(rp) + 6
    ChangeBytes Connection, rp.szEntryName
    ChangeBytes "", rp.szPhoneNumber 'Phone number stored for the connection
    ChangeBytes "*", rp.szCallbackNumber 'Callback number stored for the connection
    ChangeBytes UserName, rp.szUserName
    ChangeBytes Password, rp.szPassword
    ChangeBytes "*", rp.szDomain 'Domain stored for the connection
    'Dial
    resp = RasDial(ByVal 0, ByVal 0, rp, 0, ByVal 0, h)   'AddressOf RasDialFunc
    Dial = (resp = 0)
End FunctionPrivate Function ChangeToStringUni(Bytes() As Byte) As String
    'Changes an byte array  to a Visual Basic unicode string
    Dim temp As String
    temp = StrConv(Bytes, vbUnicode)
    ChangeToStringUni = Left(temp, InStr(temp, Chr(0)) - 1)
End FunctionPrivate Function ChangeBytes(ByVal str As String, Bytes() As Byte) As Boolean
    'Changes a Visual Basic unicode string to an byte array
    'Returns True if it truncates str
    Dim lenBs As Long 'length of the byte array
    Dim lenStr As Long 'length of the string
    lenBs = UBound(Bytes) - LBound(Bytes)
    lenStr = LenB(StrConv(str, vbFromUnicode))
    If lenBs > lenStr Then
        CopyMemory Bytes(0), str, lenStr
        ZeroMemory Bytes(lenStr), lenBs - lenStr
    ElseIf lenBs = lenStr Then
        CopyMemory Bytes(0), str, lenStr
    Else
        CopyMemory Bytes(0), str, lenBs 'Queda truncado
        ChangeBytes = True
    End If
End FunctionPrivate Sub Command1_Click()
    Dial List1.Text, Text1, Text2
End Sub
Private Sub List1_Click()
    Dim rdp As RASDIALPARAMS, t As Long
    rdp.dwSize = Len(rdp) + 6
    ChangeBytes List1.Text, rdp.szEntryName
    'Get User name and password for the connection
    t = RasGetEntryDialParams(List1.Text, rdp, 0)
    If t = 0 Then
        Text1 = ChangeToStringUni(rdp.szUserName)
        Text2 = ChangeToStringUni(rdp.szPassword)
    End If
End SubPrivate Sub Form_Load()
    'example created by Daniel Kaufmann ([email protected])
    'load the connections
    Text2.PasswordChar = "*"
    Command1.Caption = "Dial"
    Dim s As Long, l As Long, ln As Long, a$
    ReDim r(255) As RASENTRYNAME95
    
    r(0).dwSize = 264
    s = 256 * r(0).dwSize
    l = RasEnumEntries(vbNullString, vbNullString, r(0), s, ln)
    For l = 0 To ln - 1
        a$ = StrConv(r(l).szEntryName(), vbUnicode)
        List1.AddItem Left$(a$, InStr(a$, Chr$(0)) - 1)
    Next
    If List1.ListCount > 0 Then
        List1.ListIndex = 0
        List1_Click
    End If
End Sub

解决方案 »

  1.   

    我用的是这个例子,你的例子我也用过,Dial正常返回,但没有实际作用,我的OS是2K。
    '自动拨接(Win95 4, 5 个叁数不传,或为vbNullString)
    Public Function DialUp(ByVal EntryName As String, ByVal UserN As String, _
        ByVal Pwd As String, Optional ByVal PhoneBook As String, Optional sDomain As String) As Long
    Dim RasDialPara As RASDIALPARAMS
    Dim bya() As Byte, di As Long
    Dim len5 As Long, i As Long
    Dim hRasConn As Long
    len5 = LenB(RasDialPara)
    Debug.Print
    i = (4 - (len5 Mod 4)) Mod 4
    RasDialPara.dwSize = len5 + i '1052
    'Debug.Print RasDialPara.dwSize
    bya = StrConv(EntryName, vbNarrow) + ChrB(0)
    Call CopyByte(RasDialPara.szEntryName, bya)bya = StrConv(UserN, vbNarrow) + ChrB(0)
    Call CopyByte(RasDialPara.szUserName, bya)bya = StrConv(Pwd, vbNarrow) + ChrB(0)
    Call CopyByte(RasDialPara.szPassword, bya)bya = StrConv(sDomain, vbNarrow) + ChrB(0)
    Call CopyByte(RasDialPara.szDomain, bya)
    '若使用以下CallBack function的方式,则RasDial()不等连接成功或失败便结束。
    di = RasDial(ByVal 0&, PhoneBook, RasDialPara, 0&, AddressOf RasDialFunc, hRasConn)'若第二、三个叁数都是0则,RasDial会等连接成功或失败後才执行下一行指令
    'di = RasDial(0, PhoneBook, RasDialPara, 0, 0, hRasConn)
    Debug.Print 1234
    If di = 0 Then
       DialUp = hRasConn
    Else
       DialUp = 0
       Dim str5 As String
       str5 = String(255, Chr(0))
       Call RasGetErrorString(di, str5, 256)
       MsgBox Left(str5, InStr(1, str5, Chr(0)) - 1), vbCritical
       Call HangUp(hRasConn)
    End If
    End FunctionPublic Sub RasDialFunc(ByVal unMsg As Long, _
                           ByVal ConnState As Long, _
                           ByVal dwError As Long)
    Debug.Print 1212
    If ConnState = &H2000 Then
       ' Connect Complete
    End IfDebug.Print unMsg, ConnState
    End Sub
    '取消拨接
    Public Function HangUp(ByVal hconn As Long) As Boolean
    Dim st As Long, len5 As Long
    Dim i As Long, ConStatus  As RASCONNSTATUS
    st = RasHangUp(hconn)
    len5 = LenB(ConStatus)
    i = (4 - (len5 Mod 4)) Mod 4
    ConStatus.dwSize = len5 + i
    Do While True
      Call Sleep(0)
      i = RasGetConnectStatus(hconn, ConStatus)
      If i = ERROR_INVALID_HANDLE Then
         Exit Do
      End If
    Loop
    If st = 0 Then
       HangUp = True
    Else
       HangUp = False
    End If
    End Function
    '取得连接状态
    Public Function GetConnectStatus(ByVal hocnn As Long) As Long
    Dim i As Long, ConStatus  As RASCONNSTATUS
    Dim len5 As Long
    len5 = LenB(ConStatus)
    i = (4 - (len5 Mod 4)) Mod 4
    ConStatus.dwSize = len5 + i
    i = RasGetConnectStatus(hconn, ConStatus)
    If i = 0 Then
       GetConnectStatus = ConStatus.RasConnState
    Else
       GetConnectStatus = -1
    End If
    End Function
    Private Sub CopyByte(dest() As Byte, sour() As Byte)
    Dim sourL As Long, sourU As Long
    Dim destL As Long, destU As Long, i As Long, j As Long
    sourL = LBound(sour)
    sourU = UBound(sour)
    destL = LBound(dest)
    destU = UBound(dest)
    j = 0
    For i = sourL To sourU
        dest(destL + j) = sour(i)
        j = j + 1
        If j >= (destU - destL) + 1 Then
           Exit For
        End If
    Next i
    End Sub