I. 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 connexions. À tester avec différentes configurations.
Exemple sur une Form (Form1), poser un Bouton (Button1) et un Label (Label1) :
Sélectionnez
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
}