HOWTO: Detecting If You Have a Connection to the Internet

ID: Q242558


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) versions 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5


SUMMARY

Many of you want to know if a computer has an active Internet connection before trying to connect to the Internet using the WinInet API or some other communication interface. The internet connection is important if you don't want your application to cause Windows to automatically dial the default Internet modem connection.

This article provides a mechanism for determining if an Internet site is accessible without the risk of you being prompted to dial into another Internet Service Provider.


MORE INFORMATION

Usually the best way to determine if you have a connection to a particular computer is to attempt the connection. If the autodial feature of Windows is enabled then attempting the connection may cause the default Internet dialup connectoid to be opened, and you will be prompted with your credentials to connect.

To avoid having the default Internet connectoid dialed, the InternetGetConnectedState function can be used to determine if there is a default Internet dialup connectoid configured and whether it is currently active or not. If there is a default Internet dialup connectoid configured and it is not currently active then InternetGetConnectedState will return FALSE. If InternetGetConnectedState returns TRUE then you can attempt to connect to the Internet resource without fear of being prompted to connect to another Internet Service Provider.

The following code demonstrates how you would do this:


if (InternetGetConnectedState(...) == FALSE)
{
    // Don't attempt connection or it will bring up the dialog
    ...
}
else
{
    //Attempt connection
    if (InternetOpenURL(...) == NULL)
    {
        // Call failed
        err = GetLastError();
        if ((err == ERROR_INTERNET_NAME_NOT_RESOLVED) ||
            (err == ERROR_INTERNET_CANNOT_CONNECT) ||
            (err == ERROR_INTERNET_TIMEOUT))
        {
            // probably not connected...handle appropriately
            ...
        }
    }
    // We're connected!!!
    ....
}
 

You cannot rely solely on the fact that InternetGetConnectedState returning TRUE means that you have a valid active Internet connection. It is impossible for InternetGetConnectedState to determine if the entire connection to the Internet is functioning without sending a request to a server. This is why you need to send a request to determine if you are really connected or not. 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.

Be aware that InternetGetConnectedState is only concerned with the default Internet connectoid. If you are currently dialed into a different dialup connectoid that has internet access, then InternetGetConnectedState returns FALSE and the logic earlier would indicate that you have no Internet connection.

There are some other ways to try to determine if you currently have a connection to a particular network resource. The IsDestinationReachable() function can be used to find out if there is a current connection to an address. However, he IsDestinationReachable() function is only concerned with whether the IP address is reachable from your computer. It does not work through HTTP proxies or firewalls that restrict ICMP ping packets.

It is also possible to use RasEnumConnections to enhance your code so that you can tell if there is an active dialup connection that might have Internet access even though it is not the default Internet dialup connectoid.

Additional query words:

Keywords : kbIE kbIE400 kbIE401sp1 kbIE401sp2 kbIE500 kbGrpInetServer kbDSupport kbIEFAQ
Version : WINDOWS:4.0,4.01,4.01 SP1,4.01 SP2,5
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: February 1, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.