Check for Internet Connection (Part 2 of 3)

Category: .NET Framework

Check for an Internet Connection – Part Two of Three

This is Part Two of this article series. Read Part 1 here


What we learned in the first article of this series was that the best applications have the best validation and the least occurrences of errors.  We learned that we are able to import external libraries with ease and that we can utilize these libraries for various methods.  The method we are dealing with in this example is the InternetGetConnectedState.

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

[DllImport("wininet.dll", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(ref StateOfConnection lpdwFlags, int dwReserved);

In this article we will explain the definitions of the various connection states and start to build our application.

Definition of Connection States Fig. 1

  • Configured:
  • Local system has a valid connection to the Internet, but it might or might not be currently connected.
  • Lan:
  • Local system uses a local area network to connect to the Internet.
  • Modem:
  • Local system uses a modem to connect to the Internet.
  • Modem Busy:
  • No longer used.
  • Offline:
  • Local system is in offline mode.
  • Proxy:
  • Local system uses a proxy server to connect to the Internet.
  • Installed:
  • Local system has RAS installed.

In this next example we are creating an instantiation of the “StateOfConnection “class, we will call the instance “MyState” and set the default value to 0.

StateOfConnection MyState = 0;

This is where we actually call the external library and pass the two ref parameters.  The external library returns a bool and that return value will be displayed in the txtFlag text box so you can visually see the results of the call.

txtFlag.Text = InternetGetConnectedState(ref MyState, 0).ToString();

Now that we have returned from the external library call we have an instantiated enumeration of the ConnectionState that we can access and retrieve its returned value.  In this next example we are getting the value of the ConnectionState and displaying it in the txtState text box so you can visually see what was returned.

txtState.Text = MyState.ToString();

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Next Up

We are well on our way to completing the application and testing our code to see if we can test for an Internet connection.  In the next article we will show you the entire set of source code and how everything we have discussed actually works.

What have we learned?

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

  • Definition of Connection States
  • How we call and utilize the external library

Continue reading Part Three here


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!

100% SPAM FREE! We will never sell or rent your email address!