PDA

Click to See Complete Forum and Search --> : Detecting a connection


Spacetribe
Oct 16th, 2000, 06:21 AM
Hi friends.
I'm making a program that calculates the time i'm
online. My connection is via modem.
How do I detect when someone has made a dail-up connection
to the net? / mj81@spray.se

Oct 16th, 2000, 06:45 AM
To detect Internet connection:

'In a module

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 Const ERROR_SUCCESS As Long = 0
Public Const APINULL As Long = 0
Public Const HKEY_LOCAL_MACHINE As Long = &H80000002

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
Dim ReturnCode As Long

ActiveConnection = 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
ActiveConnection = False
Else
ActiveConnection = True
End If
End If

RegCloseKey (hKey)
End If

End Function


Usage

'In form1's Timer:


Private Sub Timer1_Timer()
If ActiveConnection Then
Caption = "Internet Connection Detected!"
Else
Caption = "No Internet Connection Detected!"
End Sub