473,416 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Easier, more straightforward way for VBA to determine if live connection to web exists?

MLH
I have been using variations of the following to determine whether
a functional connection to internet exists. I'm thinking of replacing
it with a simple PING, but I've never seen any code for that. What
is your opinion on the alternative strategy? Have you ever tried it?

On Error GoTo Err_Handler
Dim msXML As Object, strPageContent As String, MyURL As String

10 Set msXML = CreateObject("Microsoft.XMLHTTP")
20 MyURL =
"http://cgi1.SomeKnownWebsite.com/aw-cgi/eSAPI.dll?TShare&hc=1&hm=td.s5ddl7437"
30 msXML.Open "GET", MyURL, False
40 msXML.Send

.... blah blah blah ...

Err_Handler:
If Err = -2146697211 Then

.... blah blah blah ...
Nov 23 '05 #1
7 4387
There's an API call named InternetAttemptConnect that returns a 0 or 1
depending on whether a live connection to the Internet is available.
The entire call declaration is as follows:

Private Declare Function InternetAttemptConnect Lib "Wininet.dll"
(ByVal dwReserved As Long) As Long

Then in code you'll have:
Dim retVal as Long
retVal = InternetAttemptConnect(1)

retVal will return 1 or 0, 1 meaning a connection exists.

Other API calls to consider are InternetCheckConnection and
InternetGetConnectedState.

Nov 23 '05 #2
MLH
That's PERFECT, Steve. Thx a bunch.
Can't wait to give it a try.
Nov 23 '05 #3
Steve wrote:
There's an API call named InternetAttemptConnect that returns a 0 or 1
depending on whether a live connection to the Internet is available.
The entire call declaration is as follows:

Private Declare Function InternetAttemptConnect Lib "Wininet.dll"
(ByVal dwReserved As Long) As Long

Then in code you'll have:
Dim retVal as Long
retVal = InternetAttemptConnect(1)

retVal will return 1 or 0, 1 meaning a connection exists.

Other API calls to consider are InternetCheckConnection and
InternetGetConnectedState.


I found documentation for InternetAttemptConnect at:

http://msdn.microsoft.com/library/de...mptconnect.asp

and in Win32api.apv from the API Viewer at:

http://www.activevb.de/rubriken/apiv...viewereng.html

There doesn't seem to be any documentation for this function in the
Win32 Help file. The top link directed me to wininet.h. Where can I
find this and other header files for API functions?

James A. Fortune

Nov 23 '05 #4
I don't think is what you want, but it's what I would think about
using.

http://www.programmershelp.co.uk/showcode.php?e=341

Nov 23 '05 #5
I found your question rather interesting. This is where I am at the
present time. The code is copied from MSDN. It requires, AFAICT, that
the address be in the byte.byte.byte.byte format. One place you can
convert, say FFDBA.com to 64.79.161.45 is
http://www.hcidata.com/host2ip.htm.
Option Explicit

'Icmp constants converted from
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp

Private Const ICMP_SUCCESS As Long = 0
Private Const ICMP_STATUS_BUFFER_TO_SMALL = 11001
'Buffer Too Small
Private Const ICMP_STATUS_DESTINATION_NET_UNREACH = 11002
'Destination Net Unreachable
Private Const ICMP_STATUS_DESTINATION_HOST_UNREACH = 11003
'Destination Host Unreachable
Private Const ICMP_STATUS_DESTINATION_PROTOCOL_UNREACH = 11004
'Destination Protocol Unreachable
Private Const ICMP_STATUS_DESTINATION_PORT_UNREACH = 11005
'Destination Port Unreachable
Private Const ICMP_STATUS_NO_RESOURCE = 11006 'No
Resources
Private Const ICMP_STATUS_BAD_OPTION = 11007
'Bad Option
Private Const ICMP_STATUS_HARDWARE_ERROR = 11008
'Hardware Error
Private Const ICMP_STATUS_LARGE_PACKET = 11009
'Packet Too Big
Private Const ICMP_STATUS_REQUEST_TIMED_OUT = 11010
'Request Timed Out
Private Const ICMP_STATUS_BAD_REQUEST = 11011
'Bad Request
Private Const ICMP_STATUS_BAD_ROUTE = 11012
'Bad Route
Private Const ICMP_STATUS_TTL_EXPIRED_TRANSIT = 11013
'TimeToLive Expired Transit
Private Const ICMP_STATUS_TTL_EXPIRED_REASSEMBLY = 11014
'TimeToLive Expired Reassembly
Private Const ICMP_STATUS_PARAMETER = 11015
'Parameter Problem
Private Const ICMP_STATUS_SOURCE_QUENCH = 11016
'Source Quench
Private Const ICMP_STATUS_OPTION_TOO_BIG = 11017
'Option Too Big
Private Const ICMP_STATUS_BAD_DESTINATION = 11018
'Bad Destination
Private Const ICMP_STATUS_NEGOTIATING_IPSEC = 11032
'Negotiating IPSEC
Private Const ICMP_STATUS_GENERAL_FAILURE = 11050
'General Failure

Public Const WINSOCK_ERROR = "Windows Sockets not responding
correctly."
Public Const INADDR_NONE As Long = &HFFFFFFFF
Public Const WSA_SUCCESS = 0
Public Const WS_VERSION_REQD As Long = &H101

'Clean up sockets.
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512

Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long

'Open the socket connection.
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal
wVersionRequired As Long, lpWSADATA As WSADATA) As Long

'Create a handle on which Internet Control Message Protocol (ICMP)
requests can be issued.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmpcreatefile.asp
Private Declare Function IcmpCreateFile Lib "icmp.dll" () As Long

'Convert a string that contains an (Ipv4) Internet Protocol dotted
address into a correct address.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/wsapiref_4esy.asp
Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As
String) As Long

'Close an Internet Control Message Protocol (ICMP) handle that
IcmpCreateFile opens.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmpclosehandle.asp

Private Declare Function IcmpCloseHandle Lib "icmp.dll" (ByVal
IcmpHandle As Long) As Long

'Information about the Windows Sockets implementation
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
Private Type WSADATA
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 256) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Long
iMaxUDPDG As Long
lpVendorInfo As Long
End Type

'Send an Internet Control Message Protocol (ICMP) echo request, and
then return one or more replies.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcetcpip/htm/cerefIcmpSendEcho.asp
Private Declare Function IcmpSendEcho Lib "icmp.dll" _
(ByVal IcmpHandle As Long, _
ByVal DestinationAddress As Long, _
ByVal RequestData As String, _
ByVal RequestSize As Long, _
ByVal RequestOptions As Long, _
ReplyBuffer As ICMP_ECHO_REPLY, _
ByVal ReplySize As Long, _
ByVal Timeout As Long) As Long

'This structure describes the options that will be included in the
header of an IP packet.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcetcpip/htm/cerefIP_OPTION_INFORMATION.asp
Private Type IP_OPTION_INFORMATION
Ttl As Byte
Tos As Byte
Flags As Byte
OptionsSize As Byte
OptionsData As Long
End Type

'This structure describes the data that is returned in response to an
echo request.
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_icmp_echo_reply.asp
Public Type ICMP_ECHO_REPLY
address As Long
Status As Long
RoundTripTime As Long
DataSize As Long
Reserved As Integer
ptrData As Long
Options As IP_OPTION_INFORMATION
Data As String * 250
End Type

'-- Ping a string representation of an IP address.
' -- Return a reply.
' -- Return long code.
Public Function ping(sAddress As String, Reply As ICMP_ECHO_REPLY) As
Long

Dim hIcmp As Long
Dim lAddress As Long
Dim lTimeOut As Long
Dim StringToSend As String

'Short string of data to send
StringToSend = "hello"

'ICMP (ping) timeout
lTimeOut = 1000 'ms

'Convert string address to a long representation.
lAddress = inet_addr(sAddress)

If (lAddress <> -1) And (lAddress <> 0) Then

'Create the handle for ICMP requests.
hIcmp = IcmpCreateFile()

If hIcmp Then
'Ping the destination IP address.
Call IcmpSendEcho(hIcmp, lAddress, StringToSend,
Len(StringToSend), 0, Reply, Len(Reply), lTimeOut)

'Reply status
ping = Reply.Status

'Close the Icmp handle.
IcmpCloseHandle hIcmp
Else
Debug.Print "failure opening icmp handle."
ping = -1
End If
Else
ping = -1
End If

End Function

'Clean up the sockets.
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
Public Sub SocketsCleanup()

WSACleanup

End Sub

'Get the sockets ready.
'http://support.microsoft.com/default.aspx?scid=kb;EN-US;q154512
Public Function SocketsInitialize() As Boolean

Dim WSAD As WSADATA

SocketsInitialize = WSAStartup(WS_VERSION_REQD, WSAD) = ICMP_SUCCESS

End Function

'Convert the ping response to a message that you can read easily from
constants.
'For more information about these constants, visit the following
Microsoft Web site:
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp

Public Function EvaluatePingResponse(PingResponse As Long) As String

Select Case PingResponse

'Success
Case ICMP_SUCCESS: EvaluatePingResponse = "Success!"

'Some error occurred
Case ICMP_STATUS_BUFFER_TO_SMALL: EvaluatePingResponse = "Buffer
Too Small"
Case ICMP_STATUS_DESTINATION_NET_UNREACH: EvaluatePingResponse =
"Destination Net Unreachable"
Case ICMP_STATUS_DESTINATION_HOST_UNREACH: EvaluatePingResponse =
"Destination Host Unreachable"
Case ICMP_STATUS_DESTINATION_PROTOCOL_UNREACH: EvaluatePingResponse =
"Destination Protocol Unreachable"
Case ICMP_STATUS_DESTINATION_PORT_UNREACH: EvaluatePingResponse =
"Destination Port Unreachable"
Case ICMP_STATUS_NO_RESOURCE: EvaluatePingResponse = "No Resources"
Case ICMP_STATUS_BAD_OPTION: EvaluatePingResponse = "Bad Option"
Case ICMP_STATUS_HARDWARE_ERROR: EvaluatePingResponse = "Hardware
Error"
Case ICMP_STATUS_LARGE_PACKET: EvaluatePingResponse = "Packet Too
Big"
Case ICMP_STATUS_REQUEST_TIMED_OUT: EvaluatePingResponse = "Request
Timed Out"
Case ICMP_STATUS_BAD_REQUEST: EvaluatePingResponse = "Bad Request"
Case ICMP_STATUS_BAD_ROUTE: EvaluatePingResponse = "Bad Route"
Case ICMP_STATUS_TTL_EXPIRED_TRANSIT: EvaluatePingResponse =
"TimeToLive Expired Transit"
Case ICMP_STATUS_TTL_EXPIRED_REASSEMBLY: EvaluatePingResponse =
"TimeToLive Expired Reassembly"
Case ICMP_STATUS_PARAMETER: EvaluatePingResponse = "Parameter
Problem"
Case ICMP_STATUS_SOURCE_QUENCH: EvaluatePingResponse = "Source
Quench"
Case ICMP_STATUS_OPTION_TOO_BIG: EvaluatePingResponse = "Option Too
Big"
Case ICMP_STATUS_BAD_DESTINATION: EvaluatePingResponse = "Bad
Destination"
Case ICMP_STATUS_NEGOTIATING_IPSEC: EvaluatePingResponse =
"Negotiating IPSEC"
Case ICMP_STATUS_GENERAL_FAILURE: EvaluatePingResponse = "General
Failure"

'Unknown error occurred
Case Else: EvaluatePingResponse = "Unknown Response"

End Select

End Function

Sub test()
Dim ECHO As ICMP_ECHO_REPLY
Debug.Print EvaluatePingResponse(ping("64.79.161.26", ECHO))
End Sub

Nov 24 '05 #6
lylefair wrote:
I don't think is what you want, but it's what I would think about
using.

http://www.programmershelp.co.uk/showcode.php?e=341


Thanks for posting what you have so far. It seems like a lot of code
for me to see if the Access user remembered to start the dial-up
connection before uploading a new file to the website. Perhaps all
that code will be necessary.

James A. Fortune

Nov 24 '05 #7
While a lot of code seems like a lot of code to us, it may seem like a
millionth of a second to the cpu.

Nov 24 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
3
by: Web Webon | last post by:
Hi everybody! I wonder if this is possible? I need to determine if a client is using "windows classic folders" or anything else. If I instantiate a Shell ActiveX object is there a way of...
7
by: Chuck | last post by:
I am developing an application for use on laptop computers. I need to be able to programmatically determine if the laptop is connected to the network. If the connection exists I want to use the...
8
by: Steven Van Dyke | last post by:
Hi I need a code snippet to determine if my computer is connected to a network or not. There's probably a System.Net function for it, but I cannot find it. Thanks, Steve
6
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try {...
3
by: David | last post by:
I want to develop a single web app that will be run in three environments: development, test, and production. The test and prodc. will be on the same machine under different directories. I wish...
25
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
13
by: softwaredoug | last post by:
I can't see to easily find this on google or in a newsgroup Is there a standard function/macro/whatever you can call and determine the distance in a C program how deep one is in the C call stack...
1
by: jeffreykr | last post by:
hi, i am trying to this from portable code. ie, i have seen how to check for a live connection from .NET but i'm not using those libraries. any other way to check for a live internet connection...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.