[RESOLVED] Check if have internet VB6-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] Check if have internet VB6

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    21

    Resolved [RESOLVED] Check if have internet VB6

    Hello again. Please, I need you to help me on this one, if you can. I have a button, and what I want to do is that when I press that button, the program check if the machine has any connection to the internet. If so, to display a msgbox saying (it has), and vieversa.

    Thanks you

  2. #2
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,920

    Re: Check if have internet VB6

    Code:
    Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _
    (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, _
    ByVal dwNameLen As Integer, ByVal dwReserved As Long) As Long
    
    Dim sConnType As String * 255
    
    Private Sub Command1_Click()
        If (CheckInternetConnection = True) Then
            MsgBox "We have an active Internet connection!"
        Else
            MsgBox "We don't have an active Internet connection!"
        End If
    End Sub
    Function CheckInternetConnection() As Boolean
        Dim aux As String * 255
        Dim r As Long
        r = InternetGetConnectedStateEx(r, aux, 254, 0)
        If r = 1 Then
            CheckInternetConnection = True
        Else
            CheckInternetConnection = False
        End If
    End Function
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,591

    Re: Check if have internet VB6

    internetconnectedstate will return true if there is a valid LAN connection, regardless of whether there is a WAN connection, so it is not a reliable test

    ideally you need to ping your isp server, or download a small file from somewhere, for a more reliable test
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,920

    Re: Check if have internet VB6

    Yes you are right pete... Ok try this (I like your ping idea... I tried it with Yahoo and it worked )

    Code:
    Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" _
    (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, _
    ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
    
    Private Declare Function InternetCloseHandle Lib "wininet.dll" _
    (ByVal hInet As Long) As Long
    
    Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" _
    (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As _
    String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
    
    Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    Private Const INTERNET_FLAG_RELOAD = &H80000000
    Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
    Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
    
    Private Sub Command1_Click()
        Dim hInet As Long, hUrl As Long, Flags As Long, url As Variant
      
        hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, _
        vbNullString, vbNullString, 0&)
       
        If hInet Then
            Flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE _
            Or INTERNET_FLAG_RELOAD
            
            hUrl = InternetOpenUrl(hInet, "http://www.yahoo.com", _
            vbNullString, 0, Flags, 0)
            
            If hUrl Then
                MsgBox "Your computer is connected to Internet", _
                vbInformation, "Checking connection"
                Call InternetCloseHandle(hUrl)
            Else
                MsgBox "Your computer is not connected to Internet", _
                vbInformation, "Checking connection"
            End If
        End If
        
        Call InternetCloseHandle(hInet)
    End Sub
    Last edited by Siddharth Rout; Mar 7th, 2009 at 03:12 PM. Reason: Spell Check
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2009
    Posts
    21

    Re: Check if have internet VB6

    Well, really thanks you on this one guys. I have tested it with my internet on and it worked, later I changed the ping url to iwegweiugbwi (or something) and it said i wasn't connected.
    The only problem is if yahoo server turn inactive... jeje

    Thanks you

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    19,591

    Re: [RESOLVED] Check if have internet VB6

    The only problem is if yahoo server turn inactive... jeje
    that is why i suggested pinging your isp server, if it is inactive then you probably have no connection anyway

    presumably if your internet is disconnected then internetopen will return 0, without actually going to any site
    Last edited by westconn1; Mar 7th, 2009 at 05:13 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.