Check For Internet

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

Post Reply
User avatar
Roberto Lopez
HMG Founder
Posts: 4023
Joined: Wed Jul 30, 2008 6:43 pm

Check For Internet

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
gfilatov
Posts: 1116
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Check For Internet

Post 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:
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
Roberto Lopez
HMG Founder
Posts: 4023
Joined: Wed Jul 30, 2008 6:43 pm

Re: Check For Internet

Post 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!!
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
serge_girard
Posts: 3420
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Check For Internet

Post by serge_girard »

Very usefull !

Serge
There's nothing you can do that can't be done...
RPC
Posts: 312
Joined: Fri Feb 10, 2017 4:12 am
DBs Used: DBF

Re: Check For Internet

Post 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
Post Reply