InternetCheckConnection 始终返回 false

发布于 2024-07-14 07:50:32 字数 265 浏览 13 评论 0原文

我想使用 Wininet 函数 InternetCheckConnection 来检查机器何时连接到互联网并可以访问特定主机。 问题是这个函数总是返回 false,无论我输入什么 URL。

MSDN 链接

I want to use the Wininet function InternetCheckConnection to 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.

MSDN link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你爱我像她 2024-07-21 07:50:32

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

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

如果连接可行并且返回,则 GetLastError() 返回 0
12029(尝试连接服务器失败)如果没有。

以下组合对我不起作用:

  InternetCheckConnection(NULL, FLAG_ICC_FORCE_CONNECTION, 0) ;

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

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

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

Following combination works for me on Windows 7 and 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.

Following combonations don't work for me :

  InternetCheckConnection(NULL, FLAG_ICC_FORCE_CONNECTION, 0) ;

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

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

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

苹果你个爱泡泡 2024-07-21 07:50:32

您检查过 GetLastError() 吗? 如果我没有正确阅读 MSDN,您需要检查 ERROR_NOT_CONNECTED 以确定您是否真正离线。

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

茶色山野 2024-07-21 07:50:32

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

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?

甜心 2024-07-21 07:50:32

根据微软API文档InternetCheckConnection< /a> 已弃用。

[InternetCheckConnection 可在“要求”部分中指定的操作系统中使用。 在后续版本中可能会更改或不可用。 相反,请使用 NetworkInformation.GetInternetConnectionProfile 或 NLM 接口。 ]

我们可以使用 INetworkListManager接口,用于windows平台检查Internet是否连接。

下面是 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文