Detect When Network Cable Unplugged

前端 未结 2 1559
面向向阳花
面向向阳花 2020-12-03 03:30

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

相关标签:
2条回答
  • 2020-12-03 03:42

    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.

    0 讨论(0)
  • 2020-12-03 03:50

    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.

    0 讨论(0)
提交回复
热议问题