The best applications have the best validation and the least occurrences of errors. When dealing with the Internet we have all run into the situation where want to go to a URL or access a Web Service within code and assume that we have an Internet connection. But unbeknownst to us the router needs to be rebooted again and we are hung out to dry until the application times out.
In this example we will show you how to test for an Internet connection before you make that all important call to the Web Service so you can capture the fact that the Internet is down and display the appropriate message or branch off to another process.
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
To implement this example we will need to add a reference to an external dynamic link library and use the functionality within that library to accomplish our task.
This namespace is required in order to access the external library and must be added to your project as one of your “includes”. Failure to add this namespace will result in an error being thrown while you are trying to build the application. Once you add this library you will be able to add any external library, not just the one that we are dealing with in this example.
The external library is added to your project via the following syntax:
static extern bool InternetGetConnectedState(ref StateOfConnection lpdwFlags, int dwReserved);
What we have said is that we want to access the functionality within the “wininet.dll” and declared that library for use. There are two parameters that are required to use this library. They are:
lpdwFlags - Pointer to a variable that receives the connection description. This parameter can be one or more of the Connection State values. Please refer to the section “Definition of Connection States” in Fig. 1 further down in this article.
dwReserved - Must be zero
This external method returns a bool value that denotes whether there is an Internet connection or not.
- TRUE if there is an Internet connection
- FALSE if there is not an Internet connection
In our example we will be passing an instance of the enumeration to the external library as a ref parameter as well as passing a zero as the second ref parameter. The zero is a hard coded value and is not derived from any process and is not meant to denote anything in your application other than a required value for the external library.
The following is how we declare the enumeration that describes the connection condition and is passed to the external library by reference.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
public enum StateOfConnection
Modem = 0x1,
Lan = 0x2,
Proxy = 0x4,
Installed = 0x10,
OffLine = 0x20,
Configured = 0x40
In the above example we used the “Flags” attribute. What we are saying here is that you have an enumeration, that usually denotes an integer, but adding the “Flags” attribute to the enumeration declaration you are actually saying “Treat this enumeration as a bit field, that is, a set of flags.”
In the next article we will continue to build this example, adding the remaining lines of code and bringing everything mentioned in the article together so we can test our Internet connection.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
What have we learned?
- That CSharp has the ability to import external libraries easily
- That we can test an Internet Connection before we try to access it
- How to use an enumeration
- That an enumeration can be treated as a bit field