Internet connection active or not ?

The Microsoft WinINet API is of great importance as it is used to guide applications towards standard Internet protocols, among others including FTP (File Transfer Protocol) and HTTP (Hypertext Transfer Protocol). The usage of this API will be among the developers' options in order for them to create the smooth link with the remote servers and online resources.

InternetGetConnectedState function

In many cases, it is essential to check if a computer has an active Internet connection before attempting to establish a connection using a specific communication interface. To address this requirement, the InternetGetConnectedState function proves to be invaluable. This function allows developers to determine whether an active internet connection exists on the computer or not.

[DllImport("wininet.dll")] private extern static bool InternetGetConnectedState_ (out int conn, int val);

Invoking the InternetGetConnectedState function, the connected state of the local system can be retrieved. The function returns a boolean value, with a return value of TRUE indicating that at least one connection to the Internet is available. This knowledge is crucial as it helps applications make informed decisions about proceeding with online operations or handling scenarios where an internet connection is unavailable. The following C# program shows how to check a system has an active internet connection or not.

Full Source C#
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int conn, int val); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int Out; if (InternetGetConnectedState(out Out, 0) == true) { MessageBox.Show("Connected !"); } else { MessageBox.Show("Not Connected !"); } } } }

Conclusion

Utilizing the InternetGetConnectedState function in conjunction with the WinINet API, developers can ensure their applications effectively handle internet connectivity scenarios and deliver a smooth user experience.