views:

290

answers:

3

Windows knows when you have removed the network cable from your NIC. Is there a programmatic way to determine this via API in C++?

Note: I am not using .NET and this is for a closed LAN (not connected to Internet ever)

A: 

I am sorry about the misassumptions but the code you want is probably in here, just in C.

http://msdn.microsoft.com/en-us/library/aa366062%28VS.85%29.aspx

aiwarrior
I provided the platform, see tag for Windows and your Windows link is for CE
0A0D
Current link is OK.
MSalters
+1  A: 

Use the Network List Manager api with the INetwork api.

You can register your app to receive callbacks when networks become connected/not connected.

Or you can get the list of networks and then check each of them to see if the machine is network connected or not

Remember that a windows machine will often have multiple networks set up (Ethernet, wifi, etc)

Remember that just because a network is connected, you may not have access to the internet. Eg you could have DNS or routing problems. Or you could be on a working network that is not connected to the internet.

Due to the above, many diagnostic tools also connect to a "known-good" internet server if they want to really know if they're connected or not. Something like google.com -- they spend a lot of money to make sure that their site is up all the time.

Finally, you can also have a semi-connected situation where packets are getting through but not enough to really enable communications to flow. To test this, don't just ping an internet site since that'd only be a pair of packets. Instead, open a TCP connection or something more than a ping.

Larry K
Not sure why everyone thinks I am talking about the Internet :) I guess I should have been more clear, this is more about a closed LAN scenario
0A0D
INetwork http://msdn.microsoft.com/en-us/library/aa370750(VS.85).aspx lets you ask the separate questions "am I on a network" and "am I on the Internet". This answer gives you what you need.
Kate Gregory
Anything for XP? This says Vista and 2008 only
0A0D
+1  A: 

Raymond Chen blogged about something similar recently. Here is the relevant documentation. I think the MIB_IPADDR_DISCONNECTED flag is what you are looking for.

Luke