Link to home
Start Free TrialLog in
Avatar of Éric Moreau
Éric MoreauFlag for Canada

asked on

Connected to the Internet?

I run applications on my laptop. I have features that are trying to send e-mails. When I am not connected to the Internet, I have to skip these parts.

How can I know for sure if I am connected to the Internet?
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iboutchkine
iboutchkine

Imports System.Runtime.InteropServices

<DllImport("wininet.dll")> _
    Public Function InternetGetConnectedState( _
        ByVal lpdwFlags As Integer, ByVal res As Integer) As Boolean
End Function

Public Function InternetConnectionExists() As Boolean
    ' Determines if there is a connection to the Internet.
    ' Note that InternetGetConnectedState cannot be considered
    ' 100% reliable. An issue can occur if an internet connection
    ' has never been configured, the function may return true, even
    ' though there is no internet connection.
    '

    Dim e As Integer = 0
    Dim ret As Boolean = InternetGetConnectedState(e, 0)

    Return ret

End Function
Avatar of Éric Moreau

ASKER

iboutchkine, when I try your code, I get the following exeption (on the line: Dim ret As Boolean = InternetGetConnectedState(e, 0))

An unhandled exception of type 'System.InvalidProgramException' occurred in InternetConnection.exe

Additional information: Error: PInvoke item (field,method) must be Static.