none
Check wheather the system connected to internet or not RRS feed

  • Question

  • What is the easiest way to check wheather the internet connection is available programatically?

            /// <summary>
            /// The internet get connected state.
            /// </summary>
            /// <param name="lpdwFlags">
            /// Pointer to a variable that receives the connection description. This parameter may return a valid flag even when the function returns FALSE.
            /// </param>
            /// <param name="dwReserved">
            /// This parameter is reserved and must be 0.
            /// </param>
            /// <returns>
            /// The <see cref="bool"/>.
            /// </returns>
            [System.Runtime.InteropServices.DllImport("wininet.dll", SetLastError = true)]
            private static extern bool InternetGetConnectedState(out int lpdwFlags, int dwReserved);

            /// <summary>
            /// Used to verify the the internet connection
            /// </summary>
            /// <returns>
            /// Returns TRUE if there is an active modem or a LAN Internet connection, or 
            /// FALSE if there is no Internet connection, or if all possible Internet connections are not currently active.
            /// </returns>
            private static bool IsConnectedtoInternet()
            {
                int description;
                return InternetGetConnectedState(out description, 0);
            }

    But this code return false even though the system settings all appeared to be good and Internet explorer / Chrome was able to connect.

    NOTE:

    I was using VPN from home earlier, put my laptop into sleep mode, came into the office and connected to the building network (VPN stopped). I had been using browsers to connect to the internet with no issue.

            
    • Edited by UDPatel Monday, July 23, 2018 10:14 AM
    Monday, July 23, 2018 10:09 AM

Answers

  • Have you tried IsInternetConnected ?

    [DllImport("Connect.dll", CharSet = CharSet.Unicode, SetLastError = true)]
            public static extern HRESULT IsInternetConnected();

    • Marked as answer by UDPatel Monday, July 23, 2018 12:29 PM
    Monday, July 23, 2018 10:48 AM
  • false means S_OK for HRESULT

    It can be defined as :

            public enum HRESULT : int
            {
                S_OK = 0,
                S_FALSE = 1,           
                E_FAIL = unchecked((int)0x80004005)
            }

    • Edited by Castorix31 Monday, July 23, 2018 12:10 PM
    • Marked as answer by UDPatel Monday, July 23, 2018 12:29 PM
    Monday, July 23, 2018 12:08 PM

All replies