BOOL IsInternetConnected() | CodeGuru

BOOL IsInternetConnected()

Alot of times, programming internet applications you need to know wether the computer is connected to the internet? In some occasions you can try to use RasEnumConnections, in most cases on a typical computer opened Ras Connection mean that the user is connected to the internet. However, this idea won’t work in case the user […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 16, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Alot of times, programming internet applications you need to know wether the computer is connected to the internet?

In some occasions you can try to use RasEnumConnections, in most cases on a typical computer opened Ras Connection mean that the user is connected to the internet. However, this idea won’t work in case the user is connected to internet not via Ras device or is connected to some kind of local network.

We try to use common sence here. Since internet is something abstract, lets decide that user which is connected to internet, is the one who can connect to www.microsoft.com.

First I wanted to use ping, but found a lot of troubles with it. Therefore, I have decided to use sockets “directly”. I didn’t want to write some fance code here, rather I have wrote a simple function you can cut & paste into your application.

Another problem adressed in the function that in some computer a try to establish internet connection will bring dialup dialog. The function solve it by temporary disabling the feature in the registry and enabling it back afterwards.

Pay attention to the comments.

BOOL YourClassHere::IsInternetConnected (void)
{
 int nCheck = AfxSocketInit();
 CSocket m_Server;
 HKEY hKey;
 DWORD dwDial, dwDialType = REG_DWORD, dwDialSize = 4;
 DWORD dwNew = 0;
 BOOL bResult = true;
 if ( RegOpenKeyEx ( HKEY_CURRENT_USER,
    Software\Microsoft\Windows\CurrentVersion\Internet Settings”,
     0, KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
    ; // We cannot find the key. Handle this situation or just continue
 if ( RegQueryValueEx( hKey, “EnableAutodial”, NULL, &dwDialType,
  (BYTE *) &dwDial, &dwDialSize ) != ERROR_SUCCESS )
  ; // We cannot find the value. Handle it.
 if ( dwDial ) { // We need to change the value, in order to make
            // a dialup window not to show up.
  if ( (nCheck = RegSetValueEx( hKey, “EnableAutodial”, NULL,
   dwDialType, (BYTE *) &dwNew, dwDialSize )) != ERROR_SUCCESS)
  ; // Failed? We shouldn’t get here. You decide how to handle it
 }
 if ( !m_Server.Create() ) {
        // m_sError = _T( “Unable to create the socket.” );
        bResult = false;
 }
// You can use www.microsoft.com in order to check whether DNS is available
// or numeric IP otherwise
 else if ( !m_Server.Connect( “www.microsoft.com”, 80 ) ) {     //  207.46.130.150
        //m_sError = _T( “Unable to connect to server” );
        m_Server.Close();
        bResult = false;
 }
 if ( dwDial ) {
  if ( (nCheck = RegSetValueEx( hKey, “EnableAutodial”, NULL,
   dwDialType, (BYTE *) &dwDial, dwDialSize )) != ERROR_SUCCESS)
  ; // Failed? We shouldn’t get it. You decide how to handle this.
 }
 RegCloseKey( hKey );
 return ( bResult );
}

I would like to thank Martin Tschepe for pointing me out this registry key.

Download source – 2KB

History

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.