Library code snippets

Check for 'ActiveConnection' via WinInet

This bit of code will show you how to detect an active connection using the WinInet API.

I have seen many who use the Windows Registry to check for an active connection, this is fine but you also need to code for a Registry Wrapper or use a handy class like VBA's Registry one but this may cost you some size. So a convienent way is to use the winInet API to check for an active connection.

Dump this into a module and when you do something like 'Msgbox Connected()' you will get TRUE is its conencted and FALSE if it isn't.

Option Explicit
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (lpdwFlags As Long, lpszConnectionName As Long, dwNameLen As Long, ByVal dwReserved As Long) As Long

Public Function Connected() As Boolean

   Dim lNameLen            As Long
   Dim lRetVal             As Long
   Dim lConnectionFlags    As Long
   Dim LPTR                As Long
   Dim lNameLenPtr         As Long
   Dim sConnectionName     As String
   
   sConnectionName = Space$(256)
   lNameLen = 256
   LPTR = StrPtr(sConnectionName)
   lNameLenPtr = VarPtr(lNameLen)

   lRetVal = InternetGetConnectedStateEx(lConnectionFlags, ByVal LPTR, ByVal lNameLen, 0&)

   Connected = (lRetVal <> 0)
End Function

Smaller, reliable and most of all its using the Windows API which is far better than searching the Windows Registry for a certain KEY.

Comments

  1. 28 Aug 2005 at 00:50


    ================ Internet ==================
                                               ||
    ================  Cable Modem ============
                                                  ||
    ================ Netgear Router =========
                 ||                              ||                       ||
    ===Laptop(Wireless)====PC1 (LAN)=====PC2 (LAN)===



    If disconnect the cable modem but router is running this program/code still detects as connected to iNet. Only if I disconnect the LAN connection or remove PCMCIA wireless card, program says disconnected.


    I need some code that will find out actual iNet is connected, not just the active connection to a router. An expert idea/opinion is appreciated. Thanks.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Check for 'ActiveConnection' via WinInet.

Leave a comment

Sign in or Join us (it's free).

Thushan Fernando Currently I'm working at Hammond Street Developments Pty Ltd. My time on DeveloperFusion is limited due to workloads, I do however keep a Blog that gets updated fairly regularly with lots of Techno...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!