` Printed Icetips Article

Icetips Article



Par2: Detecting internet connection
2003-09-02 -- Paul Attryde
 
> Can anyone maybe help me on this one... It's a Piece of VB code, for
> checking the Internet Connection... But for some reason.. it keep on
> returning a Zero
> 
> This is the VB Code:
> 
> Dim dwFlags As Long
> Dim sNameBuf As String, msg As String
> Dim lPos As Long
> sNameBuf = String$(513, 0)
> If InternetGetConnectedStateEx(dwFlags, sNameBuf, 512, 0&) Then
>    lPos = InStr(sNameBuf, vbNullChar)
>    If lPos > 0 Then
>      sConnectionName = Left$(sNameBuf, lPos - 1)
>    Else
>      sConnectionName = ""
>    End If
>    msg = "Your computer is connected to Internet" & vbCrLf & "Connection
> Name: " & sConnectionName
>    If (dwFlags And INTERNET_CONNECTION_LAN) Then
>        msg = msg & vbCrLf & "Connection use LAN"
>    ElseIf lFlags And INTERNET_CONNECTION_MODEM Then
>        msg = msg & vbCrLf & "Connection use modem"
>    End If
>    If lFlags And INTERNET_CONNECTION_PROXY Then msg = msg & vbCrLf &
> "Connection use Proxy"
>    If lFlags And INTERNET_RAS_INSTALLED Then
>       msg = msg & vbCrLf & "RAS INSTALLED"
>    Else
>       msg = msg & vbCrLf & "RAS NOT INSTALLED"
>    End If
>    If lFlags And INTERNET_CONNECTION_OFFLINE Then
>       msg = msg & vbCrLf & "You are OFFLINE"
>    Else
>       msg = msg & vbCrLf & "You are ONLINE"
>    End If
>    If lFlags And INTERNET_CONNECTION_CONFIGURED Then
>       msg = msg & vbCrLf & "Your connection is Configured"
>    Else
>       msg = msg & vbCrLf & "Your connection is not Configured"
>    End If
> Else
>    msg = "Your computer is NOT connected to Internet"
> End If
>    MsgBox msg, vbInformation, "Checking connection"
> End Sub

Off the top of my head it'd be something like this:

map
  module('wininet')
   InternetGetConnectedState(ulong,ulong,ulong,ulong),bool,pascal
  end
end

result bool
name cstring(512)

code
  result = InternetGetConnectedState( address(flags), address(name), 
size(name), 0)
  if result = 1
   ! connected
  else
   ! not connected
  end


But, make sure you also read these, because the first describes your 
problem exactly ...
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q315035
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q242558
http://www.codeguru.com/internet/isinternetconnected.shtml
http://groups.google.com/groups?q=detect+internet+connection&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=v2an1ugitk4o8ddh60ntle5kr8u6k5u6jk%404ax.com&rnum=2

AFAIK there's no 100% foolproof way to detect an internet connection - I 
believe you'll need a combination of 2 or 3 techniques to get close ...

James Cooke:
No more GPF. But - I sill get as constant "Not Connected" message.
This is the revised code:

    Program
    map
      module('wininet')
       InternetGetConnectedStateEx(ulong,ulong,ulong,ulong),bool,pascal
      end
    end

result bool
thename cstring(512)
Flags   long

INTERNET_CONNECTION_MODEM Equate(1h)
INTERNET_CONNECTION_LAN Equate(2h)
INTERNET_CONNECTION_PROXY Equate(4h)
INTERNET_RAS_INSTALLED Equate(10h)
INTERNET_CONNECTION_OFFLINE Equate(20h)
INTERNET_CONNECTION_CONFIGURED Equate(40h)


  code
  Flags = INTERNET_CONNECTION_CONFIGURED

  result = InternetGetConnectedStateEx( address(INTERNET_CONNECTION_LAN),
address(thename), size(thename), 0)
  if result = 1
   Message('Connected')
  else
   Message('Not Connected')
  end

>Paul Attryde: 
My mistake

InternetGetConnectedState() takes 2 params
InternetGetConnectedStateEx() takes 4.

Change the prototype & call to use InternetGetConnectedStateEx() and it 
should work fine.



Printed November 21, 2024, 7:23 am
This article has been viewed/printed 35311 times.