how to detect web connection is lost?

Get help with using AutoHotkey and its commands and hotkeys
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

how to detect web connection is lost?

20 Oct 2013, 03:50

i give you a specific example. let's says you connect to http://www.booktv.org/Live/C-SPAN2.aspx with Firefox and sometimes (unpredictable) the connection drops and the page needs to be refreshed. is there anyway to have AHK detect when this happens and automatically refresh the page?
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

20 Oct 2013, 06:51

Code: Select all

^space:: ;control space hotkey
SetTimer,CheckInternet,100
return

Escape::
exitapp

CheckInternet:
Connected := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0) 
if Connected = 0
 msgbox You have lost internet connection
return
Edit: added hotkey control space to start the timer and the Escape hotkey to exit the script.
Last edited by DataLife on 22 Oct 2013, 23:29, edited 1 time in total.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

20 Oct 2013, 07:06

Another example that refreshes the page only when the connection has been lost and reconnected.

Code: Select all

InitialState := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0)  ;get initial state

Loop
 {
  CurrentState := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0)
  if ( InitialState <> CurrentState )
   {
     If CurrentState = 1
      Send {F5} ;you may have to do some other stuff here, like activate firefox, make sure it is on the right tab
     InitialState = %CurrentState%
   }
   sleep 1000
 }
return
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

20 Oct 2013, 11:21

thanks, but when i run this script it does not do anything. i even added an else line below. :lol: HOWEVER, i must add maybe the Internet disconnected is not the right way to describe this situation. the flash application (window) within the page i am watching stops playing (normally after a long time). the Internet connection to the page and other tabs are not dropped.
DataLife wrote:

Code: Select all

SetTimer,CheckInternet,100
return
CheckInternet:
Connected := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0) 
if Connected = 0
msgbox You have lost internet connection
else
msgbox You are connected
return
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

20 Oct 2013, 17:27

Guest10 wrote:thanks, but when i run this script it does not do anything. i even added an else line below. :lol: HOWEVER, i must add maybe the Internet disconnected is not the right way to describe this situation.
I am sure someone can come up with a better solution, but the only way I can think of now is to take a screen shot of an area of the video you are watching every few seconds or minutes. Compare the 1st screenshot with the 2nd screenshot and if they are the same then the video has stopped, then refresh the page.

Try this example. Choose the area of your screen you want to monitor. In this example the top left is x100 y100 and the bottom right of the rectangle is x500 y500.

Start the video, press control space, a traytip will tell you the video is running. Stop the video and a message box will open telling you to refresh the page.

Code: Select all

;Thanks to Sean - Screen Capture from http://www.autohotkey.com/board/topic/16677-screen-capture-with-transparent-windows-and-mouse-cursor/#entry108113
;Thanks to Rseding91 - Calc_MD5 function from http://www.autohotkey.com/board/topic/77408-md5-function-for-comparing-images/
^space:: ;press control space to start monitoring video
Loop
 {
  CaptureScreen( "100, 100, 500, 500" ,, "temp1.jpg")
  FileRead,File,%A_ScriptDir%\temp1.jpg
  FileGetSize,FileSize,%A_ScriptDir%\temp1.jpg
  MD5Value1 := Calc_MD5(&File,FileSize)

  sleep 2000

  CaptureScreen( "100, 100, 500, 500" ,, "temp2.jpg")
  FileRead,File,%A_ScriptDir%\temp2.jpg
  FileGetSize,FileSize,%A_ScriptDir%\temp2.jpg
  MD5Value2 := Calc_MD5(&File,FileSize)

  If MD5Value1 = %MD5Value2%
   MsgBox refresh screen`n %md5value1%`n%md5value2%
  else
   TrayTip,Video is, running,3
 }
return
escape::
exitapp

/* CaptureScreen(aRect, bCursor, sFileTo, nQuality)
1) If the optional parameter bCursor is True, captures the cursor too.
2) If the optional parameter sFileTo is 0, set the image to Clipboard.
   If it is omitted or "", saves to screen.bmp in the script folder,
   otherwise to sFileTo which can be BMP/JPG/PNG/GIF/TIF.
3) The optional parameter nQuality is applicable only when sFileTo is JPG. Set it to the desired quality level of the resulting JPG, an integer between 0 - 100.
4) If aRect is 0/1/2/3, captures the entire desktop/active window/active client area/active monitor.
5) aRect can be comma delimited sequence of coordinates, e.g., "Left, Top, Right, Bottom" or "Left, Top, Right, Bottom, Width_Zoomed, Height_Zoomed".
   In this case, only that portion of the rectangle will be captured. Additionally, in the latter case, zoomed to the new width/height, Width_Zoomed/Height_Zoomed.

Example:
CaptureScreen(0)
CaptureScreen(1)
CaptureScreen(2)
CaptureScreen(3)
CaptureScreen("100, 100, 200, 200")
CaptureScreen("100, 100, 200, 200, 400, 400")   ; Zoomed
*/

/* Convert(sFileFr, sFileTo, nQuality)
Convert("C:\image.bmp", "C:\image.jpg")
Convert("C:\image.bmp", "C:\image.jpg", 95)
Convert(0, "C:\clip.png")   ; Save the bitmap in the clipboard to sFileTo if sFileFr is "" or 0.
*/

CaptureScreen(aRect = 0, bCursor = False, sFile = "", nQuality = "")
{
	If	!aRect
	{
		SysGet, nL, 76  ; virtual screen left & top
		SysGet, nT, 77
		SysGet, nW, 78	; virtual screen width and height
		SysGet, nH, 79
	}
	Else If	aRect = 1
		WinGetPos, nL, nT, nW, nH, A
	Else If	aRect = 2
	{
		WinGet, hWnd, ID, A
		VarSetCapacity(rt, 16, 0)
		DllCall("GetClientRect" , "Uint", hWnd, "Uint", &rt)
		DllCall("ClientToScreen", "Uint", hWnd, "Uint", &rt)
		nL := NumGet(rt, 0, "int")
		nT := NumGet(rt, 4, "int")
		nW := NumGet(rt, 8)
		nH := NumGet(rt,12)
	}
	Else If	aRect = 3
	{
		VarSetCapacity(mi, 40, 0)
		DllCall("GetCursorPos", "int64P", pt)
		DllCall("GetMonitorInfo", "Uint", DllCall("MonitorFromPoint", "int64", pt, "Uint", 2), "Uint", NumPut(40,mi)-4)
		nL := NumGet(mi, 4, "int")
		nT := NumGet(mi, 8, "int")
		nW := NumGet(mi,12, "int") - nL
		nH := NumGet(mi,16, "int") - nT
	}
	Else
	{
		StringSplit, rt, aRect, `,, %A_Space%%A_Tab%
		nL := rt1	; convert the Left,top, right, bottom into left, top, width, height
		nT := rt2
		nW := rt3 - rt1
		nH := rt4 - rt2
		znW := rt5
		znH := rt6
	}

	mDC := DllCall("CreateCompatibleDC", "Uint", 0)
	hBM := CreateDIBSection(mDC, nW, nH)
	oBM := DllCall("SelectObject", "Uint", mDC, "Uint", hBM)
	hDC := DllCall("GetDC", "Uint", 0)
	DllCall("BitBlt", "Uint", mDC, "int", 0, "int", 0, "int", nW, "int", nH, "Uint", hDC, "int", nL, "int", nT, "Uint", 0x40000000 | 0x00CC0020)
	DllCall("ReleaseDC", "Uint", 0, "Uint", hDC)
	If	bCursor
		CaptureCursor(mDC, nL, nT)
	DllCall("SelectObject", "Uint", mDC, "Uint", oBM)
	DllCall("DeleteDC", "Uint", mDC)
	If	znW && znH
		hBM := Zoomer(hBM, nW, nH, znW, znH)
	If	sFile = 0
		SetClipboardData(hBM)
	Else	Convert(hBM, sFile, nQuality), DllCall("DeleteObject", "Uint", hBM)
}

CaptureCursor(hDC, nL, nT)
{
	VarSetCapacity(mi, 20, 0), mi := Chr(20)
	DllCall("GetCursorInfo", "Uint", &mi)
	bShow   := NumGet(mi, 4)
	hCursor := NumGet(mi, 8)
	xCursor := NumGet(mi,12)
	yCursor := NumGet(mi,16)

	If	bShow && hCursor:=DllCall("CopyIcon", "Uint", hCursor)
	{
	VarSetCapacity(ni, 20, 0)
	DllCall("GetIconInfo", "Uint", hCursor, "Uint", &ni)
	bIcon    := NumGet(ni, 0)
	xHotspot := NumGet(ni, 4)
	yHotspot := NumGet(ni, 8)
	hBMMask  := NumGet(ni,12)
	hBMColor := NumGet(ni,16)

	DllCall("DrawIcon", "Uint", hDC, "int", xCursor - xHotspot - nL, "int", yCursor - yHotspot - nT, "Uint", hCursor)
	DllCall("DestroyIcon", "Uint", hCursor)
	If	hBMMask
	DllCall("DeleteObject", "Uint", hBMMask)
	If	hBMColor
	DllCall("DeleteObject", "Uint", hBMColor)
	}
}

Zoomer(hBM, nW, nH, znW, znH)
{
	mDC1 := DllCall("CreateCompatibleDC", "Uint", 0)
	mDC2 := DllCall("CreateCompatibleDC", "Uint", 0)
	zhBM := CreateDIBSection(mDC2, znW, znH)
	oBM1 := DllCall("SelectObject", "Uint", mDC1, "Uint",  hBM)
	oBM2 := DllCall("SelectObject", "Uint", mDC2, "Uint", zhBM)
	DllCall("SetStretchBltMode", "Uint", mDC2, "int", 4)
	DllCall("StretchBlt", "Uint", mDC2, "int", 0, "int", 0, "int", znW, "int", znH, "Uint", mDC1, "int", 0, "int", 0, "int", nW, "int", nH, "Uint", 0x00CC0020)
	DllCall("SelectObject", "Uint", mDC1, "Uint", oBM1)
	DllCall("SelectObject", "Uint", mDC2, "Uint", oBM2)
	DllCall("DeleteDC", "Uint", mDC1)
	DllCall("DeleteDC", "Uint", mDC2)
	DllCall("DeleteObject", "Uint", hBM)
	Return	zhBM
}

Convert(sFileFr = "", sFileTo = "", nQuality = "")
{
	If	sFileTo  =
		sFileTo := A_ScriptDir . "\screen.bmp"
	SplitPath, sFileTo, , sDirTo, sExtTo, sNameTo

	If Not	hGdiPlus := DllCall("LoadLibrary", "str", "gdiplus.dll")
		Return	sFileFr+0 ? SaveHBITMAPToFile(sFileFr, sDirTo . "\" . sNameTo . ".bmp") : ""
	VarSetCapacity(si, 16, 0), si := Chr(1)
	DllCall("gdiplus\GdiplusStartup", "UintP", pToken, "Uint", &si, "Uint", 0)

	If	!sFileFr
	{
		DllCall("OpenClipboard", "Uint", 0)
		If	 DllCall("IsClipboardFormatAvailable", "Uint", 2) && (hBM:=DllCall("GetClipboardData", "Uint", 2))
		DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", hBM, "Uint", 0, "UintP", pImage)
		DllCall("CloseClipboard")
	}
	Else If	sFileFr Is Integer
		DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "Uint", sFileFr, "Uint", 0, "UintP", pImage)
	Else	DllCall("gdiplus\GdipLoadImageFromFile", "Uint", Unicode4Ansi(wFileFr,sFileFr), "UintP", pImage)

	DllCall("gdiplus\GdipGetImageEncodersSize", "UintP", nCount, "UintP", nSize)
	VarSetCapacity(ci,nSize,0)
	DllCall("gdiplus\GdipGetImageEncoders", "Uint", nCount, "Uint", nSize, "Uint", &ci)
	Loop, %	nCount
		If	InStr(Ansi4Unicode(NumGet(ci,76*(A_Index-1)+44)), "." . sExtTo)
		{
			pCodec := &ci+76*(A_Index-1)
			Break
		}
	If	InStr(".JPG.JPEG.JPE.JFIF", "." . sExtTo) && nQuality<>"" && pImage && pCodec
	{
	DllCall("gdiplus\GdipGetEncoderParameterListSize", "Uint", pImage, "Uint", pCodec, "UintP", nSize)
	VarSetCapacity(pi,nSize,0)
	DllCall("gdiplus\GdipGetEncoderParameterList", "Uint", pImage, "Uint", pCodec, "Uint", nSize, "Uint", &pi)
	Loop, %	NumGet(pi)
		If	NumGet(pi,28*(A_Index-1)+20)=1 && NumGet(pi,28*(A_Index-1)+24)=6
		{
			pParam := &pi+28*(A_Index-1)
			NumPut(nQuality,NumGet(NumPut(4,NumPut(1,pParam+0)+20)))
			Break
		}
	}

	If	pImage
		pCodec	? DllCall("gdiplus\GdipSaveImageToFile", "Uint", pImage, "Uint", Unicode4Ansi(wFileTo,sFileTo), "Uint", pCodec, "Uint", pParam) : DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "Uint", pImage, "UintP", hBitmap, "Uint", 0) . SetClipboardData(hBitmap), DllCall("gdiplus\GdipDisposeImage", "Uint", pImage)

	DllCall("gdiplus\GdiplusShutdown" , "Uint", pToken)
	DllCall("FreeLibrary", "Uint", hGdiPlus)
}

CreateDIBSection(hDC, nW, nH, bpp = 32, ByRef pBits = "")
{
	NumPut(VarSetCapacity(bi, 40, 0), bi)
	NumPut(nW, bi, 4)
	NumPut(nH, bi, 8)
	NumPut(bpp, NumPut(1, bi, 12, "UShort"), 0, "Ushort")
	NumPut(0,  bi,16)
	Return	DllCall("gdi32\CreateDIBSection", "Uint", hDC, "Uint", &bi, "Uint", 0, "UintP", pBits, "Uint", 0, "Uint", 0)
}

SaveHBITMAPToFile(hBitmap, sFile)
{
	DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)
	hFile:=	DllCall("CreateFile", "Uint", &sFile, "Uint", 0x40000000, "Uint", 0, "Uint", 0, "Uint", 2, "Uint", 0, "Uint", 0)
	DllCall("WriteFile", "Uint", hFile, "int64P", 0x4D42|14+40+NumGet(oi,44)<<16, "Uint", 6, "UintP", 0, "Uint", 0)
	DllCall("WriteFile", "Uint", hFile, "int64P", 54<<32, "Uint", 8, "UintP", 0, "Uint", 0)
	DllCall("WriteFile", "Uint", hFile, "Uint", &oi+24, "Uint", 40, "UintP", 0, "Uint", 0)
	DllCall("WriteFile", "Uint", hFile, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44), "UintP", 0, "Uint", 0)
	DllCall("CloseHandle", "Uint", hFile)
}

SetClipboardData(hBitmap)
{
	DllCall("GetObject", "Uint", hBitmap, "int", VarSetCapacity(oi,84,0), "Uint", &oi)
	hDIB :=	DllCall("GlobalAlloc", "Uint", 2, "Uint", 40+NumGet(oi,44))
	pDIB :=	DllCall("GlobalLock", "Uint", hDIB)
	DllCall("RtlMoveMemory", "Uint", pDIB, "Uint", &oi+24, "Uint", 40)
	DllCall("RtlMoveMemory", "Uint", pDIB+40, "Uint", NumGet(oi,20), "Uint", NumGet(oi,44))
	DllCall("GlobalUnlock", "Uint", hDIB)
	DllCall("DeleteObject", "Uint", hBitmap)
	DllCall("OpenClipboard", "Uint", 0)
	DllCall("EmptyClipboard")
	DllCall("SetClipboardData", "Uint", 8, "Uint", hDIB)
	DllCall("CloseClipboard")
}

Unicode4Ansi(ByRef wString, sString)
{
	nSize := DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", 0, "int", 0)
	VarSetCapacity(wString, nSize * 2)
	DllCall("MultiByteToWideChar", "Uint", 0, "Uint", 0, "Uint", &sString, "int", -1, "Uint", &wString, "int", nSize)
	Return	&wString
}

Ansi4Unicode(pString)
{
	nSize := DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "Uint", 0, "int",  0, "Uint", 0, "Uint", 0)
	VarSetCapacity(sString, nSize)
	DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "Uint", pString, "int", -1, "str", sString, "int", nSize, "Uint", 0, "Uint", 0)
	Return	sString
}

;----------------------------------------------------------------------------------------------------------------------------------------------------
Calc_MD5(_VarAddress, _VarSize){

	Static Hex = "123456789ABCDEF0"

	Ptr := A_PtrSize ? "Ptr" : "Uint"

	, VarSetCapacity(MD5_CTX,104,0)

	, DllCall("advapi32\MD5Init",Ptr,&MD5_CTX)

	, DllCall("advapi32\MD5Update",Ptr,&MD5_CTX,Ptr,_VarAddress,"UInt",_VarSize)

	, DllCall("advapi32\MD5Final",Ptr,&MD5_CTX)
	
	Loop,16

		N := NumGet( MD5_CTX,87+A_Index,"Char"), MD5 .= SubStr(Hex,N>>4,1) . SubStr(Hex,N&15,1)
	
	Return MD5
}
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

20 Oct 2013, 18:06

thanks, i was thinking along the same lines (using captured images to keep track of changes) but i could not come up with a working script. i'll try your code and report back. :D
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: how to detect web connection is lost?

22 Oct 2013, 02:29

I'm sorry but I'm a newbie with ahk.
I don't understand this script

Code: Select all


SetTimer,CheckInternet,100
return

CheckInternet:
msgbox hello
Connected := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0)
if Connected = 0
msgbox You have lost internet connection
else
msgbox You are connected
return

When I run this script it does not do anything. I thought that CheckInternet would run every 100 milliseconds but I see the message "hello" even once.
Where is my wrong?
Everything is possible!
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: how to detect web connection is lost?

22 Oct 2013, 02:39

I've just read that I must use #Persistent,

Code: Select all

#Persistent
SetTimer,CheckInternet,100
return

CheckInternet:
msgbox hello
Connected := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0)
if Connected = 0
msgbox You have lost internet connection
else
msgbox You are connected
return
Now, It works fine but I have a question, if you do not use #Persistent is not running any time the timer?
Everything is possible!
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

22 Oct 2013, 04:36

thansk! so #Persistent is not optional.
empardopo wrote:I've just read that I must use #Persistent,

Code: Select all

#Persistent
SetTimer,CheckInternet,100
return

CheckInternet:
msgbox hello
Connected := DllCall("Wininet.dll\InternetGetConnectedState", "Str", 0x40,"Int",0)
if Connected = 0
msgbox You have lost internet connection
else
msgbox You are connected
return
Now, It works fine but I have a question, if you do not use #Persistent is not running any time the timer?
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

22 Oct 2013, 23:22

empardopo wrote:Now, It works fine but I have a question, if you do not use #Persistent is not running any time the timer?
Without #Persistent this script will exit when it reaches the first RETURN never giving the SetTimer a chance to run.

This is because this script does not have any hotkeys, hotstrings, OnMessage() or gui's. It was intended to be part of a bigger script.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

23 Oct 2013, 07:22

In v1.0.16+, this directive (#Persistent) also makes a script single-instance. To override this or change the way single-instance behaves, see #SingleInstance. does this mean when i include #Persistent i should not include #SingleInstance? what happens if i include both?
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

23 Oct 2013, 09:06

Using #Persistent without #SingleInstance would behave as if you used #SingleInstance without any parameters, (Which is to prompt you try to run the script again when it is already running)
Spoiler
Use #Persistent and #SingleInstance with a parameter (force|ignore|off) to over ride the default behavior of running #Persistent by itself.
This parameter determines what happens when a script is launched while a previous instance of itself is already running:

The word FORCE skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.

The word IGNORE skips the dialog box and leaves the old instance running. In other words, attempts to launch an already-running script are ignored.

The word OFF allows multiple instances of the script to run concurrently.

If the parameter is omitted, a dialog box is displayed asking whether to keep the old instance or replace it with the new one.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
joedf
Posts: 7646
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: how to detect web connection is lost?

23 Oct 2013, 19:34

that doesnt work for me, but the following work very well:
Dllcall method is the fastest, and is VERY fast.

Code: Select all

QPC test results (QPC/QPX by LearningOne)
ping_exe
   0.123704 515433%
ping_com
   0.084498 352075%
ping_dll <<
   0.000024  100%
Get QPC/QPX here: http://www.autohotkey.com/board/topic/9 ... code-runs/

Using COM

Code: Select all

ping(host="8.8.8.8") {
	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" )
}
Using Ping.exe

Code: Select all

ping(host="8.8.8.8",timeout="500")
{
Runwait, %comspec% /C ping %host% -n 1 -w %timeout% >"%A_temp%\ping.txt",,Hide
fileread, out, %A_temp%\ping.txt
filedelete, %A_temp%\ping.txt
	ifnotinstring,out,%A_Space%0`%)
	{
		return "Offline"
	}
	else
	{
		return "Online"
	}
}
Using Dllcall (by uberi, i added 8.8.8.8 as default, changed timeout to 1000, more reliable)

Code: Select all

Ping(Address="8.8.8.8",Timeout = 1000,ByRef Data = "",Length = 0,ByRef Result = "",ByRef ResultLength = 0)
{
    NumericAddress := DllCall("ws2_32\inet_addr","AStr",Address,"UInt")
    If NumericAddress = 0xFFFFFFFF ;INADDR_NONE
        throw Exception("Could not convert IP address string to numeric format.")

    If DllCall("LoadLibrary","Str","icmp","UPtr") = 0 ;NULL
        throw Exception("Could not load ICMP library.")

    hPort := DllCall("icmp\IcmpCreateFile","UPtr") ;open port
    If hPort = -1 ;INVALID_HANDLE_VALUE
        throw Exception("Could not open port.")

    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
        throw Exception("Could not send echo.")
    If !DllCall("icmp\IcmpCloseHandle","UInt",hPort) ;close port
        throw Exception("Could not close port.")

    ResultLength := NumGet(Reply,12,"UShort")
    VarSetCapacity(Result,ResultLength)
    DllCall("RtlMoveMemory","UPtr",&Result,"UPtr",NumGet(Reply,16),"UPtr",ResultLength)
    Return, NumGet(Reply,8,"UInt")
}
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

23 Oct 2013, 20:55

so this is for an Internet connection in general but not necessarily for a flash video streaming site such as http://www.booktv.org/Live/C-SPAN2.aspx?
User avatar
joedf
Posts: 7646
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: how to detect web connection is lost?

23 Oct 2013, 22:29

yup! but to be "specific" you can use the COM version... ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: how to detect web connection is lost?

23 Oct 2013, 23:56

which means IE only (no Firefox), i presume?
User avatar
TLM
Posts: 1582
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: how to detect web connection is lost?

23 Oct 2013, 23:59

Code: Select all

Loop {
    For Item in (ComObjGet("winmgmts:").ExecQuery("Select * from Win32_NetworkAdapter"),l:="")
        l.=Item.NetEnabled
    If !InStr(l,-1)
        TrayTip,, Network Disconnected!
}

ESC::ExitApp	; <-- Press escape to exit.
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

24 Oct 2013, 10:52

TLM wrote:

Code: Select all

Loop {
    For Item in (ComObjGet("winmgmts:").ExecQuery("Select * from Win32_NetworkAdapter"),l:="")
        l.=Item.NetEnabled
    If !InStr(l,-1)
        TrayTip,, Network Disconnected!
}

ESC::ExitApp	; <-- Press escape to exit.

This does not work if the network adapter is still connected to your router and the internet goes down.

All of joedf's solutions actually ping an internet ip address, in this case google, so it would be much more reliable.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
joedf
Posts: 7646
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada
Contact:

Re: how to detect web connection is lost?

24 Oct 2013, 11:41

@datalife ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x8GB G.Skill RipJaws V - DDR4 3280 MHz | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
DataLife
Posts: 358
Joined: 29 Sep 2013, 19:52

Re: how to detect web connection is lost?

05 Nov 2013, 13:24

joedf wrote:

Code: Select all

Ping(Address="8.8.8.8",Timeout = 1000,ByRef Data = "",Length = 0,ByRef Result = "",ByRef ResultLength = 0)
{
    NumericAddress := DllCall("ws2_32\inet_addr","AStr",Address,"UInt")
    If NumericAddress = 0xFFFFFFFF ;INADDR_NONE
        throw Exception("Could not convert IP address string to numeric format.")

    If DllCall("LoadLibrary","Str","icmp","UPtr") = 0 ;NULL
        throw Exception("Could not load ICMP library.")

    hPort := DllCall("icmp\IcmpCreateFile","UPtr") ;open port
    If hPort = -1 ;INVALID_HANDLE_VALUE
        throw Exception("Could not open port.")

    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
        throw Exception("Could not send echo.")
    If !DllCall("icmp\IcmpCloseHandle","UInt",hPort) ;close port
        throw Exception("Could not close port.")

    ResultLength := NumGet(Reply,12,"UShort")
    VarSetCapacity(Result,ResultLength)
    DllCall("RtlMoveMemory","UPtr",&Result,"UPtr",NumGet(Reply,16),"UPtr",ResultLength)
    Return, NumGet(Reply,8,"UInt")
}
I would like to not display these error messages. Can I safely remove them from the function?
Attachments
ThrowException.jpg
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Bing [Bot], boiler, loek6000 and 229 guests