Results 1 to 6 of 6

Thread: check if internet is connected or not

  1. #1

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Red face check if internet is connected or not

    add this in module


    Code:
    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
    
    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 " & Err & " Has Occured!", vbOKOnly + vbCritical, "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



    Code:
    Private Sub Command2_Click()
    Text1.Text = IsConnected
    
    End Sub

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: check if internet is connected or not

    What operating systems does the code work under?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: check if internet is connected or not

    i have windows xp sp2 not tested on others

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: check if internet is connected or not

    The magic number &H2000 should really be &H2000& since it is a long/Enum. It is RASCS_Connected, which is set to RASCS_DONE, which is a constant &H2000&.

    Magic numbers should be avoided.


    Worse yet, this code is useless unless you are using Dial-Up Networking (Remote Access Service). And it only checks your first dial connection (you might have several and it ignores the others).

  5. #5

    Thread Starter
    Banned
    Join Date
    Nov 2012
    Posts
    1,171

    Re: check if internet is connected or not

    with respect il like to say it isnt usless like i am on dial up its usefull for me, so there are multi million people out there also will have same connection as me and will use this i tested this and works as i wanted

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: check if internet is connected or not

    Here's one I found somewhere. Don't know if it's useful or not

    Code:
    Public gWebConnection As Boolean
    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
    Public 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
    Private Sub Command1_Click()
     If (CheckInternetConnection = True) Then
       MsgBox "Your connected"
     Else
       MsgBox "Your not connected"
     End If
    End Sub


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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