Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically check whether my machine has internet access or not

How can I programmatically check whether my machine has internet access or not using C/C++, is it just a matter of pinging an IP? How does NIC do it ? I mean something like:

enter image description here

I am using Windows 7.

like image 286
Aan Avatar asked Apr 03 '13 03:04

Aan


People also ask

How can we check whether the computer is connected to the internet or not?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.

How do I know if Im connected to the internet using CMD?

To use the ping command, simply open a command prompt and type ping [hostname] or ping [ip address] and then press enter. For example, to check if you can connect to www.google.com, you would type ping www.google.com and press enter.


1 Answers

According to Microsoft API document InternetCheckConnection is deprecated.

[InternetCheckConnection is available for use in the operating systems specified in the Requirements section. It may be altered or unavailable in subsequent versions. Instead, use NetworkInformation.GetInternetConnectionProfile or the NLM Interfaces. ]

Instead of this API we can use INetworkListManager interface to check whether Internet is connected or not for windows platform.

Here below is the win32 codebase :

#include <iostream>
#include <ObjBase.h>      // include the base COM header
#include <Netlistmgr.h>

// Instruct linker to link to the required COM libraries
#pragma comment(lib, "ole32.lib")

using namespace std;

enum class INTERNET_STATUS
{
    CONNECTED,
    DISCONNECTED,
    CONNECTED_TO_LOCAL,
    CONNECTION_ERROR
};

INTERNET_STATUS IsConnectedToInternet();

int main()
{
    INTERNET_STATUS connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
    connectedStatus = IsConnectedToInternet();
    switch (connectedStatus)
    {
    case INTERNET_STATUS::CONNECTED:
        cout << "Connected to the internet" << endl;
        break;
    case INTERNET_STATUS::DISCONNECTED:
        cout << "Internet is not available" << endl;
        break;
    case INTERNET_STATUS::CONNECTED_TO_LOCAL:
        cout << "Connected to the local network." << endl;
        break;
    case INTERNET_STATUS::CONNECTION_ERROR:
    default:
        cout << "Unknown error has been occurred." << endl;
        break;
    }
}

INTERNET_STATUS IsConnectedToInternet()
{
    INTERNET_STATUS connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
    HRESULT hr = S_FALSE;

    try
    {
        hr = CoInitialize(NULL);
        if (SUCCEEDED(hr))
        {
            INetworkListManager* pNetworkListManager;
            hr = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, __uuidof(INetworkListManager), (LPVOID*)&pNetworkListManager);
            if (SUCCEEDED(hr))
            {
                NLM_CONNECTIVITY nlmConnectivity = NLM_CONNECTIVITY::NLM_CONNECTIVITY_DISCONNECTED;
                VARIANT_BOOL isConnected = VARIANT_FALSE;
                hr = pNetworkListManager->get_IsConnectedToInternet(&isConnected);
                if (SUCCEEDED(hr))
                {
                    if (isConnected == VARIANT_TRUE)
                        connectedStatus = INTERNET_STATUS::CONNECTED;
                    else
                        connectedStatus = INTERNET_STATUS::DISCONNECTED;
                }

                if (isConnected == VARIANT_FALSE && SUCCEEDED(pNetworkListManager->GetConnectivity(&nlmConnectivity)))
                {
                    if (nlmConnectivity & (NLM_CONNECTIVITY_IPV4_LOCALNETWORK | NLM_CONNECTIVITY_IPV4_SUBNET | NLM_CONNECTIVITY_IPV6_LOCALNETWORK | NLM_CONNECTIVITY_IPV6_SUBNET))
                    {
                        connectedStatus = INTERNET_STATUS::CONNECTED_TO_LOCAL;
                    }
                }

                pNetworkListManager->Release();
            }
        }

        CoUninitialize();
    }
    catch (...)
    {
        connectedStatus = INTERNET_STATUS::CONNECTION_ERROR;
    }

    return connectedStatus;
}
like image 99
yeasir007 Avatar answered Oct 17 '22 07:10

yeasir007