Edwin Eefting made a clever comment about using the IP
routing table to detect a connection. I have implemented
this brillant idea and I have to say that this is actually
a very interesting solution.
Basically, you just look at the system IP routing table and
check whether an entry exists for a default gateway
(0.0.0.0). If yes, the system either has an active dial-up
connection or is connected via LAN, DSL or cable. As soon
as the dial-up connection is broken, the default gateway
entry disappears from the IP routing table.
This doesn't guarantee that any request will succeed (the
cable may be unplugged or the user may have logged off from
the ISP) but you can check this afterwards, **the main
interest of this approach being that is doesn't trigger any
autodial process**. If the function below returns FALSE,
you will not even try to access the Internet: you know that
any request will fail anyway. If it returns TRUE, you can
safely make a connection attempt without incurring the risk
of triggering an autodial. You will possibly get an error
when using WinInet or any socket function.
GetIpForwardTable(NULL, &dwTableSize, FALSE);
pft = (MIB_IPFORWARDTABLE*) new BYTE[dwTableSize];
TRUE) == NO_ERROR)
for (unsigned int nIndex = 0;
nIndex < pft->dwNumEntries;
if (pft->table[nIndex].dwForwardDest == 0)
// Default route to gateway
bHasDefaultRoute = TRUE;
Note that your system must support the IPHLPAPI, which is
not the case for Win95 and Windows NT 4 pre-SP4.
This code has been tested on various configurations from
Win98 to XP) and it worked fine each time.
Hope this helps.
Microsoft MVP [.Net]
Posted by bob16972
on 03/26/2005 06:19pm
Your solution should have been included in "Visual C++ Goodies" instead of "Function To Verify if Connected to the internet" methinks. Too bad the book came out first.
Thanks Patrick (and Edwin) for sharing it
There seems to be a problem in the gethostbyname because I could not find a way to shorten the connection timeout. 30 seconds or more is way too long for most users. I finally resorted to threads to make this work with a 5 second timeout.
Let me start by saying that Ran is one smart cookie! I am a very serious researcher, and I have investigated internet connection functions. Ran provides the foundation for numerous possible utilitarian functions. The genius behind IsInternetConnected() is the temporary alteration of EnableAutodial registry setting. After altering this registry setting, you can perform numerous internet tasks without the fear of a Internet Connection dialog popping up. I have altered Ran's function to exclude registry function error checking and I have also exchanged CSocket for CInternetSession, with an inclusion of CInternetException to catch any errors.
My version of Ran's function is triggered by a 300000 millisecond timer (approx. 5 min.) which upon triggering checks for an internet connection. If a connection is established, it reads a string from an internet file and replaces an existing member variable string. I am not to keen on the CSocket class, but CInternetSession has many possibilities.
Between Ran's original function and my alteration to CInternetSession, you folks should have a hay day. Please post useful alterations so that others may use them. Remember to use CInternetException to catch the errors.