Link to home
Start Free TrialLog in
Avatar of Goob123
Goob123

asked on

Determining if I am connected to the internet

From VB, how can I determine if I am connected to the internet?  

I tried "InternetGetConnectedState" and that works fine except for AOL.

I was thinking about pinging some IP address and if I get a reply then I know I am connected.  But, is there some sort of api I can use to ping an IP.  Or is there a better way to find out if I am connected..
Avatar of Dr. Kamal Mehdi
Dr. Kamal Mehdi
Flag of Greece image

Try this code:

   ' Registry APIs.
   Public Const HKEY_CLASSES_ROOT = &H80000000
   Public Const HKEY_CURRENT_USER = &H80000001
   Public Const HKEY_LOCAL_MACHINE = &H80000002
   Public Const HKEY_USERS = &H80000003
   Public Const HKEY_PERFORMANCE_DATA = &H80000004
   Public Const HKEY_CURRENT_CONFIG = &H80000005
   Public Const HKEY_DYN_DATA = &H80000006
   Public Const ERROR_SUCCESS = 0&
   Public Const APINULL = 0&
   Public Const MAX_STRING_LENGTH As Integer = 256


   Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long


   Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
       ' RegQueryValueEx: If you declare the lpData parameter as String,
       '    
       ' you must pass it By Value.


   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
       ' Remote Access Services (RAS) APIs.
       Public Const RAS_MAXENTRYNAME As Integer = 256
       Public Const RAS_MAXDEVICETYPE As Integer = 16
       Public Const RAS_MAXDEVICENAME As Integer = 128
       Public Const RAS_RASCONNSIZE As Integer = 412

 
   Public Function Connected_To_ISP() As Boolean


       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
       Connected_To_ISP = False
       lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
       ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)


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


           If ReturnCode = ERROR_SUCCESS Then

               If lpData = 0 Then
                   ' Not Connected
                   MsgBox "Not Connected To ISP."
               Else
                   ' Connected
                   Connected_To_ISP = True
                   MsgBox "Currently Connected To ISP."
               End If

           End If

           RegCloseKey (hKey)
       End If

   End Function


Regards.
Avatar of Goob123
Goob123

ASKER

Sorry but this did not work.  I did run a registry monitor on AOL to see what it writes out and there was nothing usefull written to tell me it was connected.
ASKER CERTIFIED SOLUTION
Avatar of winwiz
winwiz

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
Try

If Winsock1.LocalIP = "127.0.0.1" Then
    Text1.Text = "Not connected"
Else
    Text1.Text = "Connected"
End If

Avatar of Goob123

ASKER

Great thanks