auf Internetverbindung prüfen



  • Hallo liebe Helfer,

    bin ein C++ Neuling mit nur wenig Erfahrung und lud mir neulich das Visual Studio 2008 Express Edition.

    Auf "www.c-worker.ch/api/index.php" fand ich ein API Code-Beispiel (RasGetConnectStatus), welches prüft, ob eine Internetverbindung besteht oder nicht. Dieses würde ich gern ausbauen.

    #include <stdafx.h>
    #include <windows.h>
    #include <ras.h>
    #include <iostream>
    
    using namespace std;
    
    bool CheckForConnection()
    {
    
       RASCONN RasCon;
       LPRASCONN pRasCon = &RasCon;
    
       RASCONNSTATUS RasConStatus;
    
       DWORD lpcb;
       DWORD lpcConnections;
       bool  ReturnValue;
    
       pRasCon->dwSize = 412;
       lpcb = 256 * pRasCon->dwSize;
       ReturnValue = false;
    
       if(RasEnumConnections(pRasCon, &lpcb, &lpcConnections) == 0)
       {
           RasConStatus.dwSize = 160;
           RasGetConnectStatus(pRasCon->hrasconn, &RasConStatus);
           ReturnValue = (RasConStatus.rasconnstate == 0x2000);
       }
    
       return(ReturnValue);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	if(CheckForConnection())
      {
        cout << "Connected to the Internet !";
      }
      else
      {
        cout << "Not connected to the Internet !";
      }
      return 0;
    }
    

    Beim Kompilieren des Programmes bekomme ich die Meldung:

    error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_RasGetConnectStatusW@8" in Funktion ""bool __cdecl CheckForConnection(void)" (?CheckForConnection@@YA_NXZ)".

    error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "_RasEnumConnectionsW@12" in Funktion ""bool __cdecl CheckForConnection(void)" (?CheckForConnection@@YA_NXZ)".

    fatal error LNK1120: 2 nicht aufgelöste externe Verweise.

    Wer weiß Rat und kann mir helfen?

    empty 😕



  • Rasapi32.lib. mitlinken



  • Wie mache ich das - eine lib-Datei mit zu linken?





  • Prima, das Programm startete zwar nun. Aber egal, ob ich on- oder offline war, kam die Meldung, dass ich nicht mit dem Internet verbunden bin. Bin einmal über Modem und einmal über WLAN ins Netz.



  • 1. Debugger verwenden
    2. Rückgabewerte von RasEnumConnections und RasGetConnectStatus prüfen



  • Ich mach's einfach nur so:

    if( gethostbyname( "www.google.com" ) == NULL )
    ...
    

    Ist vielleicht nicht die eleganteste Methode, aber simpel und funktioniert zuverlässig.


Anmelden zum Antworten