Results 1 to 11 of 11

Thread: is it connected to the internet?

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    is it connected to the internet?

    i want to find out if the computer that my program is running,
    is connected to the internet!

    i tried this code that i found here,
    but it shows that i'm always online, which is false,
    cuz i unplug my connection

    here's the code:

    VB Code:
    1. Private Const INTERNET_OPEN_TYPE_DIRECT = 1
    2. Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
    3.  
    4. Private Declare Function InternetOpen Lib "wininet.dll" _
    5. Alias "InternetOpenA" _
    6. (ByVal lpszAgent As String, _
    7. ByVal dwAccessType As Long, _
    8. ByVal lpszProxyName As String, _
    9. ByVal lpszProxyBypass As String, _
    10. ByVal dwFlags As Long) As Long
    11.  
    12. Private Declare Function InternetCloseHandle Lib "wininet.dll" _
    13. (ByVal hEnumHandle As Long) As Long
    14.  
    15. Public Function InternetConnectOpen() As Boolean
    16.  
    17. Dim hInternet As Long
    18.  
    19. InternetConnectOpen = False
    20. hInternet = InternetOpen("Vb FTP Transfer", INTERNET_OPEN_TYPE_DIRECT, _
    21. vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE)
    22.  
    23. If hInternet > 0 Then InternetConnectOpen = True
    24. Call InternetCloseHandle(hInternet)
    25.  
    26. hInternet = 0
    27.  
    28. End Function
    29.  
    30. Private Sub Command1_Click()
    31. If InternetConnectOpen Then
    32.     MsgBox "online"
    33. Else
    34.     MsgBox "offline"
    35. End If
    36. End Sub

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    this might work:

    VB Code:
    1. Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
    2. If InternetGetConnectedState(0, 0) Then
    3.     MsgBox "Online"
    4. Else
    5.     MsgBox "Offline"
    6. End If
    -= a peet post =-

  3. #3

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    sorry Peet,
    that the one i tried before the other one,

    i had found one here once, but i can't find it on my pc
    damm!

    tx

  4. #4
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    i researched this in DEPTH (because i needed this to work sooo bad)what i came up with was
    all functions and coding i could write or find only worked for dialups

    they worked fine if you were on a dialup
    but failed if you were on high speed (like @home)

    so here is thefinal solution ic ame up with to work for both

    you have to PING a site using vb
    and determine if they are connected based on that ping
    *usually you dont want to ping someone elses server cus they will get mad, so you must ping a computer thats yours, and that you know will be online always*

  5. #5

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    tx Kovan i'll try this,
    but i found the code that i had ,

    it's work on lan, but when your not connected, it takes a few seconds...

    this code was made to put in a module, NO form!!

    VB Code:
    1. Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
    2. Private Const INTERNET_FLAG_RELOAD = &H80000000
    3. Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
    4. Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
    5.  
    6. Private Declare Function InternetOpen _
    7. Lib "wininet.dll" Alias "InternetOpenW" _
    8. (ByVal lpszAgent As Long, _
    9. ByVal dwAccessType As Long, _
    10. ByVal lpszProxyName As Long, _
    11. ByVal lpszProxyBypass As Long, _
    12. ByVal dwFlags As Long) As Long
    13.  
    14. Private Declare Function InternetOpenUrl _
    15. Lib "wininet.dll" Alias "InternetOpenUrlW" _
    16. (ByVal hInet As Long, _
    17. ByVal lpszUrl As Long, _
    18. ByVal lpszHeaders As Long, _
    19. ByVal dwHeadersLength As Long, _
    20. ByVal dwFlags As Long, _
    21. ByVal dwContext As Long) As Long
    22.  
    23. Private Declare Function InternetCloseHandle _
    24. Lib "wininet.dll" (ByVal hInet As Long) As Long
    25.  
    26. Public Function TestConnection(stSite As String) As Boolean
    27. Dim hInet As Long
    28. Dim hUrl As Long
    29. Dim lFlags As Long
    30. Dim url As Variant
    31.  
    32. hInet = InternetOpen(StrPtr(App.Title), INTERNET_OPEN_TYPE_PRECONFIG, 0&, 0&, 0&)
    33. If hInet Then
    34. lFlags = INTERNET_FLAG_KEEP_CONNECTION Or _
    35. INTERNET_FLAG_NO_CACHE_WRITE Or _
    36. INTERNET_FLAG_RELOAD
    37. hUrl = InternetOpenUrl(hInet, StrPtr(stSite), 0&, 0, lFlags, 0)
    38. If hUrl Then
    39. TestConnection = True
    40. Call InternetCloseHandle(hUrl)
    41. hUrl = 0
    42. End If
    43. End If
    44. Call InternetCloseHandle(hInet)
    45. hInet = 0
    46. End Function
    47.  
    48.  
    49. Sub Main()
    50. If TestConnection("http://www.vbforums.com") Then
    51.     MsgBox "online"
    52. Else
    53.     MsgBox "offline"
    54. End If
    55. End
    56. End Sub

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    do this
    test it with reguarl lan
    then unplug your lan connection
    and test it agian
    it will still return your online

  7. #7

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    does do it for you,

    for me it says that i'm offline,

    i tried it, the only thing is that it take a few second when your offline
    to detect it!!

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    but your using a site to test the connection with (which in essense your "pinging" that site
    which is what i said i think
    hehe

    you still need server or a site to test it with..

  9. #9

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    Originally posted by kovan
    you still need server or a site to test it with..
    i know, but i can use my company website!!
    i think that will do,

    BUT does your pinging thing long when you not connected??

    cuz you see what i mean with the code that i post,
    when your not connected it is long!!

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    yes it does
    cus when it pings it *if the site is online* you get a quick response
    if the ping is not successful, then it takes longer to return because each ping does 3 tries i blieve
    and if the first is successful it returns true
    if the 1st fails, goes to 2nd, and 3rd..

    but if that site your pinging is down
    you will never get the correct results

  11. #11

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    yeah your right your right for the site down!!

Posting Permissions

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



Click Here to Expand Forum to Full Width