Library code snippets

Active Internet Connection?

Add a command button to your form, and place the following code in the General Declarations section:

Private Sub Command1_Click()
If ActiveConnection = True Then
  MsgBox "You have an active connection.", vbInformation
Else
  MsgBox "You have no active connections.", vbInformation
End If
End Sub

Next, add the following code to a module

Option Explicit

Public Const ERROR_SUCCESS = 0&
Public Const APINULL = 0&
Public Const HKEY_LOCAL_MACHINE = &H80000002

Public ReturnCode As Long

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long

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

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

Public Function ActiveConnection() 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
    ActiveConnection = False
    lpSubKey = "SystemCurrentControlSetServicesRemoteAccess"
    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
                ActiveConnection = False
            Else
                ActiveConnection = True
            End If
        End If
        RegCloseKey (hKey)
    End If
End Function

Comments

  1. 05 Sep 2005 at 12:14

    Hi all , I m new in this group ! I have some problem related to crystal reports ....How can I add column in crystal report at runtime in VB .NET

  2. 06 Jul 2002 at 15:38

    hehe yeah, it doesn't work

  3. 21 May 2002 at 11:22

    This script is way too long, and doesn't work with LAN connections!

  4. 16 Apr 2002 at 04:58

    This is not to put James down nor is it to offend him.... try my one...


    Article: http://www.developerfusion.com/show/1925/1/


    If you have any troubles post here please.


    Good Luck!

  5. 16 Apr 2002 at 04:46

    Get this script out of here! It doesn't work!

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of Active Internet Connection?.

Leave a comment

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