Link to home
Start Free TrialLog in
Avatar of pgg
pgg

asked on

Start and stop the network dialer

How do I automatically starts the network dialer with a predefined profile and how do I close it when I am ready ?
Avatar of Mirkwood
Mirkwood

Shell "rundll32.exe rnaui.dll,RnaDial " & sConnectionName, 1

Avatar of pgg

ASKER

Please, little more information please. I will test it, but:

is there any difference in open / close the connection ?

connection name, is it just the name with/without the extension ?

what does number 1 means, and what other alternatives exist ?

Thanks !

' *** Used to know all active connections
Private Const ERROR_SUCCESS = 0&
Private Const APINULL = 0&
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

' *** disconnect from the internet using VB
Private Const RAS_MAXENTRYNAME As Integer = 256
Private Const RAS_MAXDEVICETYPE As Integer = 16
Private Const RAS_MAXDEVICENAME As Integer = 128
Private Const RAS_RASCONNSIZE As Integer = 412

Private Type RasEntryName
   dwSize                          As Long
   szEntryName(RAS_MAXENTRYNAME)   As Byte
End Type

Private Type RasConn
   dwSize                          As Long
   hRasConn                        As Long
   szEntryName(RAS_MAXENTRYNAME)   As Byte
   szDeviceType(RAS_MAXDEVICETYPE) As Byte
   szDeviceName(RAS_MAXDEVICENAME) As Byte
End Type

Private Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (lpRasConn As Any, lpcb As Long, lpcConnections As Long) As Long
Private Declare Function RASHangUp Lib "rasapi32.dll" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long

' *** Phone dialer
Private Declare Function tapiRequestMakeCall& Lib "TAPI32.DLL" (ByVal DestAdress$, ByVal AppName$, ByVal CalledParty$, ByVal Comment$)

Public Function ActiveConnection() As Boolean
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/ 
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 18/01/99
   ' * Time             : 09:41
   ' * Module Name      : Internet_Module
   ' * Module Filename  : Internet.bas
   ' * Procedure Name   : ActiveConnection
   ' * Parameters       :
   ' **********************************************************************
   ' * Comments         :
   ' *** How can I detect if there is an active internet connection?
   ' *** For programs that rely on connecting to the internet,
   ' *** it is very useful to know whether or not the computer has an active connection.
   ' *** Whenever Windows logs on to a dial-up connection, it changes a value in the registry.

   ' * Here is an example of how to use the ActiveConnection function.
   '
   ' * If ActiveConnection = True Then
   ' *    Call MsgBox("You have an active connection.", vbInformation)
   ' * Else
   ' *    Call MsgBox("You have no active connections.", vbInformation)
   ' * End If
   ' *
   ' *
   ' **********************************************************************

   Dim hKey             As Long
   Dim lpSubKey         As String
   Dim phkResult        As Long
   Dim lpValueName      As String
   Dim lpReserved       As Long
   Dim lpType           As Long
   Dim lpData           As Long
   Dim lpcbData         As Long
   Dim nRet             As Long

   ActiveConnection = False
   lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
   nRet = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)

   If nRet = ERROR_SUCCESS Then
      hKey = phkResult
      lpValueName = "Remote Connection"
      lpReserved = APINULL
      lpType = APINULL
      lpData = APINULL
      lpcbData = APINULL
      nRet = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, ByVal lpData, lpcbData)
      lpcbData = Len(lpData)
      nRet = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)

      If nRet = ERROR_SUCCESS Then
         If lpData = 0 Then
            ActiveConnection = False
         Else
            ActiveConnection = True
         End If
      End If

      RegCloseKey (hKey)
   End If

End Function



Disconnect :
' *** Used to know all active connections
Private Const ERROR_SUCCESS = 0&
Private Const APINULL = 0&
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

' *** disconnect from the internet using VB
Private Const RAS_MAXENTRYNAME As Integer = 256
Private Const RAS_MAXDEVICETYPE As Integer = 16
Private Const RAS_MAXDEVICENAME As Integer = 128
Private Const RAS_RASCONNSIZE As Integer = 412

Private Type RasEntryName
   dwSize                          As Long
   szEntryName(RAS_MAXENTRYNAME)   As Byte
End Type

Private Type RasConn
   dwSize                          As Long
   hRasConn                        As Long
   szEntryName(RAS_MAXENTRYNAME)   As Byte
   szDeviceType(RAS_MAXDEVICETYPE) As Byte
   szDeviceName(RAS_MAXDEVICENAME) As Byte
End Type

Private Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (lpRasConn As Any, lpcb As Long, lpcConnections As Long) As Long
Private Declare Function RASHangUp Lib "rasapi32.dll" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long

' *** Phone dialer
Private Declare Function tapiRequestMakeCall& Lib "TAPI32.DLL" (ByVal DestAdress$, ByVal AppName$, ByVal CalledParty$, ByVal Comment$)

Public Sub HangUpConnection(sISPName As String)
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/ 
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 18/01/99
   ' * Time             : 09:43
   ' * Module Name      : Internet_Module
   ' * Module Filename  : Internet.bas
   ' * Procedure Name   : HangUpConnection
   ' * Parameters       :
   ' *                    sISPName As String
   ' **********************************************************************
   ' * Comments         :
   ' *** Disconnect from the internet using VB?
   ' *** If you want to terminate all connections to the internet using Visual Basic,
   ' *** you can use the Remote Access Services Hangup function.
   ' *
   ' *
   ' **********************************************************************

   Dim I                As Long
   Dim lpRasConn(255)   As RasConn
   Dim lpcb             As Long
   Dim lpcConnections   As Long
   Dim hRasConn         As Long
   Dim nRet             As Long

   lpRasConn(0).dwSize = RAS_RASCONNSIZE
   lpcb = RAS_MAXENTRYNAME * lpRasConn(0).dwSize
   lpcConnections = 0
   nRet = RasEnumConnections(lpRasConn(0), lpcb, lpcConnections)

   If nRet = ERROR_SUCCESS Then
      For I = 0 To lpcConnections - 1
         If Trim(ByteToString(lpRasConn(I).szEntryName)) = Trim(sISPName) Then
            hRasConn = lpRasConn(I).hRasConn
            nRet = RASHangUp(ByVal hRasConn)
         End If
      Next
   End If

End Sub

Private Function ByteToString(bytString() As Byte) As String
   Dim I As Integer

   ByteToString = "" 
   I = 0
   While bytString(I) = 0&
      ByteToString = ByteToString & Chr(bytString(I))
      I = I + 1
   Wend

End Function

Start :

Public Sub StartDialUp(sConnectionName As String)
   ' #VBIDEUtils#************************************************************
   ' * Programmer Name  : Waty Thierry
   ' * Web Site         : www.geocities.com/ResearchTriangle/6311/ 
   ' * E-Mail           : waty.thierry@usa.net
   ' * Date             : 2/10/98
   ' * Time             : 09:45
   ' * Module Name      : Internet_Module
   ' * Module Filename  : Internet.bas
   ' * Procedure Name   : StartDialUp
   ' * Parameters       :
   ' *                    sConnectionName As String
   ' **********************************************************************
   ' * Comments         : To start up a dial-up networking connection

   ' *
   ' *
   ' **********************************************************************

   Dim Res        As Variant

   Res = Shell("rundll32.exe rnaui.dll,RnaDial " & sConnectionName, 1)

End Sub

ASKER CERTIFIED SOLUTION
Avatar of Mirkwood
Mirkwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgg

ASKER

Takes some time to understand it, but a answer like this must work, so therefore - thanks a lot !