PDA

Click to See Complete Forum and Search --> : Pls help: Internet connection state


Shafee
Oct 27th, 2000, 12:09 PM
Does anyone know how to get the internet connection state in VB?

PRIVATE1
Oct 28th, 2000, 01:33 PM
Next time if you want a quicker response use "SEARCH" thats where this came from . It was originaly posted by Mathew Gates

To detect Internet connection:



code:--------------------------------------------------------------------------------'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



[]P

marnitzg
Oct 29th, 2000, 12:40 PM
This is smaller and works if the machine was reset while still connected to the internet. The other one returns true even if not connected after this happens. But yes, this comes off the VB-World site!!!!!


Private Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef lpSFlags As Long, _
ByVal dwReserved As Long) As Long

Private Const INTERNET_CONNECTION_MODEM As Long = &H1

Public Function ViaModem() As Boolean

Dim SFlags As Long
'return the flags associated with the connection
Call InternetGetConnectedState(SFlags, 0&)

'True if the Sflags has a modem connection
ViaModem = SFlags And INTERNET_CONNECTION_MODEM

End Function


Private Sub Command1_Click()
If ViaModem Then MsgBox "Connected"
End Sub

Shafee
Oct 30th, 2000, 06:34 AM
Thanks guys.