Solved

Detecting LAN connection using InternetGetConnectedState API doesn't work

Posted on 2003-11-28
19
5,872 Views
Last Modified: 2007-12-19
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
Comment
Question by:halfondj
  • 8
  • 5
  • 4
  • +1
19 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839307
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839310
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
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 500 total points
ID: 9839313
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
 

Author Comment

by:halfondj
ID: 9839342
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
 

Author Comment

by:halfondj
ID: 9839453
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9839529
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
 

Author Comment

by:halfondj
ID: 9839554
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
 

Author Comment

by:halfondj
ID: 9859479
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
 

Expert Comment

by:leoprex
ID: 11083815
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:halfondj
ID: 11089903
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
 

Expert Comment

by:leoprex
ID: 11092728
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
 

Author Comment

by:halfondj
ID: 11092870
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
 

Expert Comment

by:leoprex
ID: 11112477
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
 

Expert Comment

by:leoprex
ID: 11138421
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
 

Expert Comment

by:leoprex
ID: 11138955
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
 

Author Comment

by:halfondj
ID: 11138999
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
 

Expert Comment

by:s_federici
ID: 11604165
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
 

Author Comment

by:halfondj
ID: 11608533
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
 

Expert Comment

by:s_federici
ID: 11610010
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now