Frequently Asked Questions

Q. How can I detect if my internet connection is active at this moment?
 

A. Unfortunately, there is no simple answer to this question. Allen Weng of Microsoft provided a insight into this question in a newsgroup posting:

Actually, there is no single function for determining if a machine is connected to the Internet, and it is impossible to reliably determine what is happening without side effects - such as automatic network connections taking place. What you can do is reliably detect when there definitely isn't an Internet Link: in the absence of any dial up or LAN connection the system is definitely off line.

[ Allen Weng's newsgroup posting of May 4, 2001 ]

Most of the information of provided in this topic is an editorialized version of Allen's comments. Some NDIS-specific supplementary information from PCAUSA is provided near the end.

There is also mention of an Intel® paper on Network Status Detection at the end of this page.

 

System Event Notification Service (SENS)

  1. IsNetworkAlive
  2. IsDestinationReachable

You can use IsNetworkAlive If you are targeting system with IE5 or later, this is the best API call yet it even listens for traffic on a LAN.

You can also use a secondary function IsDestinationReachable which tries to resolve the hostname and ping it. This does not work through firewalls, and overestimates speed as the max the LAN card can support, rather than the actual point to point bandwidth.

 

Remote Access Service (RAS)

  1. RasEnumConnections

RasEnumConnections is a reliable technique for modems and direct dial up networking, but not for situations where Internet access is via a LAN. You should dynamically load RasEnumConnectionA from "RASAPI32.DLL", as LAN installations of Windows may not include the library.

 

Win32 Internet (WinInet)

  1. InternetGetConnectedState
  2. InternetCheckConnection

InternetGetConnectedState is a Wininet /IE4 function call can distinguish between modem and LAN, but can't handle complex LAN+autodial router situations. It is "offline state aware". Important: handling of the offline flage changed for IE5 -it returns TRUE for connected' even when off line, but signals the flags in the LPDWORD parameter.

InternetCheckConnection is a Winnet/IE4 function call. This is meant to determine if a URL is reachable- in practice it is pretty unreliable and best voided.

 

IP Helper API

The IP helper API can tell you which network interface to use to connect to a supplied IP address, and what the bandwidth and current status of that link is. The IP Helper APIs even let you find out how much traffic is going over a link, but only detect the 'loopback' interface on Windows 98, so is not a lot of use.

The IP Helper API documented in the Platform SDK, is available on  Windows NT SP4 and higher as well as some Windows 9X/ME platforms.

 

Registry Information

Using the Offline flag which is part of IE4 to allow users to manually control the online/offline state of applications. This flag is stored in the registry and can be manipulated via some function calls

 

Allen's Summary

These calls mostly determine the presence or absence of network connections -not Internet access, so can't handle a home network sharing a dial up connection, or two laptops connected directly to each other.

The global offline state flag of IE4 (and hence win98, NT5) and the call to test it - InternetGetConnectedState - look the best long term options, but will take time to become universal.

Finally, whatever technique you use, when it's time to talk to a remote site, always add timeouts or a cancel button. Even a quick functions like gethostbyname can lock up an app if something in the network chain is broken.

 

Network Driver Interface Specification (NDIS)

NDIS 5.0 and higher requires NDIS miniport drivers to support information called "media connect status". This is defined to be the "connection status of the NIC on the network".

It is important to understand that the NDIS media connect status does not actually provide the answer to the original question!

For an Ethernet NIC a media connect status result of NdisMediaStateConnected simply means that the Ethernet cable is plugged in.

If the NIC is connected to a dial-on-demand router (for example), the fact that the cable is plugged in offers no insight into whether the dialup connection is active. If the NIC is attached to a DSL router or cable modem, media connect status does not offer any insight into whether the connection to the ISP is actually active.

A NDIS device driver components, such as a NDIS protocol driver, can use the NdisRequest function to query information on the NDIS OID_GEN_MEDIA_CONNECT_STATUS "object identifier". However, NDIS media connect status is not simple to fetch from a user mode application. 

User mode applications can fetch media connect status indirectly using the Windows Management Instrumentation (WMI) API as well as other techniques. See the FAQ topic "How do I query NDIS miniport OIDs using WMI?".

 

Network Status Detection by Majd Naciri

This is an interesting paper published on the Intel® Developer Services website.

[ Network Status Detection by Majd Naciri ]

 

Topic Status

September 10, 2003 Added link to Intel paper on Network Status Detection.
January 6, 2003 Added link to WMI NDIS query FAQ topic.
December 31, 2002 Reviewed, expanded and moved to NDIS.com.
May 4, 2001 Allen Weng's newsgroup posting.

 

 

PCAUSA Home · Privacy Statement · Products · Ordering · Support · Utilities · Resources
Mailing Lists  · PCAUSA Newsletter · PCAUSA Discussion List
Rawether for Windows, Rawether .NET, WinDis 32 and NDIS Press are trademarks of Printing Communications Assoc., Inc. (PCAUSA)
Microsoft, MS, Windows, Windows Vista, Windows 95, Windows 98, Windows Millennium, Windows 2000, and Win32 are registered trademarks and Visual C++ and Windows NT are trademarks of the Microsoft Corporation.
Copyright © 1996-2009 Printing Communications Assoc., Inc. (PCAUSA)
Last modified: January 23, 2009