InternetGetConnectedState (wininet)
Last changed: -67.166.68.151

.
Summary
TODO - a short description

C# Signature:

[DllImport("wininet.dll", SetLastError=true)]
extern static bool InternetGetConnectedState(
   out InternetGetConnectedStateFlags Description, int ReservedValue);

VB Signature:

  Private Declare Function InternetGetConnectedState Lib "wininet.dll" _
  (ByRef lpdwFlags As Int32, ByVal dwReserved As Int32) As Boolean

User-Defined Types:

InternetGetConnectedStateFlags

Notes:

Private Enum ConnectionStates
    Modem = &H1
    LAN = &H2
    Proxy = &H4
    RasInstalled = &H10
    Offline = &H20
    Configured = &H40
End Enum

Tips & Tricks:

Please add some!

Sample Code:

C: http://support.microsoft.com/default.aspx?scid=kb;EN-US;242558

C#:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Runtime.InteropServices;

namespace ConsoleApplication2

{

    class Program
    {
    static void Main(string[] args)
    {
        Console.WriteLine(IsConnectedToInternet().ToString());
        Console.ReadLine();
    }

    [DllImport("wininet.dll", SetLastError = true)]
    extern static bool InternetGetConnectedState(
       out InternetGetConnectedStateFlags Description, int ReservedValue);

    [Flags]
    enum InternetGetConnectedStateFlags
    {
        INTERNET_CONNECTION_MODEM = 0x01,
        INTERNET_CONNECTION_LAN = 0x02,
        INTERNET_CONNECTION_PROXY = 0x04,
        INTERNET_CONNECTION_RAS_INSTALLED = 0x10,
        INTERNET_CONNECTION_OFFLINE = 0x20,
        INTERNET_CONNECTION_CONFIGURED = 0x40
    }

    public static bool IsConnectedToInternet()
    {
        InternetGetConnectedStateFlags flags;
        var ret = InternetGetConnectedState(out flags, 0);
        Console.WriteLine(flags.ToString());
        return ret;
    }
    }

}

VB

Function Get_InternetConnectedState(ByRef p_lngFlags As Long, Optional ByRef p_return_str As String = "") As Boolean

   Dim lngFlags As Long

      lngFlags = 0
      Get_InternetConnectedState = False

      If InternetGetConnectedState(lngFlags, 0) Then
     'connected.
     If lngFlags And ConnectionStates.LAN Then
        'LAN connection.
        p_return_str = "LAN connection."
     ElseIf lngFlags And ConnectionStates.Modem Then
        'Modem connection.
        p_return_str = "Modem connection."
     ElseIf lngFlags And ConnectionStates.Proxy Then
        'Proxy connection.
        p_return_str = "Proxy connection."
     End If

     Get_InternetConnectedState = True

      Else
     'not connected.
     p_return_str = "Not connected."
     Get_InternetConnectedState = False
      End If

      p_lngFlags = lngFlags

End Function

Alternative Managed API:

Try NetworkInterface.GetIsNetworkAvailable() in the the System.Net.NetworkInformation namespace. It returns true when a network is available and false otherwise. See also: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getisnetworkavailable(VS.80).aspx. NOTE: this is all new in .NET 2.0!

Documentation

Direct Link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/internetgetconnectedstate.asp