[C #] using InternetGetConnectedState API to detect the current computer network connection status

[C #] using InternetGetConnectedState API to detect the current computer network connection status


Sometimes we will need for the program added ability to detect network connection, perhaps when the network is disconnected show some tips, or the function of some programs to the disabled. Then we may tend to adopt NetworkChange.NetworkAddressChanged and NetworkChange.NetworkAvailabilityChanged these two events to detect the network environment is subject to change or add a Timer Ping ways to detect if possible even outside the network. The above two methods is fairly common method in .NET full program, but the author's experience, NetworkChange run in a multi-card environment with desired may differ, but even if normal triggering event may be with the other methods to determine whether they are on the network, as Ping detection methods, it needs to send the actual packet inspection, not only need to consider may be blocked, but also consider the additional burden of packet transmission, ask if they can connect directly with the system the state would certainly be a good thing. So use InternetGetConnectedState API may also be a good choice in doing a similar function here readily to the record a little bit.

To use InternetGetConnectedState API to detect the current computer network connection status, first of all we have to look at the function prototype of the API.

  __out  LPDWORD lpdwFlags,
  __in   DWORD dwReserved
);

From the function prototype, we can see that the API requires two parameters, one is passed back and forth into the current network connection type, the return value can be found in the following table:

image

Another parameter is the retention of API directly into 0 on it. As for the return value of the API is representative of whether the computer network is the connection status.

After the function prototypes with the usage is clear, we can start the actual use of API, API first to join the program PInvoke declaration:

		public static extern bool InternetGetConnectedState(
			ref uint lpdwFlags,
			uint dwReserved
			);

When you use it to declare a return to the parameter, then the parameter 0 of the statement into the API can be.

uint flags = 0x0;
var isNetworkAvailable = InternetGetConnectedState(ref flags, 0);
...

Finally, here accompanied by a more complete usage examples:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication21
{
	class Program
	{
		[DllImport("wininet")]
		public static extern bool InternetGetConnectedState(
			ref uint lpdwFlags,
			uint dwReserved
			);

		static void Main(string[] args)
		{
			uint flags = 0x0;

			var isNetworkAvailable = InternetGetConnectedState(ref flags, 0);

			Console.WriteLine(string.Format("Network available: {0} ({1})", isNetworkAvailable.ToString(), flags.ToString()));
		}
	}
}

After running you can see the following screen, the computer represents the author's currently have a network connection, and the connection status of the network is INTERNET_RAS_INSTALLED & INTERNET_CONNECTION_LAN.

image

Link

  • InternetGetConnectedState function
  • InternetGetConnectedState | M Dingle 's shelling play

Original: Large column  [C #] using InternetGetConnectedState API to detect the current computer network connection status


Guess you like

Origin www.cnblogs.com/petewell/p/11496008.html