Detecting a Connection 
 

How can we programmatically detect if the computer our application is running on is connected to the Internet?

This seems to be a common question raised on various newsgroups. Regretfully, it is a question that is very hard to find an answer to. It is difficult to try and detect an active connection without invoking the Dial-Up Networking dialog, or without causing an auto-dial router to establish a connection.

There is a fair amount of advice around (like statically linking to DLLs) that is not good if your goal is to develop shrink-wrap software that can be installed on any Win95 or later system. Also, many of the recommended DLL calls are part of Microsoft Internet Explorer (IE) and require that IE is not only installed but configured correctly.

If you manage an in-house network where you can insure that all systems run the latest Microsoft software (and have it correctly configured) then the information on this resource page probably doesn't concern you.

Please contact us if you find any inaccurate information on this page, or if you have other suggestions.


One of the great tragedies of life is the murder of a beautiful theory by a gang of brutal facts.
--Benjamin Franklin


What minimal system do we want our program to run on?

Initial release of Win95?

Then we can't use WinInet or Winsock 2 features. We could use RasEnumConnections to see if there is an active dialup connection. If there is an active connection then we could use RasGetProjectionInfo to verify that the dialup connection is a TCP/IP connection.

Win95 and IE4+?

We can use WinInet and InternetGetConnectedState but if the user runs another browser (like Netscape) and doesn't have IE configured then InternetGetConnectedState may give us false information.

For example, I used to have IE installed on both WinNT4 and Win98 systems (with a LAN between them) but I used NetScape with a modem on each machine to connect to the Internet (I now use Mozilla and DSL). With Internet Explorer configured to use a LAN connection, InternetGetConnectedState always returns TRUE. When I checked both of my systems they were both configured by default to "use a LAN connection" since I never configured IE. On both systems I got TRUE from InternetGetConnectedState when I was not online.

Microsoft http://support.microsoft.com/default.aspx?scid=kb;en-us;242558 says: "You can be assured however that if InternetGetConnectedState returns TRUE, that attempting your connection will NOT cause you to be prompted to connect to the default Internet Service Provider." That statement was false on my systems where I used NetScape. On my systems InternetGetConnectedState always returned TRUE, and if I trusted that information and attempted a connection to some place on the Internet I got the default dialup networking prompt.


Common pitfalls

Linking the RAS, WinInet, or other DLLs statically rather than dynamically.

If you just add the interface unit to these DLLs into your Delphi uses clause then your application will not run if any of the DLLs are not present. For example, NT4 does not install RAS by default and it is usually not present until a modem is defined.


Another suggestion worth looking into (thanks to Paul Gertzen and his messages on the Delphi.Internet.Winsock newsgroup):

Try pinging the Internet root servers. You need to ping an IP address so you don't cause a DNS lookup which will invoke the Dial-Up Networking dialog. A root server is the server that a DNS will connect to to resolve a name it does not have in its cache. See http://www.root-servers.org for information. If you get a response from any of them then you are connected to the Internet. If they all fail then either you are not connected to the Internet, you are behind a firewall that blocks pings, or the whole Internet is down.


Some other things to research if you really want to get low-level:

https://www.pcausa.com/tdisamp/tdiq.htm

http://members.tripod.com/~staskh/
http://www.mvps.org/win32/network/snmp.html


Other Delphi resource pages on this web site

Cleaning up the Delphi uses statements
Program EXE depends on Delphi design-time and compile-time environment


Engineering and Communication Systems Programming

Available at reasonable rates for contract or part-time employment via telecommute with occasional on-site. Small projects and maintenance projects are welcomed.

Return to recent projects and experience

Return to home page for Breneman Labs
Show this image for email address

BRENEMAN LABS
Customer support is our top priority!
Contact information on home page
email: see the image to the left