Solved

Control of Dial-Up internet connection

Posted on 1999-01-06
6
179 Views
Last Modified: 2010-05-03
I want to establish and also terminate a Dial-Up internet connection from within VB. How does this work?

Thanks
0
Comment
Question by:RalfZ
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:waty
Comment Utility
Use the following function :

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


0
 

Author Comment

by:RalfZ
Comment Utility
This is only half the answer. There is still the problem to terminate the established connection.
0
 
LVL 14

Expert Comment

by:waty
Comment Utility
Here is my full module for connections :

Option Explicit

' *** 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
   ' *** 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

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

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

Public Sub HangUpConnection(sISPName As String)
   ' *** 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

Public Sub CreateInternetShortcutOnDesktop(sURLFile As String, sURLTarget As String)
   ' *** Creating Internet Shortcuts
   ' *** A great way of advertising your site is to offer a link from the desktop or Start Menu to your site.
   ' *** To do this, you create a URL file.
   ' *** When this file is double-clicked, it opens up the default browser with the Web Site stored
   ' *** in the file.
   ' *** The structure of the file is as follows.
   ' *** A URL file has the extension .URL, and contains the following text:

   ' *** [InternetShortcut]
   ' *** URL=http://www.geocities.com/ResearchTriangle/6311/

   ' *** EX :
   ' *** sURLFile = "C:\Windows\Desktop\PrintPreview.url"
   ' *** sURLTarget = "http://www.geocities.com/ResearchTriangle/6311/"

   ' *** Declare variables
   Dim nFileNum            As Integer

   On Error Resume Next

   ' *** Initialise Variables
   nFileNum = FreeFile

   ' *** Write the Internet Shortcut file
   Open sURLFile For Output As nFileNum
   Print #nFileNum, "[InternetShortcut]"
   Print #nFileNum, "URL=" & sURLTarget
   Close nFileNum

End Sub

Private Sub PhoneDialer(sNumber As String, sAppName As String, SName As String)
   ' *** Phone dialer
   Dim nRes       As Long
   Dim sTmp       As String

   nRes = tapiRequestMakeCall&(sNumber, sAppName, SName, "")

   If nRes <> 0 Then ' *** Error
      sTmp = "Error connecting to number: "

      Select Case nRes
         Case -2&
            sTmp = sTmp & " 'PhoneDailer not installed?"
         Case -3&
            sTmp = sTmp & "Error : " & CStr(nRes) & "."
      End Select

      MsgBox sTmp

   End If

End Sub

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:RalfZ
Comment Utility
This is only half the answer. There is still the problem to terminate the established connection.
0
 

Author Comment

by:RalfZ
Comment Utility
Sorry waty, this time I wanted to give you the 100 points, something went wrong. Please leave just a dummy-answer so I can give the points to you.

Thanks

Ralf
0
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
Comment Utility
Here is my full module for connections :

Option Explicit

' *** 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
   ' *** 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

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

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

Public Sub HangUpConnection(sISPName As String)
   ' *** 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

Public Sub CreateInternetShortcutOnDesktop(sURLFile As String, sURLTarget As String)
   ' *** Creating Internet Shortcuts
   ' *** A great way of advertising your site is to offer a link from the desktop or Start Menu to your site.
   ' *** To do this, you create a URL file.
   ' *** When this file is double-clicked, it opens up the default browser with the Web Site stored
   ' *** in the file.
   ' *** The structure of the file is as follows.
   ' *** A URL file has the extension .URL, and contains the following text:

   ' *** [InternetShortcut]
   ' *** URL=http://www.geocities.com/ResearchTriangle/6311/

   ' *** EX :
   ' *** sURLFile = "C:\Windows\Desktop\PrintPreview.url"
   ' *** sURLTarget = "http://www.geocities.com/ResearchTriangle/6311/"

   ' *** Declare variables
   Dim nFileNum            As Integer

   On Error Resume Next

   ' *** Initialise Variables
   nFileNum = FreeFile

   ' *** Write the Internet Shortcut file
   Open sURLFile For Output As nFileNum
   Print #nFileNum, "[InternetShortcut]"
   Print #nFileNum, "URL=" & sURLTarget
   Close nFileNum

End Sub

Private Sub PhoneDialer(sNumber As String, sAppName As String, SName As String)
   ' *** Phone dialer
   Dim nRes       As Long
   Dim sTmp       As String

   nRes = tapiRequestMakeCall&(sNumber, sAppName, SName, "")

   If nRes <> 0 Then ' *** Error
      sTmp = "Error connecting to number: "

      Select Case nRes
         Case -2&
            sTmp = sTmp & " 'PhoneDailer not installed?"
         Case -3&
            sTmp = sTmp & "Error : " & CStr(nRes) & "."
      End Select

      MsgBox sTmp

   End If

End Sub

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now