Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6194
  • Last Modified:

Detecting LAN connection using InternetGetConnectedState API doesn't work

In VB6, I'm trying to test different connection scenarios using the InternetGetConnectedStatus API, e.g. testing if connected via modem, via LAN, etc.

My development environment is: Win 2K, VB6, T1 connection.

For some reason, after I created a DUN entry, the return code for 'connected via modem' (INTERNET_CONNECTION_MODEM) returned true, even though I didn't dial-out to the Internet and therefore, wasn't connected to the Internet.

Then when I deleted the one and only DUN entry, the return code for 'connected via LAN' always returned false and so did the 'connected via modem'.

I need to implement code (on all Windows OS platforms) that will determine if the user has an Internet connection or not and if not, my app needs to provide a drop-down list of DUN entries.

Before I experienced the problems after creating the DUN entry, I previously tried disconnecting the Ethernet cable from my PC and then ran my test app.  The 'connected via LAN' always returned true.  Why is this?

Please advise to why the InternetGetConnectedState API is not returning a value saying that I'm connected to the LAN when I am and visa versa.

Thanks.
   JBG
0
halfondj
Asked:
halfondj
  • 8
  • 5
  • 4
  • +1
1 Solution
 
Richie_SimonettiCommented:
0
 
Richie_SimonettiCommented:
some other examples:

Private Const FLAG_ICC_FORCE_CONNECTION = &H1
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    If InternetCheckConnection("http://www.allapi.net/", FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
        MsgBox "Connection to http://www.allapi.net/ failed!", vbInformation
    Else
        MsgBox "Connection to http://www.allapi.net/ succeeded!", vbInformation
    End If
End Sub
0
 
Richie_SimonettiCommented:
Private Const INTERNET_CONNECTION_CONFIGURED = &H40
Private Const INTERNET_CONNECTION_LAN = &H2
Private Const INTERNET_CONNECTION_MODEM = &H1
Private Const INTERNET_CONNECTION_OFFLINE = &H20
Private Const INTERNET_CONNECTION_PROXY = &H4
Private Const INTERNET_RAS_INSTALLED = &H10
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim Ret As Long
    Me.AutoRedraw = True
    'retrieve the connection status
    InternetGetConnectedState Ret, 0&
    'show the result
    If (Ret And INTERNET_CONNECTION_CONFIGURED) = INTERNET_CONNECTION_CONFIGURED Then Me.Print "Local system has a valid connection to the Internet, but it may or may not be currently connected."
    If (Ret And INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN Then Me.Print "Local system uses a local area network to connect to the Internet."
    If (Ret And INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM Then Me.Print "Local system uses a modem to connect to the Internet."
    If (Ret And INTERNET_CONNECTION_OFFLINE) = INTERNET_CONNECTION_OFFLINE Then Me.Print "Local system is in offline mode."
    If (Ret And INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY Then Me.Print "Local system uses a proxy server to connect to the Internet."
    If (Ret And INTERNET_RAS_INSTALLED) = INTERNET_RAS_INSTALLED Then Me.Print "Local system has RAS installed."
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
halfondjAuthor Commented:
I've ran both sample programs, they're similiar to my test app, and they return the wrong (the same as mine) values.

Thanks anyway.
0
 
halfondjAuthor Commented:
I just tried the sample using InternetCheckConnection and that seems to work, but after now learning about that API I did a search on EE and came up w/this article:  http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_20001419.html

In short, the article states:

:
1. There are several ways to detect the network state:

   I.  Use the InternetCheckConnection WinInet API call.
       Pros: it's a standard API call.
       Cons: It doesn't work in all WinInet versions, you
       may get an access violation (MS confirmed it as a
       bug). Also, each call sends 3(!) http packets and
       may be quite a burden on both client & server.
:

Is this true?  Also, does it work on all OS platforms?  Why doesn't the InternetGetConnectedState API work on my PC anymore?

Thanks again!!
0
 
Richie_SimonettiCommented:
Not all wininet.dll exposes same functions (Thanks Micro$oft!)

Second point of the above url should be good enought but it doesn't say you how it is connected.

Could you try this?
http://www.mvps.org/vbnet/index.html?code/network/internetgetconnectedstate.htm
0
 
halfondjAuthor Commented:
The InternetGetConnectedState.htm from mvps.org is the model program that I took for my sample program.  After running it, even though I do not have any DUN entries and I'm definitely connected to the Internet via LAN, the 'IsConnectViaLAN' is always returning 'False'.

I don't know why.  When I log off w/my userid on my PC and log on as a different user, the sample programs work fine.  BTW- There hasn't been any DUN entries configured logged on as the second user.

It seems to have to do w/a registery setting after I created the DUN entry.

I'm stumped and really don't know which API is reliable for my application.

I have very limited experise in this field and would appreciate advise to what APIs are reliable and which ones I should use.  It just seems like the InternetConnectState is not reliable.

Thanks and please advise.
0
 
halfondjAuthor Commented:
I figured out my problem.  Even though all the DUN entries were deleted, I had to go into 'Network and Dial-up Connections' and specify that I was connecting via the LAN.  Once I did that, the InternetGetConnectedState worked fine.
0
 
leoprexCommented:
I cannot understand what do you mean with 'specify that I was connecting via the LAN'.
I have the same problem as you. InternetConnectedStateEx says always no LAN.
Do you have some more information?
0
 
halfondjAuthor Commented:
On a Win2K PC:

1. Select 'Make New Connection'.  The 'New Connection Wizard' dialog box should display.
2. Choose 'Dial-up To The Internet'.  The 'Internet Connection Wizard' dialog box should display.
3. Choose the 3rd option - I want to set up my Internet connection manually, or I want to connect through a local area network (LAN).

The above should reset your Internet connection via the LAN, not the modem.

Hope that helps.

Let us know how you make out.
0
 
leoprexCommented:
Why should I create a new LAN connection? The existing one is running perfectly. I have also remote access connections on my computer but I use them very seldom: normally I am not connected through the modem to the phone line.
The problem is with a lap top (DELL Inspiron 8100) with a 3com Etherlynk PCMCIA card, connected through an optic fiber to a provider, with 10 Mbytes/sec speed.
The answers of my program using InternetGetConnectedStateEx are the following:
Not Connected:
Connection is Not Configured.
System has RAS installed.
I have run exactly the same program on a desktop with W98 and a 100
Mbytes/sec ethernet connection through a LAN card to a company server. This time the answers are correct:
Connected: Connessione su rete locale (this is the connection's name)
Connection uses LAN
Connection is via proxy
In effect on that desktop there is no RAS configured.
On my laptop I get the same inconsistent results using "Public Declare Function IsNetworkAlive Lib "Sensapi" (lpdwFlags As Long) As Long".
Some suggestions?
Thank you

0
 
halfondjAuthor Commented:
Your problem is different than what I was experiencing.  The reason why I needed to create another LAN connection is because when I created the DUNs (to use the modem), whenever I ran my program, the DUN dialog would display.  When I deleted all the DUNs, the dialog wouldn't display, but the LAN connection was no longer valid [working].  In order for the LAN connection to become valid again, I had to re-create it.

I'm not sure to why your program wouldn't be running properly on your laptop.  All the PCs I test on have RAS installed.

I'm using 'InternetGetConnectedState, not InternetGetConnectedStateEx.  The following is the prototype I coded:

'---------------------------------------------------------------------------------------
' Windows API to retrieve the connected state of the local system; =TRUE if there is an
' Internet connection, or FALSE otherwise.
'---------------------------------------------------------------------------------------
Private Declare Function InternetGetConnectedState Lib "wininet" _
                (ByRef dwFlags As Long, ByVal dwReserved As Long) As Long

I don't remember why I didn't use the 'Ex' version, but can you try the non-Ex version and see if you get the same results?

Also, be sure that the connection constants you're using are correct, e.g.:

'---------------------------------------------------------------------------------------
'Local system uses a modem to connect to the Internet.
Private Const INTERNET_CONNECTION_MODEM As Long = &H1

'Local system uses a LAN to connect to the Internet.
Private Const INTERNET_CONNECTION_LAN As Long = &H2

Again, hope that helps.
0
 
leoprexCommented:
Tried also the version without 'Ex' with no results. I guess that the problem is with the special kind of LAN connection that I have. In the basic configuration I do not have a public IP adress. But if I pay I can get (for istance for games on the net) : tried with no results. Once I have got a public IP adress I can enter (through Cisco) on the Intranet of my company through a VPN connection. Made also. Always same result. I have bought an Epson printer: the installation program tries to connect to the Internet in order to make a registration. But on my computer this does not work: the existing and active LAN connection is not found by the program. Maybe this program uses the same 'defective' API call.
It is almost impossible to obtain technical clarifications from my Internet provider.
I have lost almost all hopes....
Ciao
Leonardo
0
 
leoprexCommented:
I correct myself: the problem is not with some special property of my LAN connection. I have tested my VB program on the same LAN on another laptop using XP and it works! So is the problem with W2k? with some malfunction of my laptop? with the PCMCIA net card?
0
 
leoprexCommented:
A final comment.
I have found many articles on the net about InternetGetConnectedState being not realiable. A safer test of an existing active Internet connection can be made by pinging on a Ip address of a host presumably on. The code could be the foolwing:
-------------------------------------------------------------------------------
Private Declare Function GetRTTAndHopCount Lib "iphlpapi.dll" _
        (ByVal lDestIPAddr As Long, _
         ByRef lHopCount As Long, _
         ByVal lMaxHops As Long, _
         ByRef lRTT As Long) As Long
Private Declare Function inet_addr Lib "wsock32.dll" _
        (ByVal cp As String) As Long
Public Function SimplePing(sIPadr As String) As Boolean
    ' Based on an article on 'Codeguru' by 'Bill Nolde'
    ' Thx to this guy! It 's simple and great!
    ' Implemented for VB in November 2002 by G. Wirth, Ulm,  Germany
    ' Enjoy!
    Dim lIPadr      As Long
    Dim lHopsCount  As Long
    Dim lRTT        As Long
    Dim lMaxHops    As Long
    Dim lResult     As Long
    Const SUCCESS = 1
    lMaxHops = 20               ' should be enough ...
    lIPadr = inet_addr(sIPadr)
    SimplePing = (GetRTTAndHopCount(lIPadr, lHopsCount, lMaxHops, lRTT) = SUCCESS)
End Function
-------------------------------------------------------------------------------------
This code senses an existing Internet Connection also on my laptop!
0
 
halfondjAuthor Commented:
I forgot where I read it, but it was said that pinging an existing active Internet connection is unreliable too.  With all the research that I've done, the InternetGetConnectState is still the best way to go.  I haven't had any problems with it yet.
0
 
s_federiciCommented:
Hi halfondj,
I have the same problem (win2k, LAN connection with shared internet connection). My InternetGetConnectedState return FALSE. What I do not understand from this LONG discussion is if in the end you found a working API to check if there is an active Internet Connection or not.

Thanks
Stefano
0
 
halfondjAuthor Commented:
Stefano,

I'm using the InternetGetConnectedState API and it's working fine.  The problem you are experiencing may be related to the shared internet connection.

Have you tried your code on a PC that doesn't have a share internet connection?

Regards,
  halfondj
0
 
s_federiciCommented:
My code used to work when I was using it connected with a modem. But I would like to generalize it to be able to detect an internet connection whatever the user connection be.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now