[Erreur FAQ]Comment savoir si on est connecté à internet ?
Bonjour,
Je me permet de signaler une erreur dans la FAQ C++ Builder à ce jour.
La fonction qui est sensée renvoyer l'état de la connexion à internet ne fonctionne pas (et j'en ai besoin ! ;) ).
Voici ce qui est dans la FAQ au 29/02/2009 :
Citation:
Envoyé par FAQ C++ Builder
Savoir si l'on est connecté à Internet :
Pour cela nous allons utiliser la fonction "InternetGetConnectedState" de la dll "Wininet.dll". Cette façon de faire ne fonctionne pas avec tous les types de connections. A tester avec différentes configurations.
Exemple sur une Form (Form1) poser un Bouton (Button1) et un Label (Label1) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void __fastcall TForm1::Button1Click(TObject *Sender)
{
typedef BOOL (WINAPI *PF_INETGETCONNECTEDSTATE)(LPDWORD, DWORD);
HANDLE hWinInet;
PF_INETGETCONNECTEDSTATE pfInternetGetConnectedState;
//.............
hWinInet = LoadLibrary("WININET.DLL"); //Chargement de la dll
if(hWinInet == NULL)
{
Label1->Caption = "Impossible de charger Wininet.dll";
return;
}
pfInternetGetConnectedState = (PF_INETGETCONNECTEDSTATE) GetProcAddress(
hWinInet, "InternetGetConnectedState");
// affectation du pointeur sur la fonction
if(pfInternetGetConnectedState == NULL)
{
Label1->Caption = "Erreur appel fonction InternetGetConnectedState";
if(hWinInet) FreeLibrary(hWinInet);
return;
}
//.............
DWORD TypeCon ;
if (pfInternetGetConnectedState(&TypeCon, 0)) //appel de la fonction
Label1->Caption = "Connecté";
else Label1->Caption = "Déconnecté";
//.............
if(hWinInet) FreeLibrary(hWinInet); //libération de la dll
} |
Or, la fonction "pfInternetGetConnectedState(&TypeCon, 0)" renvoie un entier (TypeCon) qui n'est pas traité ici !
Si on va lire le détail de ce paramètre sur MSDN, on peut lire ceci :
// INTERNET_CONNECTION_CONFIGURED 0x40 Local system has a valid connection to the Internet, but it might or might not be currently connected.
// INTERNET_CONNECTION_LAN 0x02 Local system uses a local area network to connect to the Internet.
// INTERNET_CONNECTION_MODEM 0x01 Local system uses a modem to connect to the Internet.
// INTERNET_CONNECTION_MODEM_BUSY 0x08 No longer used.
// INTERNET_CONNECTION_OFFLINE 0x20 Local system is in offline mode.
// INTERNET_CONNECTION_PROXY 0x04
Que je sois connecté ou déconnecté, pfInternetGetConnectedState me renvoie toujours true.
Par contre, quand je suis vraiment connecté, TypeCon vaut 0x51 (1 = connexion par modem, 0x50 = ??? )
Quand je suis vraiment déconnecté, TypeCon vaut 0x12 (2 = réseau local, 0x10 = ??? )
Pour tester vraiment la connexion à internet, il faut peut-être aller plus loin et appeler une fonction pfInternetOpen("Exemple",
INTERNET_OPEN_TYPE_DIRECT,
0, 0, 0);
et
pfInternetOpenUrl( hSession,
strUrl.c_str(),
0, 0, INTERNET_FLAG_RELOAD, 0);
Non ?
Si vous avez une méthode de test de la connexion à internet qui fonctionne, je suis preneur ! ;)