It checks if the computer has access to the internet.
It works really fast (around 200 ms), but I occasionally get errors.
My script checks the status of the internet probably every 5 seconds and stays in the loop until the internet comes back online. It may take hours to get the error.
I have no idea what these errors mean, I have no idea how this function works.
If I can fix the function to not encounter errors that would be great. When I click OK, the script continues, so I would be okay with suppressing the errors. My script can handle if the internet is offline, but visually it is ugly. I am opening the Save as dialog and saving webpages and parsing the mhtml. So, if the mhtml is not what my script is expecting it will try again. So, the continuous closing and opening the webpage and opening and closing the Save As dialog is the ugly part.
I posted the function and screenshots below.
Maybe, I am calling the function to quickly in the loop. After 20 attempts, I display the tooltip and wait 1 second and try again. Maybe I can add a sleep outside of the "if" statement. I will try that but it may be hours before I know if that works.
I am looking for suggestions on how to fix the errors or suppress the errors.
Code: Select all
;Should be compatible with Win XP or higher, 32/64 bit, Unicode or ANSI, latest version.
;code here
;gosub CheckInternet ;(probably about 5 seconds elapses before calling the gosub again)
;more code here
return
CheckInternet:
Loop
{
Value := IsInternetConnected() ;returns 0 or 1 (1 is connected)
if value = 1
break
if a_index > 20
{
ToolTip, Waiting on internet to stablize
sleep 1000
}
}
return
IsInternetConnected()
{
static sz := A_IsUnicode ? 408 : 204, addrToStr := "Ws2_32\WSAAddressToString" (A_IsUnicode ? "W" : "A")
VarSetCapacity(wsaData, 408)
if DllCall("Ws2_32\WSAStartup", "UShort", 0x0202, "Ptr", &wsaData)
return false
if DllCall("Ws2_32\GetAddrInfoW", "wstr", "dns.msftncsi.com", "wstr", "http", "ptr", 0, "ptr*", results)
{
DllCall("Ws2_32\WSACleanup")
return false
}
ai_family := NumGet(results+4, 0, "int") ;address family (ipv4 or ipv6)
ai_addr := Numget(results+16, 2*A_PtrSize, "ptr") ;binary ip address
ai_addrlen := Numget(results+16, 0, "ptr") ;length of ip
DllCall(addrToStr, "ptr", ai_addr, "uint", ai_addrlen, "ptr", 0, "str", wsaData, "uint*", 204)
DllCall("Ws2_32\FreeAddrInfoW", "ptr", results)
DllCall("Ws2_32\WSACleanup")
http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
if (ai_family = 2 && wsaData = "131.107.255.255:80")
{
http.Open("GET", "http://www.msftncsi.com/ncsi.txt")
}
else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
{
http.Open("GET", "http://ipv6.msftncsi.com/ncsi.txt")
}
else
{
return false
}
http.Send()
return (http.ResponseText = "Microsoft NCSI") ;ncsi.txt will contain exactly this text
}