windows InternetCheckConnection 总是返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/560771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:00:03  来源:igfitidea点击:

InternetCheckConnection always return false

windowsconnectionwininet

提问by gmuller

I want to use the Wininetfunction InternetCheckConnectionto check whenever the machine is connected to the internet and can access a specific host. The problem is that this function is always returning false, no matter the URL I put on it.

我想使用该Wininet功能InternetCheckConnection来检查机器何时连接到互联网并且可以访问特定主机。问题是这个函数总是返回false,不管我放在它上面的 URL 是什么。

MSDN link

MSDN链接

回答by Jabberwocky

Following combination works for me on Windows 7 and Windows XP SP3:

以下组合适用于 Windows 7 和 Windows XP SP3:

 InternetCheckConnection("http://www.google.com", FLAG_ICC_FORCE_CONNECTION, 0) ;

GetLastError() returns 0 if the connexion is possible and it returns 12029 (The attempt to connect to the server failed) if not.

如果连接可能,GetLastError() 返回 0,否则返回 12029(连接到服务器的尝试失败)。

Following combonations don't work for me :

以下组合对我不起作用:

  InternetCheckConnection(NULL, FLAG_ICC_FORCE_CONNECTION, 0) ;

GetLastError() returns 12016 (The requested operation is invalid).

GetLastError() 返回 12016(请求的操作无效)。

  InternetCheckConnection(NULL, 0, 0) ;
  InternetCheckConnection(("http://www.google.com", 0, 0) ;

for both GetLastError() returns 2250 (The network connection could not be found).

对于 GetLastError() 返回 2250(找不到网络连接)。

回答by MSalters

Have you checked GetLastError()? If I read MSDN correctly, you would need to check for ERROR_NOT_CONNECTEDto determine whether you're truly offline.

你检查了GetLastError()吗?如果我正确阅读了 MSDN,您将需要检查ERROR_NOT_CONNECTED以确定您是否真正处于离线状态。

回答by Adrian Grigore

Just a wild guess, but perhaps this is due to a personal firewall blocking all outgoing connections for one of the Windows DLLs and / or your application?

只是一个疯狂的猜测,但也许这是由于个人防火墙阻止了 Windows DLL 和/或您的应用程序之一的所有传出连接?

回答by yeasir007

According to Microsoft API document InternetCheckConnectionis deprecated.

根据 Microsoft API 文档InternetCheckConnection已弃用。

[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. ]

[InternetCheckConnection 可用于要求部分中指定的操作系统。它可能会在后续版本中更改或不可用。而是使用 NetworkInformation.GetInternetConnectionProfile 或 NLM 接口。]

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

我们可以使用INetworkListManager接口代替这个 API来检查 Windows 平台是否连接了 Internet。

Here below is the win32 codebase :

下面是 win32 代码库:

    #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;
    }