Solved

Connected to Internet???

Posted on 1999-01-17
1
276 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:carlosvs
1 Comment
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1469109
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)
   '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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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