Page 1 of 1
Check if user is connected to the internet
Posted: Tue Oct 01, 2024 11:31 pm
by Quin
I have an old app that uses the result of InitNetwork() to determine if the user is connected to the internet or not. I'm not 100% certain if this was correct in retrospect, but it worked. However, InitNetwork() is now removed. Is this still possible to do somehow?
Thanks.
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 12:16 am
by jacdelad
Hi Quin!
viewtopic.php?t=31623
Short answer: Windows offers the API function InternetGetConnectedState_(), which can be used like in the thread linked. Someone states it's not reliable in some cases, so i would simply try to connect to google.com or something similar (with ReceiveHTTPFile, which would make it multiplatform available).
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 1:58 am
by kenmo
Something like this?
Code: Select all
Procedure.i IsConnectedToInternet()
Connected = #False
CompilerIf #PB_Compiler_Version < 600
If (InitNetwork())
*Buffer = ReceiveHTTPMemory("captive.apple.com", #PB_HTTP_NoRedirect)
EndIf
CompilerElse
*Buffer = ReceiveHTTPMemory("captive.apple.com", #PB_HTTP_NoRedirect)
CompilerEndIf
If (*Buffer)
Connected = Bool(FindString(PeekS(*Buffer, MemorySize(*Buffer), #PB_Ascii), "Success"))
FreeMemory(*Buffer)
EndIf
ProcedureReturn Connected
EndProcedure
Debug IsConnectedToInternet()
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 6:29 am
by RASHAD
Using Windows API
Code: Select all
#FLAG_ICC_FORCE_CONNECTION = 1
If InternetCheckConnection_(@"http://www.google.com",#FLAG_ICC_FORCE_CONNECTION,0) = 1
Debug "OK"
Else
Debug "NO"
EndIf
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 8:50 am
by PBJim
I saw a comment elsewhere that suggests the InternetCheckConnection API is deprecated and does not work with proxy servers.
INetworkListManager::GetConnectivity method (netlistmgr.h) was recommended :
https://learn.microsoft.com/en-us/windo ... nnectivity
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 1:21 pm
by blueb
Saw this from mk-soft
Code: Select all
;- mk-soft May 17, 2021
; INTERNET_CONNECTION_CONFIGURED; 0x40
; Local system has a valid connection to the Internet, but it might or might not be currently connected.
; INTERNET_CONNECTION_LAN; 0x02
; Local system uses a local area network to connect to the Internet.
; INTERNET_CONNECTION_MODEM; 0x01
; Local system uses a modem to connect to the Internet.
; INTERNET_CONNECTION_MODEM_BUSY; 0x08
; No longer used.
; INTERNET_CONNECTION_OFFLINE; 0x20
; Local system is in offline mode.
; INTERNET_CONNECTION_PROXY; 0x04
; Local system uses a proxy server to connect to the Internet.
; INTERNET_RAS_INSTALLED; 0x10
; Local system has RAS installed.
lpdwFlags.l
r1 = InternetGetConnectedState_(@lpdwFlags, 0)
If r1
Debug "Online"
Else
Debug "Offline"
EndIf
Debug "Flags: 0x" + Hex(lpdwFlags)
Re: Check if user is connected to the internet
Posted: Wed Oct 02, 2024 2:17 pm
by Quin
Thanks all for your answers!
Ended up going with Kenmo's solution as I need this app to be cross-platform, but I can at least say I learned something about the Windows API today

Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 10:19 am
by BarryG
Does Kenmo's solution work on a non-English OS? It checks for the word "Success" which might not be the return value on a non-English OS, or on the remote server.
Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 11:23 am
by infratec
Then you should use timeout.
Else you have to wait a long time if you have no connection.
Code: Select all
Procedure.i IsConnectedToInternet()
Protected.i Connected, HTTPRequest
HTTPTimeout(1000)
HTTPRequest = HTTPRequest(#PB_HTTP_Get, "https://captive.apple.com", "", #PB_HTTP_HeadersOnly|#PB_HTTP_NoSSLCheck)
If HTTPRequest
Connected = #True
FinishHTTP(HTTPRequest)
EndIf
ProcedureReturn Connected
EndProcedure
Debug IsConnectedToInternet()
Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 11:28 am
by infratec
But I think this is faster and takes less resources:
Code: Select all
Procedure.i IsConnectedToInternet()
Protected.i Connection
Connection = OpenNetworkConnection("captive.apple.com", 443, #PB_Network_TCP, 500)
If Connection
CloseNetworkConnection(Connection)
EndIf
ProcedureReturn Connection
EndProcedure
If IsConnectedToInternet()
Debug "Connected"
Else
Debug "Not connected"
EndIf
Because this opens only the connection without making a ssl handshake.
You can check this stuff with wireshark to see the differences.
Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 12:28 pm
by Little John
Thanks infratec, much appreciated!
Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 12:29 pm
by Quin
BarryG wrote: Thu Oct 03, 2024 10:19 am
Does Kenmo's solution work on a non-English OS? It checks for the word "Success" which might not be the return value on a non-English OS, or on the remote server.
Don't think Apple's captive server checks the locale of the user pinging it, so it should
Re: Check if user is connected to the internet
Posted: Thu Oct 03, 2024 12:30 pm
by Quin
infratec wrote: Thu Oct 03, 2024 11:28 am
But I think this is faster and takes less resources:
Code: Select all
Procedure.i IsConnectedToInternet()
Protected.i Connection
Connection = OpenNetworkConnection("captive.apple.com", 443, #PB_Network_TCP, 500)
If Connection
CloseNetworkConnection(Connection)
EndIf
ProcedureReturn Connection
EndProcedure
If IsConnectedToInternet()
Debug "Connected"
Else
Debug "Not connected"
EndIf
Because this opens only the connection without making a ssl handshake.
You can check this stuff with wireshark to see the differences.
A wizard as always, Infratec. Thanks a ton!
