使用 internetcheckconnection 在 C++ 中检查互联网连接

checking internet connection in c++ using internetcheckconnection

我正在尝试使用 internetcheckconnection() 检查用户的互联网连接。

代码:

#include <Wininet.h>
#include <iostream>
#include <string.h>
#include <windows.h>
#pragma comment(lib, "wininet.lib")

int main()
{

    char url[128];
    strcat(url, "http://www.techtoolbox.com");
    bool bConnect = InternetCheckConnection(url, FLAG_ICC_FORCE_CONNECTION, 0);

    if (bConnect) {
        //internet connection exists !

        std::cout << "yes";
    }
    else {
        std::cout << "no ";
    }

    return 0;
}

但是出现了很多错误,比如

29 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'LPVOID' does not name a type

30 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'HINTERNET' does not name a type

32 11 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\Wininet.h [Error] 'WORD' does not name a type and 431 more .

我已经安装了 Wininet.lib,但仍然会出现这些错误。如果你能解决这个简单的问题,那就太好了:) .

LPVOIDHINTERNET 和错误消息中的其他类型在 windows.h 中声明。您应该重新排列包含以修复这些错误:

#include <windows.h>
#include <Wininet.h>