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
SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

How to find Internet Connection Status ?
http://www.autohotke...p?p=60892#60892

Function ConnectedToInternet( Flag )

ConnectedToInternet() should work with all Windows OS.

0x40 INTERNET_CONNECTION_CONFIGURED - Local system has a valid connection to the Internet, but not be currently connected.
0x02 INTERNET_CONNECTION_LAN - Local system uses a local area network to connect to the Internet.
0x01 INTERNET_CONNECTION_MODEM - Local system uses a modem to connect to the Internet.
0x08 INTERNET_CONNECTION_MODEM_BUSY - No longer used.
0x20 INTERNET_CONNECTION_OFFLINE - Local system is in offline mode.
0x04 INTERNET_CONNECTION_PROXY - Local system uses a proxy server to connect to the Internet

MSDN Reference: List of WinINet Functions

The Function:
ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}
Copy-Paste-Try Example:
If ConnectedToInternet() 
   Msgbox, 64, WinInet.dll, ONLINE! 
else 
   Msgbox, 48, WinInet.dll, OFFLINE! 
Return 

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Credits:
Thanks to Jon for his Post @ How to deal with dllcall returning boolean TRUE/FALSE? ( jordi )
Thanks to Chris for his Post @ the same topic suggesting corrections.


Edit : 07-Oct-2006

Here is an another method using an URL to ascertain Internet Connected Status :


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) 
}

Reference: Request by meter @ Help with Wininet.dll\InternetCheckConnection


kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012
I found an error in your ConnectedToInternet() function. You used:

ConnectedToInternet(flag="") {
If flag=""


The if statement is not an expression so the default value of 0x40 is never set (unless two literal quotation marks were passed). Using ConnectedToInternet(flag = 0x40) would be better.

However I find that calling the function with any of the listed values doesn't work :?

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

I found an error in your ConnectedToInternet() function. You used:

ConnectedToInternet(flag="") {
If flag=""


The if statement is not an expression so the default value of 0x40 is never set (unless two literal quotation marks were passed). Using ConnectedToInternet(flag = 0x40) would be better.


:shock: :shock:
Thanks for pointing it.

However I find that calling the function with any of the listed values doesn't work


Have updated ConnectedToInternet(). Please try again and let me know. It works now properly with my Single user PC with ADSL connection.

Regards, :)

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

Have updated ConnectedToInternet(). Please try again and let me know. It works now properly with my Single user PC with ASDL connection.

No it doesn't work for me :?
This version seems to work:
MsgBox % Connected()

Connected(fl = 0x40) {
	Return, DllCall("Wininet.dll\InternetGetConnectedState", "UInt *", fl, "UInt", 0)
}
However the flags seem to be ignored which is quite strange.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Titan, :)

This version seems to work:

MsgBox % Connected()

Connected(fl = 0x40) {
	Return, DllCall("Wininet.dll\InternetGetConnectedState", "UInt *", fl, "UInt", 0)
}
However the flags seem to be ignored which is quite strange.


:roll: Yes! This version works for me.. I am too confused! :roll:

Regards, :)

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

The following post has been updated:
[*:1933jp11]How to find Internet Connection Status ? posted 22-May-2006 updated 07-Aug-2006.

Added example: Wininet.dll\InternetCheckConnection

Regards, :)


kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

ConnectedToInternet([color=red]flag=""[/color]) { 
[color=red]If flag=
   flag=0x40[/color]
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}


Since 0x40 is a literal integer you can simplify it to:
ConnectedToInternet([color=green]flag = 0x40[/color]) {
	Return, DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag, "Int", 0)
}


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Since 0x40 is a literal integer you can simplify it


Thanks! :D .. Done!
kWo4Lk1.png

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
I would greatly appreciate help on these two functions..

The first one gives the same result on using any of the given flags.

The second one makes my program unresponsive till the function returns, but otherwise works well.

Could you tell me how to ?? isolate that thread (or do something better ;-) )

Thanks for your response.





How to find Internet Connection Status ?
http://www.autohotke...p?p=60892#60892

Function ConnectedToInternet( Flag )

ConnectedToInternet() should work with all Windows OS.

0x40 INTERNET_CONNECTION_CONFIGURED - Local system has a valid connection to the Internet, but not be currently connected.
0x02 INTERNET_CONNECTION_LAN - Local system uses a local area network to connect to the Internet.
0x01 INTERNET_CONNECTION_MODEM - Local system uses a modem to connect to the Internet.
0x08 INTERNET_CONNECTION_MODEM_BUSY - No longer used.
0x20 INTERNET_CONNECTION_OFFLINE - Local system is in offline mode.
0x04 INTERNET_CONNECTION_PROXY - Local system uses a proxy server to connect to the Internet

MSDN Reference: List of WinINet Functions

The Function:
ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}
Copy-Paste-Try Example:
If ConnectedToInternet() 
   Msgbox, 64, WinInet.dll, ONLINE! 
else 
   Msgbox, 48, WinInet.dll, OFFLINE! 
Return 

ConnectedToInternet(flag=0x40) { 
Return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag,"Int",0) 
}

Credits:
Thanks to Jon for his Post @ How to deal with dllcall returning boolean TRUE/FALSE? ( jordi )
Thanks to Chris for his Post @ the same topic suggesting corrections.


Edit : 07-Oct-2006

Here is an another method using an URL to ascertain Internet Connected Status :


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) 
}

Reference: Request by meter @ Help with Wininet.dll\InternetCheckConnection



SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
There are two more options for you to try: IsDestinationReachable() and Ping :)

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006

There are two more options for you to try: IsDestinationReachable() and Ping :)


Thanks SKAN very much!!
But the first one has the same problem i stated ( it makes my script unresponsive for the time of the call), please try to right click the taskbar icon while the call is in progress (upto 19 secs sometimes)

The second option just gives me unreachable inspite of being online! Am i doing something wrong (i even changed the URL to google.com)

Thanks again!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

The second option just gives me unreachable inspite of being online! Am i doing something wrong (i even changed the URL to google.com)


url=www.google.com
RunWait, ping.exe %url% -n 1,, Hide UseErrorlevel
If Errorlevel
     MsgBox,16,%url%, Destination Unreachable
Else MsgBox,64,%url%, Destination Reachable

For me, the above has been the most reliable and works for me in all OS.
Are you sure your firewall is not blocking ping.exe ? BTW, What is your OS ?

:)

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Sorry SKAN, now it works..
i checked it on the command line, the problem is the response is slow, so it times out before the response and returns an Errorlevel..
It is totally cause i am using BSNL EVDO!! It is supposed to be wireless broadband, but it is slower than dial-up :oops:

I have a broadband at my home, so will test it when i get back..

Thanks again!
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

Guest
  • Guests
  • Last active:
  • Joined: --
What this function really does is checking if the internet-cable pluged IN or pluged OFF.
No matter which from param is in flag.

msdn.microsoft.com says:
A return value of TRUE from InternetGetConnectedState indicates that at least one connection to the Internet is available.

For me it indicates nothing. And other variants needs few seconds to response, it's too long.
So what stays?

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

i tried that code and 

Spoiler