I've a script which checks if my pc is connected to the internet or not. I want it to trigger a subroutine in another script when there's no connection. Something like this:
Code: Select all
#SingleInstance, Force
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") Broken Link for safety
}
else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
{
http.Open("GET", "http ipv6.msftncsi.com /ncsi.txt") Broken Link for safety
}
else
{
return false
}
http.Send()
return (http.ResponseText = "Microsoft NCSI") ;ncsi.txt will contain exactly this text
}
a = 0 ; Toggle Connection ON
b = 0 ; Toggle Connection OFF
loop{
Sleep 300
if (IsInternetConnected() = 1 && a = 0)
{
GoSub, Subroutine in script2
a++ ;
b = 0 ;
}
else if (IsInternetConnected() = 0 && b = 0)
{
GoSub, Subroutine in script2
b++ ;
a = 0 ;
}
}
