Results 1 to 28 of 28

Thread: Detecting Internet Connection

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Hi.

    How can I tell if a PC is currently connected to the Internet through VB?

    Thanks.

  2. #2
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    Here's the code to check.......

    Put this code in your form

    Public Function IsConnected() As Boolean
    Dim TRasCon(255) As RASCONN95
    Dim lg As Long
    Dim lpcon As Long
    Dim RetVal As Long
    Dim Tstatus As RASCONNSTATUS95
    '
    TRasCon(0).dwSize = 412
    lg = 256 * TRasCon(0).dwSize
    '
    RetVal = RasEnumConnections(TRasCon(0), lg, lpcon)
    If RetVal <> 0 Then
    MsgBox "ERROR"
    Exit Function
    End If
    '
    Tstatus.dwSize = 160
    RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
    If Tstatus.RasConnState = &H2000 Then
    IsConnected = True
    Else
    IsConnected = False
    End If

    End Function

    Private Sub Form_Load()
    IsConnected
    MsgBox IsConnected
    End Sub

    And put this code in a module

    Option Explicit

    Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
    Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
    '
    Public Const RAS95_MaxEntryName = 256
    Public Const RAS95_MaxDeviceType = 16
    Public Const RAS95_MaxDeviceName = 32
    '
    Public Type RASCONN95
    dwSize As Long
    hRasCon As Long
    szEntryName(RAS95_MaxEntryName) As Byte
    szDeviceType(RAS95_MaxDeviceType) As Byte
    szDeviceName(RAS95_MaxDeviceName) As Byte
    End Type
    '
    Public Type RASCONNSTATUS95
    dwSize As Long
    RasConnState As Long
    dwError As Long
    szDeviceType(RAS95_MaxDeviceType) As Byte
    szDeviceName(RAS95_MaxDeviceName) As Byte
    End Type

    Regards,

    Hutchie

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post Thanks Hutchie!

    Hutchie,

    It works like a charm!

    Thanks again.


    Edited by OneSource on 02-26-2000 at 10:21 AM

  4. #4
    Addicted Member
    Join Date
    Apr 1999
    Location
    Freeport
    Posts
    204

    Post Is This a Ping

    Hutchie does this also act like a ping program

  5. #5
    New Member
    Join Date
    Feb 2000
    Posts
    2

    Post

    One can check if one is connected without using ras, like this:

    Module:
    Public Const ERROR_SUCCESS = 0&
    Public Const APINULL = 0&
    Public Const HKEY_LOCAL_MACHINE = &H80000002
    Public ReturnCode As Long

    Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
    hKey As Long) As Long

    Declare Function RegOpenKey Lib "advapi32.dll" Alias _
    "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
    String, phkResult As Long) As Long

    Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
    "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
    As String, ByVal lpReserved As Long, lpType As Long, _
    lpData As Any, lpcbData As Long) As Long

    Form code:
    Public Function IsConnected() As Boolean
    Dim hKey As Long
    Dim lpSubKey As String
    Dim phkResult As Long
    Dim lpValueName As String
    Dim lpReserved As Long
    Dim lpType As Long
    Dim lpData As Long
    Dim lpcbData As Long
    IsConnected = False
    lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
    ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, _
    phkResult)

    If ReturnCode = ERROR_SUCCESS Then
    hKey = phkResult
    lpValueName = "Remote Connection"
    lpReserved = APINULL
    lpType = APINULL
    lpData = APINULL
    lpcbData = APINULL
    ReturnCode = RegQueryValueEx(hKey, lpValueName, _
    lpReserved, lpType, ByVal lpData, lpcbData)
    lpcbData = Len(lpData)
    ReturnCode = RegQueryValueEx(hKey, lpValueName, _
    lpReserved, lpType, lpData, lpcbData)

    If ReturnCode = ERROR_SUCCESS Then
    If lpData = 0 Then
    IsConnected = False
    Else
    IsConnected = True
    End If
    End If
    RegCloseKey (hKey)
    End If
    end function

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post More than 1 way to skin a cat.

    Arsi Rantala,

    Thanks for your reply.

    I tried out your code and it worked also. Your code and Hutchie's contain almost exactly the same number of lines. I can't see where one is more efficient or reliable than the other. Therefore, I'll keep both examples.

    Is there any particular reason why your method is better than using ras as Hutchie illustrated?

    Thanks.

  7. #7
    New Member
    Join Date
    Feb 2000
    Posts
    2

    Post Which one to use...


    Arsi Rantala,

    Thanks for your reply.

    I tried out your code and it worked also. Your code and Hutchie's contain almost exactly the same number of lines. I can't see where one is more efficient or reliable than the other. Therefore, I'll keep both examples.

    Is there any particular reason why your method is better than using ras as Hutchie illustrated?

    Thanks.
    Well I have had trouble doing ras stuff in nt, so my guess is that my code would work better in nt than that other code. can't be sure, haven't tested that - yet. (gives errors about rasapi32.dll - if one - like me - hasn't installed ras to nt).

  8. #8
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    Hi One Source / Arsi,

    I've not had any trouble in using this code in NT or Win 9.X, I must admit though it is preference.
    Uou are perfectly right in what your saying though, I just find this more effective using Ras if you know it's there.



    ScottF - Sorry on the late reply, no this can't be used as a ping program.

    Hutchie.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Thanks for the info, guys.

  10. #10
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post Hmm All good but. . .

    Hutchie, I'm getting a Ambiguous Name error in your code. . . the line that's triggering it is

    Dim TRasCon(255) As RASCONN95


    Mind you, I'm trying to pull this off in VBA/Access.


    In addition, will your code work if not using Dial-Up Networking? I know for a fact that Arsi's won't.

    I'm looking for a "universal" test for internet connectivity, not relying upon Dial-Up.

  11. #11
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    John,

    This is only available for a Dial up connection, sorry!

    If I get time over the next couple of days I'll see if there is any way of detecting any "connection"

    Hutchie

  12. #12
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post Theoretically

    Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long


    THat function should work.

    But I don't know that much about VB API calls (read as: almost nothing) so I don't know what I should be shoving into the parameters.

    Any input would be spectacular.

  13. #13
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    John / All source,

    I've been messing about with your aPi John.

    Here's both via Lan and via modem connection, hope this helps you out now !!!

    Add this code to a module :

    Option Explicit
    Public Declare Function InternetGetConnectedState _
    Lib "wininet.dll" (ByRef lpSFlags As Long, _
    ByVal dwReserved As Long) As Long

    Public Const INTERNET_CONNECTION_LAN As Long = &H2
    Public Const INTERNET_CONNECTION_MODEM As Long = &H1

    Public Function ViaLAN() As Boolean

    Dim SFlags As Long
    'return the flags associated with the connection
    Call InternetGetConnectedState(SFlags, 0&)

    'True if the Sflags has a LAN connection
    ViaLAN = SFlags And INTERNET_CONNECTION_LAN

    End Function
    Public Function ViaModem() As Boolean

    Dim SFlags As Long
    'return the flags associated with the connection
    Call InternetGetConnectedState(SFlags, 0&)

    'True if the Sflags has a modem connection
    ViaModem = SFlags And INTERNET_CONNECTION_MODEM

    End Function

    Add this code to a form with 1 command button andd two text boxes. It will return "True" for which ever one you are connected to.

    Option Explicit

    Private Sub Command1_Click()
    Text1 = ViaLAN()
    Text2 = ViaModem()
    End Sub

    Hutchie

  14. #14
    Addicted Member
    Join Date
    Apr 1999
    Location
    Freeport
    Posts
    204

    Post Ping Code

    Hutchie do you know of a ping code I can learn off of.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post Wrong Name ... but

    Hutchie,

    The name's OneSource. But I don't mind the mixup. I hadn't considered that the code that you and Arsi posted before was only for a dial-up connection. If the code you just put up also covers LAN's, that definetly makes my app better!

    Thanks.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post Sorry fellas but ....

    Hutchie,

    I tried out your code. It detected if I was using a LAN or dial-up (modem) connection, but the problem is it returns the same results whether I am connected to the Internet or not - via Modem - yes and via LAN - no.

    So, this brings us back to John's question:
    ... will your code work if not using Dial-Up Networking?
    If my understanding is correct, we still will not be able to determine if someone is connected via LAN because we can't use ras or Arsi's code. We will only be able to detect what kind of connection that person has with your last post Hutchie.

    Please correct me if I'm wrong.

    Thanks.


    Edited by OneSource on 02-28-2000 at 07:42 PM

  17. #17
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    OneSource,

    I've only checked that this works via 95/98 but not yet NT.

    I've now been able to check that this will work if you are connected via a LAN. IT DOES !. Lan TRUE modem FALSE

    I have than dialed up via my modem and it changes to Modem TRUE, LAN FALSE.

    Are you connected via Lan by Proxy ?, I think there may some other constant for this.

    Hutchie.


  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    Hutchie,

    I only have access to a Windows '95 dial-up connection.

    In order to see what I'm talking about, test your code using a dial-up connection when you are not connected to the Internet. The results that I got were the same whether I was connected or not: Modem - yes and LAN - no.

    Are we to assume that if someone has a LAN connection they are ALWAYS connected to the Internet?

    If so, the problem is solved. If not, your code will only tell what type of connection exists - LAN or dial-up - not whether a PC with a LAN connection is on-line or not.

    I hope you understand what I'm trying to convey.

    Thanks for your help on this.

  19. #19
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post Hmmm this could be alleviated

    If we make the assumption that LAN access = Internet Access then we can use the following:

    A) Run Hutchie's code to determine LAN vs Modem.

    If LAN, assume connection exists.

    If Modem, run additional code to determine if Dial-Up Networking is active.


    That would do it; only problem is LAN <> Internet Access.

    I was sent some code by a member here that may get around this via pinging a known website; it's a slower method but it is pretty certain to work. Unfortunately, this one is browser specific.

    You just can't win ;P

    Code:
    'Declares for direct ping
    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 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 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
    Dim checkType As Integer
    Dim remMsg(2) As String
    
    'End of Declarations, begin function
    Private Sub CheckConnection3()
       Dim sTmp As String
       Dim hInet As Long
       Dim hUrl As Long
       Dim Flags As Long
       Dim url As Variant
       hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
       sTmp = Me.Caption
       Me.Caption = "Checking connection with www.yahoo.com..."
       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, "Checing connection"
             Call InternetCloseHandle(hUrl)
          Else
             MsgBox "Your computer is not connected to Internet", vbInformation, "Checing connection"
          End If
       End If
       Call InternetCloseHandle(hInet)
       Me.Caption = sTmp
    End Sub
    This is from Arkadiy Olovyannikov
    I can't remember his handle, but much credit goes to him.

    P.S. His code had 3 parts to it, so I grabbed what looked like all the Direct Internet stuff.
    I'd be happy to forward any of you the project he sent me... just email me.

  20. #20
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    OK.

    After getting so much greif I've managed to do ALL of what you require. My former code will work by adding this to the module:
    Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
    Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40

    Public Function Online() As Boolean
    'If your online it will return true, if not as you guessed False
    Online = InternetGetConnectedState(0&, 0&)
    End Function

    Add another text box to your form and paste this line into the command button again
    Text3 = Online()

    This should now work and check whether the connection is active aswell.

    I have checked this both online and off with modem and my LAN and it works fine.

    Hope this helps, without the use of pinging !!!

    Kind regards,

    Hutchie

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post Yes!

    Hutchie,

    It definitely wasn't my intention to give you any grief! I really appreciate all you did to help me (and John) out!

    Now the code works regardless if the connection is LAN or dial-up! Remember, the type of connection (dial-up or LAN) was never the main issue. I refer you to my original post that started all this:
    How can I tell if a PC is currently connected to the Internet through VB?
    To review, we got into this LAN/dial-up connection business because the solutions that you and Arsi provided to my initial post worked for dial-up's, but not for LAN's.

    This is the code that I ended up using:
    Code:
    Option Explicit
    
    Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
    
    Public Function Online() As Boolean
        'If you are online it will return True, otherwise False
        Online = InternetGetConnectedState(0&, 0&)
    End Function
    .

    In addition to being valid for a LAN connection, it is much shorter than the solutions that you and Arsi posted to my original query. It also avoids using the 2 R's - RAS and the Registry.

    Another positive development from all this is that ScottF got his ping code from John's post that pinged Yahoo.

    So, in this case John,
    You just can't win ;P
    it seems like we did win!

    Thanks again Hutchie and to all others who contributed to this thread!


    Edited by OneSource on 03-02-2000 at 09:18 AM

  22. #22
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post

    Well, we got tons done.
    :)

    Code:
    Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
    
    Public Function Online() As Boolean
        'If you are online it will return True, otherwise False
        Online = InternetGetConnectedState(0& ,0& ;)
    End Function
    Online = InternetGetConnectedState(0& ,0& ;)
    that line gives me a syntax error. What am I missing?

  23. #23
    Guest

    Post

    hehe... you had a typing error:

    Code:
    Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
    
    Public Function Online() As Boolean
        'If you are online it will return True, otherwise False
        Online = InternetGetConnectedState(0& ,0&)
    End Function
    taLON



    Edited by taLON on 03-02-2000 at 09:06 AM

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Mobile, AL, USA
    Posts
    600

    Post

    John,

    Put this line of code into your project:

    Online = InternetGetConnectedState(0&, 0& )

    The problem here is being caused because UBB's formatting is converting the right part of the statement into a smiley face: 0&). If you copy and paste the line above into your project as it is, it will work.

    All the best.

  25. #25
    Lively Member
    Join Date
    Jul 1999
    Posts
    78

    Post Danke

    That'll do it

  26. #26
    New Member
    Join Date
    May 2000
    Location
    South Africa
    Posts
    1

    Smile Problem with using the Registry to track active connections

    I am using Win 2000 and have found that the Registry method of tracking active connections doesn't work. It appears that win2k doesn't make that entry in the registry. However, I have use the API InternetGetConnectedStateEx and have found that it works.

    I am now faced with a new issue. I need to find a way of reading all the Dial UP Networking connection available on the machine. Anyone have any bright ideas?

  27. #27
    Hyperactive Member Krass's Avatar
    Join Date
    Aug 2000
    Location
    Montreal
    Posts
    489
    I tried to read out most of this long thread and wasn't able to find out what's not working here. All I want to know is if the current computer is connected to the net, here's what I got:

    Private Declare Function InternetAutodial Lib "wininet.dll" (ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
    Private Const INTERNET_AUTODIAL_FORCE_ONLINE = 1
    Private Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
    Private Declare Function InternetAutodialHangup Lib "wininet.dll" (ByVal dwReserved As Long) As Long
    Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long

    Whenever I try this following line of code:
    MsgBox InternetGetConnectedState(0&, 0&)

    It will return '1', always. Why is that?

    Chris

  28. #28
    Member
    Join Date
    Jan 1999
    Location
    Warner Robins, GA
    Posts
    37
    Have you tried this with your computer connected to a LAN then checking the return value when you unplug the network cable??? When I did it, it still returned a TRUE.

    Thanks.

    Originally posted by Hutchie
    [B]OK.

    After getting so much greif I've managed to do ALL of what you require. My former code will work by adding this to the module:
    Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
    Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40

    Public Function Online() As Boolean
    'If your online it will return true, if not as you guessed False
    Online = InternetGetConnectedState(0&, 0&)
    End Function

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