VB and VBA Users Source Code: Determine if a machine is connected to the internet
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determine if a machine is connected to the internet
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, June 05, 2002
Hits:
999
Category:
Internet/Remote Comms
Article:
The following code demonstrates how to determine if a machine is connected to the internet (+ how the machine is connected). Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Boolean Public Enum eConnectionType INTERNET_CONNECTION_MODEM = &H1& INTERNET_CONNECTION_LAN = &H2& INTERNET_CONNECTION_PROXY = &H4& INTERNET_RAS_INSTALLED = &H10& INTERNET_CONNECTION_OFFLINE = &H20& INTERNET_CONNECTION_CONFIGURED = &H40& End Enum 'Purpose : Determines basic information regarding the local machine's internet connection state. 'Inputs : sConnectionName See outputs. ' eType See outputs. 'Inputs : Returns True if connected + ' sConnectionName The name of the internet connection ' eType The type of internet connection (see eConnectionType) 'Author : Andrew Baker 'Date : 25/03/2001 'Notes : Public Property Get InternetConnected(Optional ByRef sConnectionName As String, Optional ByRef eType As eConnectionType) As Boolean Dim sConNameBuffer As String * 513 Dim lPos As Long 'Clear output values sConnectionName = "" eType = 0 'Call API InternetConnected = InternetGetConnectedStateEx(eType, sConNameBuffer, 512, 0&) 'Clean up output lPos = InStr(sConNameBuffer, vbNullChar) If lPos > 0 Then sConnectionName = Left$(sConNameBuffer, lPos - 1) End If End Property 'Test routine Private Sub Form_Load() Dim eType As eConnectionType Dim sName As String 'Determine whether we have a connection: If InternetConnected(sName, eType) Then MsgBox "Connected to internet using '" & sName & "' ", vbInformation Else MsgBox "Not connected to internet ", vbInformation End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder