4

I wrote one small function to check internet connection availability

void cis()
{
    if(InternetCheckConnection(NULL,FLAG_ICC_FORCE_CONNECTION,0))
    {
        cout << "internet alive";
    }
}

I'm using WinInet.h for InternetCheckConnection(). Now the problem is that I get the following linker error:

[Linker error] undefined reference to `_imp__InternetCheckConnectionA@12'

I am using DevC++ for my project. Any ideas on how to fix this linker problem or any other ideas to check for an active internet connection?

Christian Ammer
  • 7,464
  • 6
  • 51
  • 108
darkworks
  • 65
  • 3
  • 12
  • 1
    You probably need to link some library. – Alexey Frunze Jan 21 '13 at 07:35
  • 1
    Did you check the [documentation[)http://msdn.microsoft.com/en-us/library/windows/desktop/aa384346%28v=vs.85%29.aspx) for the function you're calling? The "requirements" section lists this little piece of information: `Library: Wininet.lib`. By the way, congratulations on having found the single worst, buggiest and most outdated IDE possible. How is it even *possible* to end up using Dev-C++? That thing is *ancient* and even when it wasn't, it *sucked* – jalf Jan 21 '13 at 07:35
  • Please excuse, but your posting is a mess. Even if you are not a native english speaker it should at least be possible to format your source properly, to start sentences with capitals and to finish them using a full-stop. – alk Jan 21 '13 at 07:40
  • See if you add reference to WinInet.dll – Mihai8 Jan 21 '13 at 07:41
  • @jalf i used overall dev c++ , its now updated .... also anyother good IDE u wana suggest :) but do not suggest microsfot studio :D – darkworks Jan 21 '13 at 07:44
  • @darkworks there are express versions of Visual Studio which are very powerful. Would you mind giving the reason why you don't want to use that? – default Jan 21 '13 at 07:52
  • @jalf i have problems with compiling , linking like that with microsoft products , – darkworks Jan 21 '13 at 07:59
  • @jalf well lets try , i have visual stduio 2012 , let see what happen when i copy my code to it – darkworks Jan 21 '13 at 08:00
  • @jalf well i created console application project in visual studio under vc++ tab i press run button it gives error on its pre added coded that fatal error C1083: Cannot open include file: 'SDKDDKVer.h': No such file or directory i have not added code yet its pre coded when creating project – darkworks Jan 21 '13 at 08:04
  • @jalf i have visual studio 2012 ultimate edition , so tell na how to fix this SDKDDVer.h problem – darkworks Jan 21 '13 at 08:11
  • @darkworks which project type did you create? Make sure to select Win32 Console Application, and then from the Wizard that pops up, select `Empty project` to get rid of all the Microsoft cruft. Yes, Visual Studio has horrible project defaults. – jalf Jan 21 '13 at 09:33
  • @jalf ok tell one thing more where can i did my libraries i mean .h file in dev c we place in /include folder , also libs in /libs folder so what about libs in visual studio too – darkworks Jan 21 '13 at 10:04
  • I don't understand the question. Where can you *what* with libraries? Where you can find them? Where you should place them? Something else? – jalf Jan 21 '13 at 11:35
  • @jalf ya i want to know in visual studio how can we add libraires (.h files) and libs (.a files ) i mean their folders location where they can be placed , in dev c++ normally the paths for header files are /include folder and for lib ./libs – darkworks Jan 21 '13 at 12:34
  • @darkworks: you can place them wherever you like, but this one is already available with the Windows SDK (and, as far as I know, also with VS Express). Just open project properties and specify the library under linker inputs – jalf Jan 21 '13 at 13:45
  • `#pragma comment(lib, "wininet.lib")` and `if (!InternetCheckConnectionW(L"http://google.com", FLAG_ICC_FORCE_CONNECTION, 0))` this good works ) – GooliveR Jul 30 '17 at 15:36

3 Answers3

5

Its a linker error. as per the documentation you need to use wininet library. adding -lwininet in makefile may work.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • @darkworks: You can mark an answer as accepted b clicking the white checkmark on the left. – Niklas R Jan 21 '13 at 07:43
  • ya i will accept but say wait .... minutes :D – darkworks Jan 21 '13 at 07:47
  • The reason for that forced wait is that you often get multiple answers if you wait a bit, and then you can accept the best of them. Whereas if you accept an answer immediately, that typically discourages others from answering, so you might get poorer answers overall. – jalf Jan 21 '13 at 09:35
  • where to add -lwininet option in Visual Studio 2013? – Veena Dec 25 '15 at 18:52
2

For unix

    if (system("ping www.google.com -c 2 > /dev/nul") == 0) {
    cout << "all good" << endl;
}else{
    cout << "bad" << endl;
}

Windows

    if (system("ping www.google.com -t 2 > nul") == 0) {
    cout << "all good" << endl;
}else{
    cout << "bad" << endl;
}
Alexandru Calin
  • 220
  • 2
  • 11
0

It found your header, however you need to link to the libraries. Try adding Wininet.lib to your project(try as a file or in the linker properties) and make sure that the Windows SDK is properly installed on your system.

MagikWorx
  • 342
  • 1
  • 10