Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[How To] Find Internet Connection Status


  • Please log in to reply
25 replies to this topic
garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

modified = 20150830

;- http://www.autohotkey.com/board/topic/111086-unsolved-check-if-connected-to-the-internet/
;- user = Linear Spoon
modified=20150830
aaa:= % IsInternetConnected()
if aaa=0
  msgbox, 262208,InternetCheck,Internet NOT connected
else
  msgbox, 262208,InternetCheck,Internet IS connected
return
;-------

;- Should be compatible with Win XP or higher, 32/64 bit, Unicode or ANSI, latest version.
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")

xxx := ComObjCreate("WinHttp.WinHttpRequest.5.1")
F1:="http://www.msftncsi.com/ncsi.txt"
F2:="http://ipv6.msftncsi.com/ncsi.txt"
xxx.SetTimeouts(500,500,500,500)
try {
  if (ai_family = 2 && wsaData = "131.107.255.255:80")
    xxx.Open("GET",F1)
  else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
    xxx.Open("GET",F2)
  else
    return false
  xxx.Send()
  return (xxx.ResponseText = "Microsoft NCSI")           ;-ncsi.txt will contain exactly this text otherwise=0
} catch e {
    xxxe:=e.Message
    msgbox, 262208,ERROR ,Error=Catch`n%f1%`n - or-`n%f2%`n------------------------------------------`n%xxxe%`n------------------------------------------`nit TRYS again ...,
    }
return
}
;-------------------------------------------------------------------------------------------------------




Kim Jong Un
  • Members
  • 16 posts
  • Last active: Jul 05 2013 03:27 AM
  • Joined: 08 May 2013

I tried this code and it works well..

but can i have some explanation for this?

 

#persistent     ;what is #persistent?
gosub,aa     ;and this gossub too?
settimer
,aa,10000
return

aa:     ;This following lines too
cti:=ConnectedToInternet()
if cti=1
   ctj=OK
else
   ctj=Not OK
e
=0
For objStatus in ComObjGet("winmgmts:").ExecQuery("Select * From Win32_PingStatus where Address = 'www.google.com'")

 

 

 

 

And also thnks, I understand some codes but im still a newbie here.  I can copy-paste the and edit some of the lines but it is still better if i can understand them so i can make my own script someday and help some people too.. grin.png cool.png



Alibaba
  • Members
  • 435 posts
  • Last active: Nov 19 2014 04:21 PM
  • Joined: 01 Nov 2012

Kim, if you would use the code tags (this symbol: < >), it would highlight the code automatically:

#persistent     ;what is #persistent?
gosub,aa     ;and this gossub too?
settimer,aa,10000
return

aa:     ;This following lines too
cti:=ConnectedToInternet()
if cti=1
   ctj=OK
else
   ctj=Not OK
e=0
For objStatus in ComObjGet("winmgmts:").ExecQuery("Select * From Win32_PingStatus where Address = 'www.google.com'")

안녕히가세요


Find the recent autohotkey version here: ahkscript.org


Kim Jong Un
  • Members
  • 16 posts
  • Last active: Jul 05 2013 03:27 AM
  • Joined: 08 May 2013

I got it working!

 

 
ConnectedToInternet(flag=0x40)
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0)
}
If (ConnectedToInternet()){
 
Run, www.wartune.com, ,max
WinWaitActive, Chrome_WidgetWin_1,,3  ; im too lazy to give them colors 1 by 1 , is there an easyway?
Send, {Tab} 8709b78f80f {TAB} 123456
 
}else{
Shutdown,1
}

 

 

 

this is what exactly what i want to achieve, to log my char. in the game..

is there another way i can check my game website if it is on? bcs sometimes my game's site is dead. pls make it simple for my peble mind cant handle complicated codes at the moment..  replys are very appreciated :)



Kim Jong Un
  • Members
  • 16 posts
  • Last active: Jul 05 2013 03:27 AM
  • Joined: 08 May 2013

lol right in time thnks admin sir salute!



garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

just for first question

;-- help file , save as scriptname=Autohotkey_help.ahk
run,%a_programfiles%\Autohotkey\autohotkey.chm
return

/*
see autohotkey help for commands
#persistent
This is useful in cases where a script contains timers and/or custom menu items but not hotkeys, hotstrings, or any use of OnMessage() or Gui.

*/



Kim Jong Un
  • Members
  • 16 posts
  • Last active: Jul 05 2013 03:27 AM
  • Joined: 08 May 2013

Thnks for that Gary. help me a lot, ill save this thread to make a reffrence in the future wink.png

 

 

 

 

 

 

 

 

이 사이트는 내 핵 공격에서 면제 될 것입니다



garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

이 사이트는 내 핵 공격에서 면제 될 것입니다

 

thanx, good news ....



LazyMan
  • Members
  • 587 posts
  • Last active: Oct 21 2014 05:13 PM
  • Joined: 17 Feb 2011
SKAN,

Many times when I'm searching this forum for an answer, I find it in your posts.

THANKS for this one! B)

Amein
  • Members
  • 13 posts
  • Last active: Nov 06 2015 04:03 PM
  • Joined: 26 Aug 2015

Hi, code below still applicable? I tried, even connect to internet but msg box show no internet :/

URL := "http://www.autohotkey.com"

If InternetCheckConnection(URL) 
   Msgbox, 64, WinInet.dll  [%URL%], Connection Success!
else 
   Msgbox, 48, WinInet.dll  [%URL%], Connection Failed!

Return

; The Function

InternetCheckConnection(Url="",FIFC=1) { 
Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0) 
}


garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005

this works for me  ,

;- http://www.autohotkey.com/board/topic/111086-unsolved-check-if-connected-to-the-internet/
;- user = Linear Spoon
modified=20150830
aaa:= % IsInternetConnected()
if aaa=0
  msgbox, 262208,InternetCheck,Internet NOT connected
else
  msgbox, 262208,InternetCheck,Internet IS connected
return
;-------

;- Should be compatible with Win XP or higher, 32/64 bit, Unicode or ANSI, latest version.
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")

xxx := ComObjCreate("WinHttp.WinHttpRequest.5.1")
F1:="http://www.msftncsi.com/ncsi.txt"
F2:="http://ipv6.msftncsi.com/ncsi.txt"
xxx.SetTimeouts(500,500,500,500)
try {
  if (ai_family = 2 && wsaData = "131.107.255.255:80")
    xxx.Open("GET",F1)
  else if (ai_family = 23 && wsaData = "[fd3e:4f5a:5b81::1]:80")
    xxx.Open("GET",F2)
  else
    return false
  xxx.Send()
  return (xxx.ResponseText = "Microsoft NCSI")           ;-ncsi.txt will contain exactly this text otherwise=0
} catch e {
    xxxe:=e.Message
    msgbox, 262208,ERROR ,Error=Catch`n%f1%`n - or-`n%f2%`n------------------------------------------`n%xxxe%`n------------------------------------------`nit TRYS again ...,
    }
return
}
;-------------------------------------------------------------------------------------------------------