Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Connected to Internet???

How can I make my app know that I am connected to Internet?
if I use Winsock.Connect and I am not connect to Internet
it open the Dial Up. I need to know without a Connect Command.
1 Solution
Use the ActiveConnection function :

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

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


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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