Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Activating Dialup connection

How can I activate a selected Dial up connection from a VB application (that uses FTP), and disconnect it when the VB program terminates ?
0
nimrodg080397
Asked:
nimrodg080397
  • 3
1 Solution
 
mcriderCommented:
Here you go....


'Global constant declarations
Global Const SUCCESS = 0
Global Const APINULL = 0&
Global Const MaxEntryName = 256
Global Const RAS_MaxPhoneNumber = 128
Global Const MaxCallbackNumber = RAS_MaxPhoneNumber
Global Const UNLEN = 256
Global Const PWLEN = 256
Global Const DNLEN = 18
Global Const NOT_SUPPORTED = 120&
Global Const RAS_MaxIpAddress = 15
Global Const RASP_PppIp = &H8021&

Global Const RAS_MaxDeviceType = 16 ' Self typed
Global Const RAS_MaxDeviceName = 128 ' Self typed
Global Const MAX_PATH = 260

'Declaration for lstrcpy() API
Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
    (lpString1 As Any, ByVal lpString2 As String) As Long

'Declaration for RasDial() API
Declare Function RasDial Lib "rasapi32" Alias "RasDialA" _
    (ByVal lpRasDialExtensions As Long, _
     ByVal lpszPhonebook As String, _
     lprasdialparams As RasDialParams, _
     ByVal dwNotifierType As Long, _
     ByVal lpvNotifier As Long, _
     lphRasConn As Long) As Long

'Declaration for RasHangUp() API
Declare Function RasHangup Lib "rasapi32" Alias "RasHangUpA" _
    (ByVal hRasConn As Long) As Long

'Type declaration for RASDIALPARAM structure
Public Type RasDialParams
    dwSize As Long
    szEntryName(MaxEntryName) As Byte
    szPhoneNumber(RAS_MaxPhoneNumber) As Byte
    szCallbackNumber(MaxCallbackNumber) As Byte
    szUserName(UNLEN) As Byte
    szPassword(PWLEN) As Byte
    szDomain(DNLEN) As Byte
End Type

'RasDial function
Function fcnRasDial(Entryname As String, _
                    PhoneNumber As String, _
                    CallbackNumber As String, _
                    Username As String, _
                    Password As String, _
                    Domain As String, _
                    hRasConn As Long) As Long
    Dim lngnRet As Long
    Dim MyRasDialParams As RasDialParams
    Dim OldMouse
         
    ' Fill in the RASDIALPARAMS structure
    MyRasDialParams.dwSize = LenB(MyRasDialParams)
    lngnRet = lstrcpy(MyRasDialParams.szEntryName(0), Entryname)
    lngnRet = lstrcpy(MyRasDialParams.szPhoneNumber(0), PhoneNumber)
    lngnRet = lstrcpy(MyRasDialParams.szCallbackNumber(0), CallbackNumber)
    lngnRet = lstrcpy(MyRasDialParams.szUserName(0), Username)
    lngnRet = lstrcpy(MyRasDialParams.szPassword(0), Password)
    lngnRet = lstrcpy(MyRasDialParams.szDomain(0), Domain)
         
    OldMouse = Screen.MousePointer
    Screen.MousePointer = vbHourglass
    ' Call RasDial
    fcnRasDial = RasDial(0, vbNullString, _
                         MyRasDialParams, 0, ByVal 0, hRasConn)
    Screen.MousePointer = OldMouse
End Function






If you wish to iterate the current ras connections, use the following:


Public Type RASCONN
    dwSize As Long
    hRasConn As Long
    szEntryName(MaxEntryName + 1) As Byte
    szDeviceType(RAS_MaxDeviceType) As Byte
    szDeviceName(RAS_MaxDeviceName) As Byte
End Type

'Declaration of RasEnumConnections () API
Public Declare Function RasEnumConnections Lib "RasApi32.DLL" Alias "RasEnumConnectionsA" _
        (lpRasconn As Any, _
         lpcb As Long, _
         lpcConnections As Long) As Long

Function EnumConns()
    Dim lRetcode As Long
    Dim lConns As Long
    Dim lSize As Long
    Dim lBuffer As Long
     
    ' Should be enough for everyone... should be improved this
    lSize = 255
     
    ReDim lpRasconn(lSize) As RASCONN
    lpRasconn(0).dwSize = LenB(lpRasconn(0))
     
    ' Calculate the buffer size
    lBuffer = lSize * 412
    ' Get the connections
    lRetcode = RasEnumConnections(lpRasconn(0), lBuffer, lConns)
    If lRetcode <> 0 Then
        ' Handle the rasHangup error
        Err.Raise vbErrorEnumConnections, "EnumConnections", fcnRasErrorString(lRetcode)
    End If
     
    Dim I As Long
    Dim mConn As CConnection
    For I = 0 To lConns - 1
        Set mConn = New CConnection
        With mConn
            .Entryname = fcnTrimNulls(StrConv(lpRasconn(I).szEntryName, vbUnicode))
            .hRasConn = lpRasconn(I).hRasConn
        End With
        mCol.Add mConn, mConn.Entryname
        Set mConn = Nothing
    Next I
End Function



Cheers!
0
 
mcriderCommented:
You can also use the "easy method" of starting dialup session by running the command "Rundll Rnaui.dll,RnaDial <Dialup name>" in a shell... for example:

id=shell("Rundll Rnaui.dll,RnaDial Erol's Internet")

Then use AppActivate and SendKeys to set focus to the "Connect To..." window and send TAB keys to get to the phone number field and ENTER key to start the connection.

Cheers!

0
 
nimrodg080397Author Commented:
Thanks
0
 
mcriderCommented:
You're welcome...  Thanks for the points! Glad I could help!


Cheers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now