Page 1 of 1

Check For Internet

Posted: Thu Jun 30, 2022 12:58 pm
by Roberto Lopez
Hi All,

I'm looking for a fast solution to check if Internet is working and differentiate that from LAN activity.

I mean, sometimes, LAN is working fine, but there is no Internet because an ISP problem.

I could run a ping and analyze the result, but it is somewhat slow for my needs.

Any idea is welcome.

TIA.

Re: Check For Internet

Posted: Thu Jun 30, 2022 1:51 pm
by gfilatov
Roberto Lopez wrote: Thu Jun 30, 2022 12:58 pm Hi All,

I'm looking for a fast solution to check if Internet is working and differentiate that from LAN activity.

I mean, sometimes, LAN is working fine, but there is no Internet because an ISP problem.

I could run a ping and analyze the result, but it is somewhat slow for my needs.

Any idea is welcome.

TIA.
Hi Roberto,

Please take a look for the following WinAPI-based functions:

Code: Select all

// Flags for InternetGetConnectedState and Ex

#define INTERNET_CONNECTION_MODEM       1
#define INTERNET_CONNECTION_LAN         2
#define INTERNET_CONNECTION_PROXY       4
#define INTERNET_CONNECTION_MODEM_BUSY  8 /* no longer used */
#define INTERNET_RAS_INSTALLED         16
#define INTERNET_CONNECTION_OFFLINE    32
#define INTERNET_CONNECTION_CONFIGURED 64

// Flag for InternetCheckConnection

#define FLAG_ICC_FORCE_CONNECTION       1

*--------------------------------------------------------*
FUNCTION IsConnected()
*--------------------------------------------------------*
	LOCAL nFlags := 0, lRet := .F.

	IF InternetGetConnectedState( @nFlags, 0 )
/*
		if nFlags == INTERNET_CONNECTION_CONFIGURED + INTERNET_RAS_INSTALLED + INTERNET_CONNECTION_PROXY + INTERNET_CONNECTION_LAN
			MsgInfo( "Internet Connection via LAN" )
		endif
*/
		IF InternetCheckConnection( "https://harbour.github.io", FLAG_ICC_FORCE_CONNECTION, 0 )

			lRet := .T.
		ENDIF

	ENDIF
  
RETURN lRet


#pragma BEGINDUMP

#include <windows.h>
#include "hbapi.h"

/*****************************************************************************************
*   MACRO DEFINITION FOR CALL DLL FUNCTION
******************************************************************************************/

#define HMG_DEFINE_DLL_FUNC(\
                             _FUNC_NAME,             \
                             _DLL_LIBNAME,           \
                             _DLL_FUNC_RET,          \
                             _DLL_FUNC_TYPE,         \
                             _DLL_FUNC_NAMESTRINGAW, \
                             _DLL_FUNC_PARAM,        \
                             _DLL_FUNC_CALLPARAM,    \
                             _DLL_FUNC_RETFAILCALL   \
                            )\
\
_DLL_FUNC_RET _DLL_FUNC_TYPE _FUNC_NAME _DLL_FUNC_PARAM \
{\
   typedef _DLL_FUNC_RET (_DLL_FUNC_TYPE *PFUNC) _DLL_FUNC_PARAM;\
   static PFUNC pfunc = NULL;\
   if( pfunc == NULL )\
   {\
      HMODULE hLib = LoadLibrary( _DLL_LIBNAME );\
      pfunc = (PFUNC) GetProcAddress( hLib, _DLL_FUNC_NAMESTRINGAW );\
   }\
   if( pfunc == NULL )\
      return( (_DLL_FUNC_RET) _DLL_FUNC_RETFAILCALL );\
   else\
      return pfunc _DLL_FUNC_CALLPARAM;\
}

HMG_DEFINE_DLL_FUNC( win_InternetGetConnectedState,                      // user function name
                     "Wininet.dll",                                      // dll name
                     BOOL,                                               // function return type
                     WINAPI,                                             // function type
                     "InternetGetConnectedState",                        // dll function name
                     (LPDWORD lpdwFlags, DWORD dwReserved),              // dll function parameters (types and names)
                     (lpdwFlags, dwReserved),                            // function parameters (only names)
                     FALSE                                               // return value if fail call function of dll
                   )

HMG_DEFINE_DLL_FUNC( win_InternetCheckConnection,                        // user function name
                     "Wininet.dll",                                      // dll name
                     BOOL,                                               // function return type
                     WINAPI,                                             // function type
                     "InternetCheckConnectionA",                         // dll function name
                     (LPCSTR lpszUrl, DWORD dwFlags, DWORD dwReserved),  // dll function parameters (types and names)
                     (lpszUrl, dwFlags, dwReserved),                     // function parameters (only names)
                     FALSE                                               // return value if fail call function of dll
                   )

HB_FUNC ( INTERNETGETCONNECTEDSTATE )
{
   DWORD dwFlags    = 0;
   DWORD dwReserved = hb_parnl( 2 );

   hb_retl( ( BOOL ) win_InternetGetConnectedState( &dwFlags, dwReserved ) );
   if( HB_ISBYREF( 1 ) )
       hb_stornl( dwFlags, 1 );
}

HB_FUNC ( INTERNETCHECKCONNECTION )
{
   char * lpszUrl   = ( char * ) hb_parc( 1 );
   DWORD dwFlags    = hb_parnl( 2 );
   DWORD dwReserved = hb_parnl( 3 );

   hb_retl( ( BOOL ) win_InternetCheckConnection( lpszUrl, dwFlags, dwReserved ) );
}

#pragma ENDDUMP
Usage:
MsgInfo( iif( IsConnected(), "Internet is connected", "Internet is NOT connected" ) )
In hope that useful :idea:

Re: Check For Internet

Posted: Thu Jun 30, 2022 2:41 pm
by Roberto Lopez
gfilatov wrote: Thu Jun 30, 2022 1:51 pm Hi Roberto,

Please take a look for the following WinAPI-based functions:
...
You are a GENIUS man!

We all are very lucky of have you here!!!

Many Thanks!!

Re: Check For Internet

Posted: Sun Jul 03, 2022 6:41 am
by serge_girard
Very usefull !

Serge

Re: Check For Internet

Posted: Fri Sep 02, 2022 7:44 pm
by RPC
Hi Grigory
Thanks for nice code to check internet connection.
Is it possible to check if the internet connection is switched off while downloading ?
Thanks