Link to home
Start Free TrialLog in
Avatar of halfondj
halfondj

asked on

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
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

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
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of halfondj
halfondj

ASKER

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.
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:  https://www.experts-exchange.com/questions/20001419/a-few-general-pointers-reqd.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!!
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
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.
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?
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

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

Thanks
Stefano
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
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.