Does anybody have a working code to know if we have access to Internet or not through Cablemodem?
Printable View
Does anybody have a working code to know if we have access to Internet or not through Cablemodem?
probaly not the correct way... but you could ping google
Sending a request to a remote node is really the only reliable
way to know whether you're connected to the internet or not.
Anything else will just check to see if you're on a network.
So you can do as Brandon suggested and ping a URL you know is
likely to be up when connected to the internet, better yet, have a
list of 2 or 3 URL's.
Example:VB Code:
Option Explicit Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" ( _ ByVal sAgent As String, _ ByVal lAccessType As Long, _ ByVal sProxyName As String, _ ByVal sProxyBypass As String, _ ByVal lFlags As Long) As Long Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" ( _ ByVal hInternetSession As Long, _ ByVal sServerName As String, _ ByVal nServerPort As Integer, _ ByVal sUsername As String, _ ByVal sPassword As String, _ ByVal lService As Long, _ ByVal lFlags As Long, _ ByVal lContext As Long) As Long Private Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" ( _ ByVal hHttpSession As Long, _ ByVal sVerb As String, _ ByVal sObjectName As String, _ ByVal sVersion As String, _ ByVal sReferer As String, _ ByVal something As Long, _ ByVal lFlags As Long, _ ByVal lContext As Long) As Long Private Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" ( _ ByVal hHttpRequest As Long, _ ByVal sHeaders As String, _ ByVal lHeadersLength As Long, _ ByVal sOptional As String, _ ByVal lOptionalLength As Long) As Long Private Declare Function InternetCloseHandle Lib "wininet.dll" ( _ ByVal hInet As Long) As Integer Private Const INTERNET_FLAG_RELOAD = &H80000000 Private Const INTERNET_SERVICE_HTTP = 3 Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0 Public Function IsInternetReady() As Boolean Dim lSession As Long Dim lConnect As Long Dim lRequest As Long Dim lResponse As Long Const sURL = "www.microsoft.com" ' Open an Internet Session lSession = InternetOpen("NetReady", _ INTERNET_OPEN_TYPE_PRECONFIG, _ vbNullString, _ vbNullString, _ 0) If lSession = 0 Then Exit Function ' Create a connection lConnect = InternetConnect(lSession, _ sURL, _ 80, _ vbNullString, _ vbNullString, _ INTERNET_SERVICE_HTTP, _ 0, _ 0) ' Generate an HTTP GET request lRequest = HttpOpenRequest(lConnect, _ "GET", _ sURL, _ "HTTP/1.0", _ vbNullString, _ 0, _ INTERNET_FLAG_RELOAD, _ 0) ' Send the request, Response will be non-zero if successful lResponse = HttpSendRequest(lRequest, _ vbNullString, _ Len(vbNullString), _ vbNullString, _ Len(vbNullString)) Call InternetCloseHandle(lSession) Call InternetCloseHandle(lConnect) Call InternetCloseHandle(lRequest) IsInternetReady = lResponse End Function
Thanks, Aaron. It worked!!
Just to add on to what you might already have:
VB Code:
Private Declare Function InetIsOffline Lib "url.dll" (ByVal dwFlags As Long) As Long Private Sub Form_Load() 'KPD-Team 2001 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'InetIsOffline returns 0 if you're connected MsgBox "Are you connected to the internet? " + CStr(CBool(Not (InetIsOffline(0)))), vbInformation End Sub
Pinging to detect an internet connection may not work in some cases if your ISP has disabled that capability. Due to some of the virus attacks of late, I know someone whose ISP disabled the ping capabilities.
Just to clairfy, Aaron young's solution doesn't ping does it... it uses HTTP to verify the connection... yeah?
Either way, I'm using it, thanks :) I think I'll use two sites, google and microsoft
I can use Aaron's code with great success but only on the first time. If I then remove the network connection and test it again, it returns a success value that I am connected to the Internet when in fact I am not.
Is this checking something that is cached?
If this is the case - is there a way to clear the cache before running a check for connectivity?