Mega Code Archive

 
Categories / Delphi / Examples
 

Internetconncheck

> Does anyone know how can I detect if I am connected to internet with Delphi5 ? interface uses Windows, WinInet; type TConDescription = ( cdModem, { Local system uses a modem to connect to the Internet } cdLan, { Local system uses a local area network to connect to the Internet } cdUsesProxy, { Local system uses a proxy server to connect to the Internet } cdModemBusy, { not used } cdRasInstalled, { Local system has RAS installed } cdOffline, { Local system is in offline mode } cdConfigured { Local system has a valid connection to the Internet, but it may or may not be currently connected } ); TConDescriptions = set of TConDescription; function inet_IsConnected: boolean; overload; function inet_IsConnected(var ConDes: TConDescriptions): boolean; overload; function inet_IsConnected(var ConDes: TConDescriptions; var sConName : string): boolean; overload; implementation uses Sysutils ,Forms ,TypInfo ,classes ; const { these are already defined into WinInet.pas } //INTERNET_CONNECTION_MODEM = 1; //INTERNET_CONNECTION_LAN = 2; //INTERNET_CONNECTION_PROXY = 4; //INTERNET_CONNECTION_MODEM_BUSY = 8; { from the W2K SDK } INTERNET_RAS_INSTALLED = 16; INTERNET_CONNECTION_OFFLINE = 32; INTERNET_CONNECTION_CONFIGURED = 64; const winetdll = 'wininet.dll'; { from the W2K SDK } function InternetGetConnectedStateEx(lpdwFlags: LPDWORD; lpszConnectionName: LPSTR; dwNameLen: DWORD; dwReserved: DWORD): BOOL; stdcall; external winetdll name 'InternetGetConnectedStateExA'; {---------------------------------------------------------------------------------- Description : detects an internet connection Error checking : ? Notes : uses wininet.pas Author : Theo Bebekis - bebekis@otenet.gr -----------------------------------------------------------------------------------} function inet_IsConnected: boolean; var dwConnectionTypes : DWORD; begin dwConnectionTypes := 0; Result := InternetGetConnectedState(@dwConnectionTypes, 0); end; {---------------------------------------------------------------------------------- Description : detects an internet connection returns the connection description into the ConDes Error checking : ? Notes : uses wininet.pas Author : Theo Bebekis - bebekis@otenet.gr -----------------------------------------------------------------------------------} function inet_IsConnected(var ConDes: TConDescriptions): boolean; var S : string; begin Result := inet_IsConnected(ConDes, S); end; {---------------------------------------------------------------------------------- Description : detects an internet connection returns the connection description into the ConDes Error checking : ? Notes : uses Sysutils, wininet Author : Theo Bebekis - bebekis@otenet.gr -----------------------------------------------------------------------------------} function inet_IsConnected(var ConDes: TConDescriptions; var sConName : string): boolean; var dwConnectionTypes : DWORD; Buffer : array[0..MAX_PATH - 1] of char; begin dwConnectionTypes := 0; Result := InternetGetConnectedStateEx(@dwConnectionTypes, Buffer, Sizeof(Buffer)-1, 0); if Result then sConName := StrPas(Buffer); ConDes := []; //if dwConnectionTypes <> 0 then //IntToSet(ConDes, dwConnectionTypes ); if ( (dwConnectionTypes and INTERNET_CONNECTION_CONFIGURED ) = INTERNET_CONNECTION_CONFIGURED ) then Include(ConDes, cdConfigured ); if ( (dwConnectionTypes and INTERNET_CONNECTION_LAN ) = INTERNET_CONNECTION_LAN ) then Include(ConDes, ); if ( (dwConnectionTypes and INTERNET_CONNECTION_MODEM ) = INTERNET_CONNECTION_MODEM ) then Include(ConDes, m ); if ( (dwConnectionTypes and INTERNET_CONNECTION_OFFLINE ) = INTERNET_CONNECTION_OFFLINE ) then Include(ConDes, fline ); if ( (dwConnectionTypes and INTERNET_CONNECTION_PROXY ) = INTERNET_CONNECTION_PROXY ) then Include(ConDes, UsesProxy ); if ( (dwConnectionTypes and INTERNET_RAS_INSTALLED ) = INTERNET_RAS_INSTALLED ) then Include(ConDes, cdRasInstalled); end; ----------------------------------- Theo Bebekis Thessaloniki, Greece bebekis@otenet.gr ----------------------------------- _______________________________________________ Delphi mailing list -> Delphi@elists.org http://elists.org/mailman/listinfo/delphi