VB icon

a Better way of testing if connected to the NET

Email
Submitted on: 1/4/2015 10:13:00 AM
By: Half-Dead (from psc cd)  
Level: Beginner
User Rating: By 14 Users
Compatibility: VB 5.0, VB 6.0
Views: 1166
 
     Ok i had previously posted the Improper way of doin a test for fun :) I just ran across this code on MVPS.ORG This code uses the wininet.dll VIA API calls, and it will tell you if you are connected and wether you are connected via LAN, modem, or proxy ! I had to post this up since it seems many ppl are lookin for somethin like this.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: a Better way of testing if connected to the NET
' Description:Ok i had previously posted the Improper way of doin a test for fun :) I just ran across this code on MVPS.ORG
This code uses the wininet.dll VIA API calls, and it will tell you if you are connected and wether you are connected via LAN, modem, or proxy !
I had to post this up since it seems many ppl are lookin for somethin like this.
' By: Half-Dead (from psc cd)
'**************************************

'This code was copied from http://www.mvps.org/vbnet/ ! Go checkem out
'START .BAS MODULE CODE
Option Explicit
Public Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef lpdwFlags As Long, _
ByVal dwReserved As Long) As Long
'Local system uses a modem to connect to the Internet.
Public Const INTERNET_CONNECTION_MODEM As Long = &H1
'Local system uses a LAN to connect to the Internet.
Public Const INTERNET_CONNECTION_LAN As Long = &H2
'Local system uses a proxy server to connect to the Internet.
Public Const INTERNET_CONNECTION_PROXY As Long = &H4
'No longer used.
Public Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8
Public Const INTERNET_RAS_INSTALLED As Long = &H10
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
'InternetGetConnectedState wrapper functions
Public Function IsNetConnectViaLAN() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a LAN connection
IsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LAN
End Function
Public Function IsNetConnectViaModem() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a modem connection
IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEM
End Function
Public Function IsNetConnectViaProxy() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the flags indicate a proxy connection
IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXY
End Function
Public Function IsNetConnectOnline() As Boolean
'no flags needed here - the API returns True
'if there is a connection of any type
IsNetConnectOnline = InternetGetConnectedState(0&, 0&)
End Function
Public Function IsNetRASInstalled() As Boolean
Dim dwflags As Long
'pass an empty varialbe into which the API will
'return the flags associated with the connection
Call InternetGetConnectedState(dwflags, 0&)
'return True if the falgs include RAS installed
IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLED
End Function
Public Function GetNetConnectString() As String
Dim dwflags As Long
Dim msg As String
'build a string for display
If InternetGetConnectedState(dwflags, 0&) Then
If dwflags And INTERNET_CONNECTION_CONFIGURED Then
msg = msg & "You have a network connection configured." & vbCrLf
End If
If dwflags And INTERNET_CONNECTION_LAN Then
msg = msg & "The local system connects to the Internet via a LAN"
End If
If dwflags And INTERNET_CONNECTION_PROXY Then
msg = msg & ", and uses a proxy server. "
Else: msg = msg & "."
End If
If dwflags And INTERNET_CONNECTION_MODEM Then
msg = msg & "The local system uses a modem to connect to the Internet. "
End If
If dwflags And INTERNET_CONNECTION_OFFLINE Then
msg = msg & "The connection is currently offline. "
End If
If dwflags And INTERNET_CONNECTION_MODEM_BUSY Then
msg = msg & "The local system's modem is busy with a non-Internet connection. "
End If
If dwflags And INTERNET_RAS_INSTALLED Then
msg = msg & "Remote Access Services are installed on this system."
End If
Else
msg = "Not connected to the internet now."
End If
GetNetConnectString = msg
End Function
' END MODULE CODE
'##############################
'START FORM CODE
Option Explicit
' Put 6 textboxes and 1 Command button and fire it up !
Private Sub Command1_Click()
Text1 = IsNetConnectViaLAN()
Text2 = IsNetConnectViaModem()
Text3 = IsNetConnectViaProxy()
Text4 = IsNetConnectOnline()
Text5 = IsNetRASInstalled()
Text6 = GetNetConnectString()
End Sub


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.