Is there anyway with Windows api to detect whether a Windows 7 computer's network connection is an internet connection or a LAN connection without internet access? Windows 7 and Vista indicate this in the network icon (Local only v. Local and Internet), is there anyway to grab that information progammatically?
5 Answers
http://msdn.microsoft.com/en-us/library/aa370750(VS.85).aspx describes the difference between IsConnected
and IsConnectedToInternet
. Windows 7 only. I don't have a native code sample handy, sorry.

- 18,808
- 8
- 56
- 85
-
Thanks, this is exactly the type of thing I am looking for. – MattR Apr 22 '10 at 13:50
Pinging 4.2.2.2
is a common means of determining if internet connectivity is present.
The story behind this lies here.

- 182,639
- 35
- 285
- 343
-
-
2Although now 8.8.8.8 is probably better (behold the might of google) – Martin Beckett Apr 20 '10 at 18:55
-
@Martin: Yes, though 4.2.2.2 is now muscle memory for me. I don't know if I can bear to learn 8.8.8.8 and its uncomfortable angle between index- and middle finger. – Adam Robinson Apr 20 '10 at 18:56
Not perfectly.
You could detect if you are on a lan from the IP address (assuming your local DHCP handed out one of the 192.168.xx.xx private addresses.)
YOu could ping a known external site to check if that lan has an internet connection.

- 94,801
- 28
- 188
- 263
-
Just because the computer has a private IP address (which also include 10.x.x.x and 172.16-31.x.x) doesn't mean it's not connected to the Internet. As anyone accessing this site from behind a home router performing NAT can attest! – Stephen C. Steel Apr 20 '10 at 19:20
-
It wasn't clear what the OP was asking ie. direct internet connection vs NAT – Martin Beckett Apr 20 '10 at 21:17
Windows 7 performs DNS query for dns.msftncsi.com 5 seconds after link establishment. Do not see any reason to do that except for checking internet connection.

- 197
- 1
- 8
The Windows API Code Pack will do this for you http://archive.msdn.microsoft.com/WindowsAPICodePack
its NetworkListManager Class has an IsConnected property that indicates if you have LAN connection
and an IsConnectedToInternet property that indicates if you have Internet connection
the pack download contains a Network Demo application that demonstrates this

- 1,764
- 5
- 21
- 33
-
the question is tagged C++ meaning native code. The Code Pack (which I love and am partially responsible for) is managed code eg C# – Kate Gregory Feb 20 '13 at 12:47