Internet Connection Checker

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Internet Connection Checker

21 Dec 2015, 19:47

[Update 1] - Code improved (uses random file instead exitcode /random exitcode):
https://autohotkey.com/boards/viewtopic ... 040#p64040

[Update 2] - (uses exitcode /random exitcode instead random file):
https://autohotkey.com/boards/viewtopic.php?f=6&t=12399

Update 1 and Update 2 are not reliable, they use ping cmd! (Using cmd.exe or ping.exe to check internet connection is not reliable at all, see why in [Update 2]!)

[Update 3] - (DllCall and Com Ping Version - Seems to be reliable):
https://autohotkey.com/boards/viewtopic ... 440#p64440

Update 3 is the most reliable! (DllCall version particularly! It is the most reliable, safe and faster!)

Check every 10 seconds:



(Ignore the notes below "guiclose")

Code: Select all

gui, add, text, x65 y20, Status:
gui, add, text, x+5 vStatus, Please Wait! Checking for Internet Connection!
gui, add, text, x65 y+20, Last Check:
gui, add, text, x+5 w150 vLastCheck, Wait ...!
gui, add, text, x65 y+20, (Check Internet Connection every 10 seconds!)

gui, add, text, x70 y+20, by, InFiLLion

gui, show, w400 h150, Internet Connection Checker!

InternetCheck:

guicontrol, , Status, Please Wait! Checking for Internet Connection!

PingFileName = %a_Sec%%a_MSec%	;%a_Sec% current windows os seconds
				;%a_MSec% current windows os milliseconds

				;for every internet check, the file name will be diferrent (randomized)

run, cmd /c ipconfig /flushdns && ping www.google.com && Type Nul > "%a_workingdir%\%PingFileName%", , hide UseErrorLevel, PingPid

			;"ping www.google.com" ping 4 times
			;"ping -n 1 www.google.com" ping 1 time	
			;"ping -n 10 www.google.com" ping 10 times

			;"%a_workingdir%\%PingFileName%" must use quotation marks (""), otherwise, if path contains "space" character, etc, errors may happen
		
if ErrorLevel	;if "cmd.exe" or "ping.exe" files are not found
{
msgbox, Fail! "cmd.exe" or "ping.exe" files not found!
exitapp
}

settimer, CheckPingFileName, -10000	;"-10.000" check once after 10 seconds
return

	CheckPingFileName:

	IfExist, %A_WorkingDir%\%PingFileName%		;if %PingFileName% exist in script "working dir"
	{
	FileDelete, %A_WorkingDir%\%PingFileName%
	guicontrol, , Status, Wait 10 seconds ...!
	guicontrol, , LastCheck, Success - Connected!
	settimer, InternetCheck, -10000			;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
	}
	else
	{
	guicontrol, , Status, Wait 10 seconds ...!	
	guicontrol, , LastCheck, Fail - Not Connected!
	settimer, InternetCheck, -10000			;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
	}

	return


guiclose:
process, close, %PingPid%			;close "cmd.exe" executed by autohotkey through its unique process id (pid) if process still exist
process, close, ping.exe			;close "ping.exe" executed by %PingPid% if process still exist
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp


	
	;"&&" if "ipconfig /flushdns" succeed, "ping www.google.com" will be executed, if ping succeed, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
	;"&" if "ipconfig /flushdns" fails, "ping www.google.com" will be executed anyway
	;"||" if "ping www.google.com" doesn't succeed (if google ping fails), "exit" will be executed
	;if "ipconfig /flushdns" fails, "ping www.google.com" will not be executed and google ping will be considered as a failure, so, "Type Nul > %a_workingdir%\%PingFileName%" will not be executed

	;"ipconfig /flushdns" neccessary because, if internet goes down, dns cache will be refreshed, so ping to goolge will fail immediately
	;"Type Nul > %a_workingdir%\%PingFileName%" if ping to google succeed,  file named "%PingFileName%" will be created at script "working directory"

	;"hide" cmd windows will not show up (will be hidden)
	;"UseErrorLevel" if "cmd.exe" or "ping.exe" is not found, autohotkey will use "ErrorLevel" instead showing a default "warning message box" and 	then stop lines bellow execution!
		;by using "ErrorLevel", the programmer will decide what will happen if file not found (in this case "cmd.exe" or "ping.exe" )
	;"PingPid" stores "cmd.exe" unique process id (Pid) executed by autohotkey in "PingPid" variable! ("PingPid" coud be any other name, for exampe, "PidPing, etc")

	;"/k" prevents cmd from closing after ping process, if "exit" command line is not used (if "pause" command line is used, cmd will not exit after pressing any keyboard button, unless "exit" command line is used)
	;"/k" command lines are shown in cmd title windows - example: "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")"
	;"/c" instead "/k" , cmd will exit after ping process, (if "pause" command line is used, cmd will exit after pressing any keyboard button)
	;"/c" command lines are not shown in cmd title windows - example: no "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")" will appear
	; without "/k" or "/c" , cmd will open, but the "batchcode" lines  will not be executed 


/* ;notes.......................................................	;commands from the right and bellow   /*   will not be executed    ..............................................................................................

& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

| [...] means that all commands will run at same time, but if one command fails to open, all command will fail too regardless their position (example):

	notepad | mspaint | control

	if notepad fails, all will fail too ; if control fails, all will fail too ; if mspaint fails, all will fail too

|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

( ) [...] (command1 & command2)
Use to group or nest multiple commands.

; or , command1 parameter1;parameter2
Use to separate command parameters.

*/ ;notes.......................................................	;commands from the right and bellow   */   will be executed     ............................................................................................
Check only once at script execution (Ignore the notes below "guiclose"):
cmd - internet check - 97_ if random file exist - check once at app startup.jpg

Code: Select all

gui, add, text, x90 y65, Please Wait! Checking for Internet Connection!
gui, show, w400 h150, Checking Internet Connection ...!


PingFileName = %a_Sec%%a_MSec%	;%a_Sec% current windows os seconds
				;%a_MSec% current windows os milliseconds

				;for every internet check, the file name will be diferrent (randomized)

run, cmd /c ipconfig /flushdns && ping www.google.com && Type Nul > "%a_workingdir%\%PingFileName%", , hide UseErrorLevel, PingPid

			;"ping www.google.com" ping 4 times
			;"ping -n 1 www.google.com" ping 1 time	
			;"ping -n 10 www.google.com" ping 10 times

			;"%a_workingdir%\%PingFileName%" must use quotation marks (""), otherwise, if path contains "space" character, etc, errors may happen
		
if ErrorLevel	;if "cmd.exe" or "ping.exe" files are not found
{
msgbox, Fail! "cmd.exe" or "ping.exe" files not found!
exitapp
}

settimer, CheckPingFileName, -10000	;"-10.000" check once after 10 seconds
return

	CheckPingFileName:

	gui, destroy

	IfExist, %A_WorkingDir%\%PingFileName%		;if %PingFileName% exist in script "working dir"
	{
	msgbox, Sucess! Connected!
	}
	else
	msgbox, Fail! Not Connected!

	FileDelete, %A_WorkingDir%\%PingFileName%

	exitapp


guiclose:
process, close, %PingPid%			;close "cmd.exe" executed by autohotkey through its unique process id (pid) if process still exist
process, close, ping.exe			;close "ping.exe" executed by %PingPid% if process still exist
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp


	
	;"&&" if "ipconfig /flushdns" succeed, "ping www.google.com" will be executed, if ping succeed, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
	;"&" if "ipconfig /flushdns" fails, "ping www.google.com" will be executed anyway
	;"||" if "ping www.google.com" doesn't succeed (if google ping fails), "exit" will be executed
	;if "ipconfig /flushdns" fails, "ping www.google.com" will not be executed and google ping will be considered as a failure, so, "Type Nul > %a_workingdir%\%PingFileName%" will not be executed

	;"ipconfig /flushdns" neccessary because, if internet goes down, dns cache will be refreshed, so ping to goolge will fail immediately
	;"Type Nul > %a_workingdir%\%PingFileName%" if ping to google succeed,  file named "%PingFileName%" will be created at script "working directory"

	;"hide" cmd windows will not show up (will be hidden)
	;"UseErrorLevel" if "cmd.exe" or "ping.exe" is not found, autohotkey will use "ErrorLevel" instead showing a default "warning message box" and 	then stop lines bellow execution!
		;by using "ErrorLevel", the programmer will decide what will happen if file not found (in this case "cmd.exe" or "ping.exe" )
	;"PingPid" stores "cmd.exe" unique process id (Pid) executed by autohotkey in "PingPid" variable! ("PingPid" coud be any other name, for exampe, "PidPing, etc")

	;"/k" prevents cmd from closing after ping process, if "exit" command line is not used (if "pause" command line is used, cmd will not exit after pressing any keyboard button, unless "exit" command line is used)
	;"/k" command lines are shown in cmd title windows - example: "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")"
	;"/c" instead "/k" , cmd will exit after ping process, (if "pause" command line is used, cmd will exit after pressing any keyboard button)
	;"/c" command lines are not shown in cmd title windows - example: no "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")" will appear
	; without "/k" or "/c" , cmd will open, but the "batchcode" lines  will not be executed 


/* ;notes.......................................................	;commands from the right and bellow   /*   will not be executed    ..............................................................................................

& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

| [...] means that all commands will run at same time, but if one command fails to open, all command will fail too regardless their position (example):

	notepad | mspaint | control

	if notepad fails, all will fail too ; if control fails, all will fail too ; if mspaint fails, all will fail too

|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

( ) [...] (command1 & command2)
Use to group or nest multiple commands.

; or , command1 parameter1;parameter2
Use to separate command parameters.

*/ ;notes.......................................................	;commands from the right and bellow   */   will be executed     ............................................................................................
Last edited by silvex3000 on 28 Jun 2016, 12:48, edited 7 times in total.
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: Internet Connection Checker

23 Dec 2015, 13:59

Or

Code: Select all

ConnectedTo(URL) {
    RunWait, ping.exe %url% -n 1,, Hide UseErrorlevel
    Return !ErrorLevel
}
Or

Code: Select all

ConnectedToInternet(flag=0x40) {
    Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0)
}
   
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"
User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Re: Internet Connection Checker

23 Dec 2015, 17:19

Bon wrote:Or

Code: Select all

ConnectedTo(URL) {
    RunWait, ping.exe %url% -n 1,, Hide UseErrorlevel
    Return !ErrorLevel
}
I don't like to use "RunWait" command, it pauses current loop, settimer, etc thread in execution!

and by tha way, cmd errorlevel is not reliable, because, for examle:

- if "ping.exe" process is terminated from cmd windows X button, Offline message box shows up! (Fine!)
- if "ping.exe" process is terminated from taskmanager, Offline message box shows up! (Fine!)
- if "ping.exe" process is terminated from a batch file that contains "taskkill /f /im ping.exe", Offline message box shows up! (Fine!)

but ....,

- if "ping.exe" process is terminated from an autohotkey script that contains "process, close, ping.exe", OnLine message box shows up! (not good! Offline message box should show up instead!)

you can try it for yourself with this code:
cmd - internet check - #_ cmd is not reliable (ping as example).jpg
cmd - internet check - #_ cmd is not reliable (ping as example).jpg (33.05 KiB) Viewed 6729 times

Code: Select all


settimer, gui, -2000

CheckNet:

runwait, ping -n 10 www.google.com
if errorlevel
msgbox, Off-Line
else
msgbox, Online

return

gui:
gui, add, text, , Close cmd windows from "x" button `n (will always show up Off-Line message Box - Nice!)
gui, add, text, , kill "ping.exe" through task manager `n (will always show up Off-Line message Box - Nice!)
gui, add, text, , F11 = run, taskkill /f /im ping.exe = kill "ping.exe" with cmd command `n (will always show up Off-Line message Box - Nice!)
gui, add, text, , F12 = process, close, ping.exe = kill "ping.exe" with AutoHotkey `n (will always show up Online message Box - Not good!)
gui, add, button, xm gCheckNet, Ping Again
gui, show, ,  Process Closer

return

f11::
run, taskkill /f /im ping.exe
return

f12::
process, close, ping.exe
return

guiclose:
exitapp

Bon wrote:

Code: Select all

ConnectedToInternet(flag=0x40) {
    Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0)
}
this one will only check if computer is connected to a router/switch (through wireless or cable), the code doesn't check for internet connection
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Internet Connection Checker

25 Dec 2015, 22:29

This might interest you.
see 3 different ping methods : https://autohotkey.com/boards/viewtopic ... 3292#p3292
cheers ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Re: Internet Connection Checker

26 Dec 2015, 23:15

Thanks "joedf", I will Check them out!

by tha way, I already found a solution for "process, close, ping.exe", but if anyone finds a better one, please post it here!
cmd - internet check - 95_ string find instead flush dns.jpg
(Ignore the notes below "guiclose")

Code: Select all


	;[Fixed] 
	;If Off-Line, by adding (| findstr /i "ttl") in the "cmd" script, no random file will be generated  anymore if "ping.exe" process is terminated from the following Autohotkey command: "process, close, ping.exe"!

	;Script will not be paused while checking for internet connection

	;"ipconfig /flushdns" removed from cmd script
	;(| findstr /i "ttl") added in "cmd" script

	;Random Ping File Name Improved

	;"PingFileName" path can be changed to windows temp folder instead script working directory, though it is not necessary since the file name is randomized

gui, add, text, x65 y20, Status:
gui, add, text, x+5 vStatus, Please Wait! Checking for Internet Connection!
gui, add, text, x65 y+20, Last Check:
gui, add, text, x+5 w150 vLastCheck, Wait ...!
gui, add, text, x65 y+20, "F12" = "Process, Close, Ping.exe"
gui, add, text, x65 y+20, (Check Internet Connection every 10 seconds!)

gui, add, text, x70 y+20, by, InFiLLion

gui, show, w400 h150, Internet Connection Checker!

InternetCheck:

guicontrol, , Status, Please Wait! Checking for Internet Connection!


Random, PingFileName, 0, 10000		;for every internet check, the file name will be diferrent (randomized from 0 to 10.000)


PingFileName = %a_Sec%%PingFileName%%a_MSec%		;%a_Sec% current windows os seconds
						;%a_MSec% current windows os milliseconds	
						;improve File Name randomization
						

run, cmd /c ping www.google.com | findstr /i "ttl" && Type Nul > "%a_workingdir%\%PingFileName%", , hide UseErrorLevel, PingPid

			;"ping www.google.com" ping 4 times
			;"ping -n 1 www.google.com" ping 1 time	
			;"ping -n 10 www.google.com" ping 10 times

			;"%a_workingdir%\%PingFileName%" must use quotation marks (""), otherwise, if path contains "space" character, etc, errors may happen
		
if ErrorLevel	;if "cmd.exe" or "ping.exe" files are not found
{
msgbox, Fail! "cmd.exe" or "ping.exe" files not found!
exitapp
}

settimer, CheckPingFileName, -10000	;"-10.000" check once after 10 seconds
return

	CheckPingFileName:

	IfExist, %A_WorkingDir%\%PingFileName%		;if %PingFileName% exist in script "working dir"
	{
	FileDelete, %A_WorkingDir%\%PingFileName%
	guicontrol, , Status, Wait 10 seconds ...!
	guicontrol, , LastCheck, Success - Connected!
	settimer, InternetCheck, -10000			;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
	}
	else
	{
	guicontrol, , Status, Wait 10 seconds ...!	
	guicontrol, , LastCheck, Fail - Not Connected!
	settimer, InternetCheck, -10000			;"-10000"after 10 seconds, "InternetCheck" lable will be executed only once
	}

	return


f12::
Process, Close, Ping.exe
return


guiclose:
process, close, %PingPid%			;close "cmd.exe" executed by autohotkey through its unique process id (pid) if process still exist
process, close, ping.exe			;close "ping.exe" executed by %PingPid% if process still exist
FileDelete, %A_WorkingDir%\%PingFileName%
exitapp


	
	;"&&" if "ipconfig /flushdns" succeed, "ping www.google.com" will be executed, if ping succeed, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
	      ;by using (| findstr /i "ttl"), "ipconfig /flushdns" is not necessay anymore
	;"&" if "ipconfig /flushdns" fails, "ping www.google.com" will be executed anyway
	;"||" if "ping www.google.com" doesn't succeed (if google ping fails), "exit" will be executed
	;if "ipconfig /flushdns" fails, "ping www.google.com" will not be executed and google ping will be considered as a failure, so, "Type Nul > %a_workingdir%\%PingFileName%" will not be executed

	;"ipconfig /flushdns" neccessary because, if internet goes down, dns cache will be refreshed, so ping to goolge will fail immediately
	;"Type Nul > %a_workingdir%\%PingFileName%" if ping to google succeed,  file named "%PingFileName%" will be created at script "working directory"
	;(| findstr /i "ttl") if "ttl" string is found in "ping www.google.com" output, "Type Nul > %a_workingdir%\%PingFileName%" will be executed
	      ;"ipconfig /flushdns"  is not necessary if (| findstr /i "ttl") is in use

		;( | findstr /i "ttl" ) only shows output/results lines from previous "ping" that contains "ttl" string
		; if "ttl" is found, it means that ping succeed 
		;( | findstr - find string/word/Characters) - ( /i - ignores case sensitives "will find tTl or ttL or TTL")

	;"hide" cmd windows will not show up (will be hidden)
	;"UseErrorLevel" if "cmd.exe" or "ping.exe" is not found, autohotkey will use "ErrorLevel" instead showing a default "warning message box" and 	then stop lines bellow execution!
		;by using "ErrorLevel", the programmer will decide what will happen if file not found (in this case "cmd.exe" or "ping.exe" )
	;"PingPid" stores "cmd.exe" unique process id (Pid) executed by autohotkey in "PingPid" variable! ("PingPid" coud be any other name, for exampe, "PidPing, etc")

	;"/k" prevents cmd from closing after ping process, if "exit" command line is not used (if "pause" command line is used, cmd will not exit after pressing any keyboard button, unless "exit" command line is used)
	;"/k" command lines are shown in cmd title windows - example: "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")"
	;"/c" instead "/k" , cmd will exit after ping process, (if "pause" command line is used, cmd will exit after pressing any keyboard button)
	;"/c" command lines are not shown in cmd title windows - example: no "timeout /t 30" or "start /b ping -n 1 -w 1 -a 192.168.1.25 | start /b findstr /i "[ ttl")" will appear
	; without "/k" or "/c" , cmd will open, but the "batchcode" lines  will not be executed 


/* ;notes.......................................................	;commands from the right and bellow   /*   will not be executed    ..............................................................................................

& [...] command1 & command2
Use to separate multiple commands on one command line. Cmd.exe runs the first command, and then the second command.

&& [...] command1 && command2
Use to run the command following && only if the command preceding the symbol is successful. Cmd.exe runs the first command, and then runs the second command only if the first command completed successfully.

| [...] means that all commands will run at same time, but if one command fails to open, all command will fail too regardless their position (example):

	notepad | mspaint | control

	if notepad fails, all will fail too ; if control fails, all will fail too ; if mspaint fails, all will fail too

|| [...] command1 || command2
Use to run the command following || only if the command preceding || fails. Cmd.exe runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).

( ) [...] (command1 & command2)
Use to group or nest multiple commands.

; or , command1 parameter1;parameter2
Use to separate command parameters.

*/ ;notes.......................................................	;commands from the right and bellow   */   will be executed     ............................................................................................

User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Re: Internet Connection Checker

29 Dec 2015, 17:28

joedf wrote:This might interest you.
see 3 different ping methods : https://autohotkey.com/boards/viewtopic ... 3292#p3292
cheers ;)
Thank you very much one more time "joedf"!

But I must say this first (just my opinion),

You senior guys from this forum should post more practical scripts (with practical examples) in order to amateurs like me can understand them! (with all the respect!)

saying that, here are 2 practical scripts I wrote from those you mentioned in the link above (I hope they are ok):

ping - Com Version

Code: Select all


	;Suggested by "joedf"

host = 8.8.8.8	;8.8.8.8 Google IP address or use www.google.com
		;using 8.8.8.8 is faster than using www.google.com, and script won't pause or stays unresponsive or even crash if offline or if internet connection goes down
		

gui, add, text, x90 y65, Please Wait! Checking for Internet Connection!
gui, show, w400 h150, Checking Internet Connection!

	;CheckInternet := ping(host) 	;"ping(host)" see function at the end of the script 

	;if CheckInternet = Online

if ping(host) = "Online"		;"ping(host)" see function at the end of the script 
settimer, Online, -10000
else
settimer, OffLine, -10000
return


	Online:
	gui, destroy
	msgBox, Online - Connected
	goto, guiclose
	
	OffLine:
	gui, destroy
	msgBox, OffLine - Not Connected
	goto, guiclose


guiclose:
exitapp


ping(host) 
{	
colPings := ComObjGet( "winmgmts:" ).ExecQuery("Select * From Win32_PingStatus where Address = '" host "'")._NewEnum

While colPings[objStatus]
Return ((oS:=(objStatus.StatusCode="" or objStatus.StatusCode<>0)) ? "Offline" : "Online" )
}



ping - Dllcall version

Code: Select all


	;by "uberi" - Suggested by "joedf"

Address = 8.8.8.8	;8.8.8.8 Google IP address or use www.google.com
		;using 8.8.8.8 is faster than using www.google.com, and script won't pause or stays unresponsive or even crash if offline or if internet connection goes down

TimeOut = 1000	;recommended by "joedf"

gui, add, text, x90 y65, Please Wait! Checking for Internet Connection!
gui, show, w400 h150, Checking Internet Connection!

	;CheckInternet := ping(Address, TimeOut)	;"ping(Address, TimeOut)" see function at the end of the script 

	;if CheckInternet = OffLine

if ping(Address, TimeOut) = "OffLine"	;"ping(Address, TimeOut)" see function at the end of the script 
settimer, OffLine, -10000
else
settimer, Online, -10000
return


	Online:
	gui, destroy
	msgBox, Online - Connected
	goto, guiclose
	
	OffLine:
	gui, destroy
	msgBox, OffLine - Not Connected
	goto, guiclose


guiclose:
exitapp


Ping(Address, Timeout, ByRef Data = "",Length = 0,ByRef Result = "",ByRef ResultLength = 0)
{
    NumericAddress := DllCall("ws2_32\inet_addr","AStr",Address,"UInt")
    If NumericAddress = 0xFFFFFFFF ;INADDR_NONE
        Return "OffLine"
 
    If DllCall("LoadLibrary","Str","icmp","UPtr") = 0 ;NULL
        Return "OffLine"
 
    hPort := DllCall("icmp\IcmpCreateFile","UPtr") ;open port
    If hPort = -1 ;INVALID_HANDLE_VALUE
        Return "OffLine"
 
    StructLength := 278 ;ICMP_ECHO_REPLY structure
    VarSetCapacity(Reply,StructLength)
    Count := DllCall("icmp\IcmpSendEcho","UPtr",hPort,"UInt",NumericAddress,"UPtr",&Data,"UShort",Length,"UPtr",0,"UPtr",&Reply,"UInt",StructLength,"UInt",Timeout)
    If NumGet(Reply,4,"UInt") = 11001 ;IP_BUF_TOO_SMALL
    {
        VarSetCapacity(Reply,StructLength * Count)
        DllCall("icmp\IcmpSendEcho","UPtr",hPort,"UInt",NumericAddress,"UPtr",&Data,"UShort",Length,"UPtr",0,"UPtr",&Reply,"UInt",StructLength * Count,"UInt",Timeout)
    }
    If NumGet(Reply,4,"UInt") != 0 ;IP_SUCCESS
        Return "OffLine"
    If !DllCall("icmp\IcmpCloseHandle","UInt",hPort) ;close port
        Return "OffLine"
 
    ResultLength := NumGet(Reply,12,"UShort")
    VarSetCapacity(Result,ResultLength)
    DllCall("RtlMoveMemory","UPtr",&Result,"UPtr",NumGet(Reply,16),"UPtr",ResultLength)
    Return, NumGet(Reply,8,"UInt")
}

About the cmd ping version, I think it should not be recommended to anyone, since it is not reliable at all! (Again, in my opinion!)


by tha way, which one do you think is the most reliable/safe, com version or dllcall version? (Do you know if there is any way to hack them or bypass them?)
Last edited by silvex3000 on 01 Jan 2016, 19:03, edited 1 time in total.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Internet Connection Checker

29 Dec 2015, 19:25

Hmm! Both scripts seem to be just fine :)
I would go with the DllCall version since it is a more "direct" approach.
"bypass", I am not sure what you mean by that :?
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
silvex3000
Posts: 188
Joined: 19 Dec 2015, 22:42

Re: Internet Connection Checker

29 Dec 2015, 20:08

Thanks for the fast reply!
joedf wrote: "bypass", I am not sure what you mean by that :?
if there is any easy way to trick the scripts above to show up Online message box even though computer is not connected to the internet! (for example, cmd ping is very easy to be tricked! Is it the same for dllcall and com version?)
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Internet Connection Checker

29 Dec 2015, 21:13

DllCall does direct system function calls, unless you have some really malicious virus or users, I don't see it to be fooled, quite simply. ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: IfThenElse and 121 guests