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.
Check For Internet
Moderator: Rathinagiri
- Roberto Lopez
- HMG Founder
- Posts: 4023
- Joined: Wed Jul 30, 2008 6:43 pm
Check For Internet
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
Re: Check For Internet
Hi Roberto,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.
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
In hope that usefulMsgInfo( iif( IsConnected(), "Internet is connected", "Internet is NOT connected" ) )
Kind Regards,
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
Grigory Filatov
"Everything should be made as simple as possible, but no simpler." Albert Einstein
- Roberto Lopez
- HMG Founder
- Posts: 4023
- Joined: Wed Jul 30, 2008 6:43 pm
Re: Check For Internet
You are a GENIUS man!
We all are very lucky of have you here!!!
Many Thanks!!
Regards/Saludos,
Roberto
(Veritas Filia Temporis)
Roberto
(Veritas Filia Temporis)
- 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
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
Thanks for nice code to check internet connection.
Is it possible to check if the internet connection is switched off while downloading ?
Thanks