5

I am in need of a piece of code that can detect if a network connection is connected or disconnected. The connected state would mean a cable was plugged into the Ethernet connection. A disconnected state would mean there is not cable connected.

I can't use the WMI interface due to the fact that I'm running on Windows CE. I don't mind invoking the Win32 API but remember that I'm using Windows CE and running on the Compact Framework.

Bobby Cannon
  • 6,665
  • 8
  • 33
  • 46
  • One thing to remember is that a network connection is not equal to an internet connection. It sounds like you already get that because you define connected as meaning "a cable is plugged into the Ethernet connection", but it bears repeating. – Joel Coehoorn Feb 18 '14 at 22:25

4 Answers4

4

The easiest way is to use OpenNETCF's SDF and look at the OpenNETCF.Net.NetworkInformation.NetworkInterfaceWatcher class, which will raise events when NDIS sends out notifications (like MEDIA_CONNECT and MEDIA_DISCONNECT).

You can do the same work without the SDF, of course. It involves onening the NDIS driver directly and calling IOCTL_NDISUIO_REQUEST_NOTIFICATION with a P2P message queue handle. It's not overly difficult, but there's a lot you have to get right for it to work and not leak.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • Hi, I downloaded 2.3 binaries from https://opennetcf.codeplex.com/releases/view/137461 but I can't find the AdapterStatusMonitor in OpenNETCF.Net Namespace... Am I missing something? Thanks! – franDayz Feb 05 '16 at 11:39
  • 1
    Take a look at the `NetworkInterfaceWatcher` http://opennetcf.codeplex.com/SourceControl/latest#OpenNETCF.Net/OpenNETCF.Net/NetworkInformation/NetworkInterfaceWatcher.cs – ctacke Feb 05 '16 at 22:18
2

Check out this MSDN article:

Testing for and Responding to Network Connections in the .NET Compact Framework

TWA
  • 12,756
  • 13
  • 56
  • 92
  • The "solution" the link points to will not detect whether a cable is present of not - it detects whether a host is reachable. As Joel points out, those are two totally different things. – ctacke Jun 02 '09 at 00:47
1

Call GetAdaptersInfo and loop through the list of available network adapters until you find the one you're looking for?

Tom van Enckevort
  • 4,170
  • 1
  • 25
  • 40
0

One thing to remember is that a network connection is not equal to an internet connection.

It sounds like you already get that because you define connected as meaning "a cable is plugged into the Ethernet connection", but it bears repeating.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794