Gold mine of Visual C++ tricks!
How to check whether internet connection is available.
![]()
I often notice that my anti-virus starts automatic update when internet connection is available. How does these applications detect internet connection?
![]()
You can use the api – InetIsOffline(). If internet connection is not available, it will return true else false. See the sample code snippet below.
#include "intshcut.h"
...
// Check whether internet connection is enabled.
if( !InetIsOffline( 0 ))
{
// Internet connection is available.
// Do automatic updates.
}
![]()
Don’t forget to add library – url.lib to your project settings.
![]()
Targeted Audience – Beginners.
I just tried this function. But it is returning FALSE all time. I mean even if the LAN is not connected, it is returning FALSE.
It works perfect in my dialup connection. Need to check further.
It seems that InetIsOffline() corresponds to Internet Explorer’s “Offline Mode” and does not accurately gauge connection status unless you’re using dialup and Internet Explorer…. which is a dwindling percentage.
The more correct way seems to be to use either the WinInet library and InternetCheckConnection(), or alternatively Rasapi32.lib and RasEnumConnections() to tell if there is an active connection (from Windows’ perspective which is not always accurate either, but better than the above).
You can also just take the easy way out and use Ping() to a known good server, which is tempting since it works on all versions of Windows, but firewalls and such often block ICMP too. Trying a full request to the same server is probably your only simple way to know for sure you’re online. If you’re going to do that though you usually don’t need to bother checking in the first place. These functions just give you a heads up when you’re absolutely going to fail — they don’t give you any indication you’re really online.
Hi Saka,
Thank you very much for sharing your knowledge. I’ll check InternetCheckConnection() and update the article, soon.
Keep sharing your thoughts!
Best Regards,
Jijo.