Detect When Network Cable Unplugged

2019-01-11 13:41发布

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)

2条回答
beautiful°
2楼-- · 2019-01-11 13:54

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.

查看更多
甜甜的少女心
3楼-- · 2019-01-11 13:58

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.

查看更多
登录 后发表回答