0

I want to check my internet connection. This is my code:

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

using namespace std;

int main()
{
    if (InternetCheckConnection("http:\\www.google.com", FLAG_ICC_FORCE_CONNECTION, 0))
    {
        cout << "You are connected to the Internet" << endl;
    }
    else
    {
        cout << "You are not connected to the Internet" << endl;
    }
}

But it shows the following error:

C:\Users\icl\AppData\Local\Temp\ccGjaicH.o  n.cpp:(.text+0x22): undefined reference to `__imp_InternetCheckConnectionA'
C:\Users\icl\Desktop\New folder\collect2.exe    [Error] ld returned 1 exit status

How to solve it?

  • Which compiler are you using? Although `WinInet.lib` is the correct library to link to for `InternetCheckConnection()`, most compilers do not recognize the `#pragma comment` directive, requiring alternative solutions for linking to libraries. On a side note, the URL you are passing to `InternetCheckConnection()` is malformed. – Remy Lebeau Nov 21 '21 at 09:25
  • @RemyLebeau I am using dev-c++ –  Nov 21 '21 at 09:29
  • In Dev-C++, DLL import libs are linked to in the project settings instead of in code. – Remy Lebeau Nov 21 '21 at 09:41
  • @RemyLebeau What should i write in linker? –  Nov 21 '21 at 17:02
  • see https://sourceforge.net/p/dev-cpp/discussion/128327/thread/1baf9670/, using `wininet` as the lib to add. – Remy Lebeau Nov 21 '21 at 18:53

0 Answers0