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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richie_SimonettiIT OperationsCommented:
Richie_SimonettiIT OperationsCommented:
some other examples:

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
    If InternetCheckConnection("", FLAG_ICC_FORCE_CONNECTION, 0&) = 0 Then
        MsgBox "Connection to failed!", vbInformation
        MsgBox "Connection to succeeded!", vbInformation
    End If
End Sub
Richie_SimonettiIT OperationsCommented:
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 2001
    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.
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:

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!!
Richie_SimonettiIT OperationsCommented:
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?
halfondjAuthor Commented:
The InternetGetConnectedState.htm from 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.
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.
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?
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.
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

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.

'Local system uses a LAN to connect to the Internet.

Again, hope that helps.
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....
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?
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!
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.
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.

halfondjAuthor Commented:

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?

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.