|
-
Feb 24th, 2000, 10:37 PM
#1
Thread Starter
Fanatic Member
Hi.
How can I tell if a PC is currently connected to the Internet through VB?
Thanks.
-
Feb 25th, 2000, 12:00 AM
#2
Junior Member
Here's the code to check.......
Put this code in your form
Public Function IsConnected() As Boolean
Dim TRasCon(255) As RASCONN95
Dim lg As Long
Dim lpcon As Long
Dim RetVal As Long
Dim Tstatus As RASCONNSTATUS95
'
TRasCon(0).dwSize = 412
lg = 256 * TRasCon(0).dwSize
'
RetVal = RasEnumConnections(TRasCon(0), lg, lpcon)
If RetVal <> 0 Then
MsgBox "ERROR"
Exit Function
End If
'
Tstatus.dwSize = 160
RetVal = RasGetConnectStatus(TRasCon(0).hRasCon, Tstatus)
If Tstatus.RasConnState = &H2000 Then
IsConnected = True
Else
IsConnected = False
End If
End Function
Private Sub Form_Load()
IsConnected
MsgBox IsConnected
End Sub
And put this code in a module
Option Explicit
Public Declare Function RasEnumConnections Lib "RasApi32.dll" Alias "RasEnumConnectionsA" (lpRasCon As Any, lpcb As Long, lpcConnections As Long) As Long
Public Declare Function RasGetConnectStatus Lib "RasApi32.dll" Alias "RasGetConnectStatusA" (ByVal hRasCon As Long, lpStatus As Any) As Long
'
Public Const RAS95_MaxEntryName = 256
Public Const RAS95_MaxDeviceType = 16
Public Const RAS95_MaxDeviceName = 32
'
Public Type RASCONN95
dwSize As Long
hRasCon As Long
szEntryName(RAS95_MaxEntryName) As Byte
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
'
Public Type RASCONNSTATUS95
dwSize As Long
RasConnState As Long
dwError As Long
szDeviceType(RAS95_MaxDeviceType) As Byte
szDeviceName(RAS95_MaxDeviceName) As Byte
End Type
Regards,
Hutchie
-
Feb 25th, 2000, 10:19 PM
#3
Thread Starter
Fanatic Member
Thanks Hutchie!
Hutchie,
It works like a charm! 
Thanks again.
Edited by OneSource on 02-26-2000 at 10:21 AM
-
Feb 26th, 2000, 01:49 PM
#4
Addicted Member
Is This a Ping
Hutchie does this also act like a ping program
-
Feb 26th, 2000, 04:08 PM
#5
New Member
One can check if one is connected without using ras, like this:
Module:
Public Const ERROR_SUCCESS = 0&
Public Const APINULL = 0&
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public ReturnCode As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal _
hKey As Long) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias _
"RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As _
String, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias _
"RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal lpReserved As Long, lpType As Long, _
lpData As Any, lpcbData As Long) As Long
Form code:
Public Function IsConnected() As Boolean
Dim hKey As Long
Dim lpSubKey As String
Dim phkResult As Long
Dim lpValueName As String
Dim lpReserved As Long
Dim lpType As Long
Dim lpData As Long
Dim lpcbData As Long
IsConnected = False
lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
ReturnCode = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, _
phkResult)
If ReturnCode = ERROR_SUCCESS Then
hKey = phkResult
lpValueName = "Remote Connection"
lpReserved = APINULL
lpType = APINULL
lpData = APINULL
lpcbData = APINULL
ReturnCode = RegQueryValueEx(hKey, lpValueName, _
lpReserved, lpType, ByVal lpData, lpcbData)
lpcbData = Len(lpData)
ReturnCode = RegQueryValueEx(hKey, lpValueName, _
lpReserved, lpType, lpData, lpcbData)
If ReturnCode = ERROR_SUCCESS Then
If lpData = 0 Then
IsConnected = False
Else
IsConnected = True
End If
End If
RegCloseKey (hKey)
End If
end function
-
Feb 27th, 2000, 03:41 AM
#6
Thread Starter
Fanatic Member
More than 1 way to skin a cat.
Arsi Rantala,
Thanks for your reply.
I tried out your code and it worked also. Your code and Hutchie's contain almost exactly the same number of lines. I can't see where one is more efficient or reliable than the other. Therefore, I'll keep both examples.
Is there any particular reason why your method is better than using ras as Hutchie illustrated?
Thanks.
-
Feb 27th, 2000, 03:58 AM
#7
New Member
Which one to use...
Arsi Rantala,
Thanks for your reply.
I tried out your code and it worked also. Your code and Hutchie's contain almost exactly the same number of lines. I can't see where one is more efficient or reliable than the other. Therefore, I'll keep both examples.
Is there any particular reason why your method is better than using ras as Hutchie illustrated?
Thanks.
Well I have had trouble doing ras stuff in nt, so my guess is that my code would work better in nt than that other code. can't be sure, haven't tested that - yet. (gives errors about rasapi32.dll - if one - like me - hasn't installed ras to nt).
-
Feb 27th, 2000, 05:47 AM
#8
Junior Member
Hi One Source / Arsi,
I've not had any trouble in using this code in NT or Win 9.X, I must admit though it is preference.
Uou are perfectly right in what your saying though, I just find this more effective using Ras if you know it's there.
ScottF - Sorry on the late reply, no this can't be used as a ping program.
Hutchie.
-
Feb 27th, 2000, 07:17 AM
#9
Thread Starter
Fanatic Member
Thanks for the info, guys.
-
Feb 28th, 2000, 06:05 AM
#10
Lively Member
Hmm All good but. . .
Hutchie, I'm getting a Ambiguous Name error in your code. . . the line that's triggering it is
Dim TRasCon(255) As RASCONN95
Mind you, I'm trying to pull this off in VBA/Access.
In addition, will your code work if not using Dial-Up Networking? I know for a fact that Arsi's won't.
I'm looking for a "universal" test for internet connectivity, not relying upon Dial-Up.
-
Feb 28th, 2000, 06:13 AM
#11
Junior Member
John,
This is only available for a Dial up connection, sorry!
If I get time over the next couple of days I'll see if there is any way of detecting any "connection"
Hutchie
-
Feb 28th, 2000, 06:16 AM
#12
Lively Member
Theoretically
Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Long, ByVal lpszConnectionName As String, ByVal dwNameLen As Long, ByVal dwReserved As Long) As Long
THat function should work.
But I don't know that much about VB API calls (read as: almost nothing) so I don't know what I should be shoving into the parameters.
Any input would be spectacular.
-
Feb 28th, 2000, 06:54 AM
#13
Junior Member
John / All source,
I've been messing about with your aPi John.
Here's both via Lan and via modem connection, hope this helps you out now !!!
Add this code to a module :
Option Explicit
Public Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef lpSFlags As Long, _
ByVal dwReserved As Long) As Long
Public Const INTERNET_CONNECTION_LAN As Long = &H2
Public Const INTERNET_CONNECTION_MODEM As Long = &H1
Public Function ViaLAN() As Boolean
Dim SFlags As Long
'return the flags associated with the connection
Call InternetGetConnectedState(SFlags, 0&)
'True if the Sflags has a LAN connection
ViaLAN = SFlags And INTERNET_CONNECTION_LAN
End Function
Public Function ViaModem() As Boolean
Dim SFlags As Long
'return the flags associated with the connection
Call InternetGetConnectedState(SFlags, 0&)
'True if the Sflags has a modem connection
ViaModem = SFlags And INTERNET_CONNECTION_MODEM
End Function
Add this code to a form with 1 command button andd two text boxes. It will return "True" for which ever one you are connected to.
Option Explicit
Private Sub Command1_Click()
Text1 = ViaLAN()
Text2 = ViaModem()
End Sub
Hutchie
-
Feb 28th, 2000, 07:01 AM
#14
Addicted Member
Ping Code
Hutchie do you know of a ping code I can learn off of.
-
Feb 28th, 2000, 07:05 AM
#15
Thread Starter
Fanatic Member
Wrong Name ... but
Hutchie,
The name's OneSource. But I don't mind the mixup. I hadn't considered that the code that you and Arsi posted before was only for a dial-up connection. If the code you just put up also covers LAN's, that definetly makes my app better!
Thanks.
-
Feb 28th, 2000, 07:37 AM
#16
Thread Starter
Fanatic Member
Sorry fellas but ....
Hutchie,
I tried out your code. It detected if I was using a LAN or dial-up (modem) connection, but the problem is it returns the same results whether I am connected to the Internet or not - via Modem - yes and via LAN - no.
So, this brings us back to John's question:
... will your code work if not using Dial-Up Networking?
If my understanding is correct, we still will not be able to determine if someone is connected via LAN because we can't use ras or Arsi's code. We will only be able to detect what kind of connection that person has with your last post Hutchie.
Please correct me if I'm wrong.
Thanks.
Edited by OneSource on 02-28-2000 at 07:42 PM
-
Feb 28th, 2000, 04:45 PM
#17
Junior Member
OneSource,
I've only checked that this works via 95/98 but not yet NT.
I've now been able to check that this will work if you are connected via a LAN. IT DOES !. Lan TRUE modem FALSE
I have than dialed up via my modem and it changes to Modem TRUE, LAN FALSE.
Are you connected via Lan by Proxy ?, I think there may some other constant for this.
Hutchie.
-
Feb 28th, 2000, 11:40 PM
#18
Thread Starter
Fanatic Member
Hutchie,
I only have access to a Windows '95 dial-up connection.
In order to see what I'm talking about, test your code using a dial-up connection when you are not connected to the Internet. The results that I got were the same whether I was connected or not: Modem - yes and LAN - no.
Are we to assume that if someone has a LAN connection they are ALWAYS connected to the Internet? 
If so, the problem is solved. If not, your code will only tell what type of connection exists - LAN or dial-up - not whether a PC with a LAN connection is on-line or not.
I hope you understand what I'm trying to convey.
Thanks for your help on this.
-
Feb 29th, 2000, 12:22 AM
#19
Lively Member
Hmmm this could be alleviated
If we make the assumption that LAN access = Internet Access then we can use the following:
A) Run Hutchie's code to determine LAN vs Modem.
If LAN, assume connection exists.
If Modem, run additional code to determine if Dial-Up Networking is active.
That would do it; only problem is LAN <> Internet Access.
I was sent some code by a member here that may get around this via pinging a known website; it's a slower method but it is pretty certain to work. Unfortunately, this one is browser specific.
You just can't win ;P
Code:
'Declares for direct ping
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Dim checkType As Integer
Dim remMsg(2) As String
'End of Declarations, begin function
Private Sub CheckConnection3()
Dim sTmp As String
Dim hInet As Long
Dim hUrl As Long
Dim Flags As Long
Dim url As Variant
hInet = InternetOpen(App.Title, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0&)
sTmp = Me.Caption
Me.Caption = "Checking connection with www.yahoo.com..."
If hInet Then
Flags = INTERNET_FLAG_KEEP_CONNECTION Or INTERNET_FLAG_NO_CACHE_WRITE Or INTERNET_FLAG_RELOAD
hUrl = InternetOpenUrl(hInet, "http://www.yahoo.com", vbNullString, 0, Flags, 0)
If hUrl Then
MsgBox "Your computer is connected to Internet", vbInformation, "Checing connection"
Call InternetCloseHandle(hUrl)
Else
MsgBox "Your computer is not connected to Internet", vbInformation, "Checing connection"
End If
End If
Call InternetCloseHandle(hInet)
Me.Caption = sTmp
End Sub
This is from Arkadiy Olovyannikov
I can't remember his handle, but much credit goes to him.
P.S. His code had 3 parts to it, so I grabbed what looked like all the Direct Internet stuff.
I'd be happy to forward any of you the project he sent me... just email me.
-
Feb 29th, 2000, 01:35 AM
#20
Junior Member
OK.
After getting so much greif I've managed to do ALL of what you require. My former code will work by adding this to the module:
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
Public Function Online() As Boolean
'If your online it will return true, if not as you guessed False
Online = InternetGetConnectedState(0&, 0&)
End Function
Add another text box to your form and paste this line into the command button again
Text3 = Online()
This should now work and check whether the connection is active aswell.
I have checked this both online and off with modem and my LAN and it works fine.
Hope this helps, without the use of pinging !!!
Kind regards,
Hutchie
-
Feb 29th, 2000, 07:49 AM
#21
Thread Starter
Fanatic Member
Yes!
Hutchie,
It definitely wasn't my intention to give you any grief! I really appreciate all you did to help me (and John) out!
Now the code works regardless if the connection is LAN or dial-up! Remember, the type of connection (dial-up or LAN) was never the main issue. I refer you to my original post that started all this:
How can I tell if a PC is currently connected to the Internet through VB?
To review, we got into this LAN/dial-up connection business because the solutions that you and Arsi provided to my initial post worked for dial-up's, but not for LAN's.
This is the code that I ended up using:
Code:
Option Explicit
Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
Public Function Online() As Boolean
'If you are online it will return True, otherwise False
Online = InternetGetConnectedState(0&, 0&)
End Function
.
In addition to being valid for a LAN connection, it is much shorter than the solutions that you and Arsi posted to my original query. It also avoids using the 2 R's - RAS and the Registry. 
Another positive development from all this is that ScottF got his ping code from John's post that pinged Yahoo.
So, in this case John, it seems like we did win!
Thanks again Hutchie and to all others who contributed to this thread! 
Edited by OneSource on 03-02-2000 at 09:18 AM
-
Mar 1st, 2000, 08:51 PM
#22
Lively Member
Well, we got tons done.
:)
Code:
Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
Public Function Online() As Boolean
'If you are online it will return True, otherwise False
Online = InternetGetConnectedState(0& ,0& ;)
End Function
Online = InternetGetConnectedState(0& ,0& ;)
that line gives me a syntax error. What am I missing?
-
Mar 1st, 2000, 09:04 PM
#23
hehe... you had a typing error:
Code:
Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
Public Function Online() As Boolean
'If you are online it will return True, otherwise False
Online = InternetGetConnectedState(0& ,0&)
End Function
taLON
Edited by taLON on 03-02-2000 at 09:06 AM
-
Mar 1st, 2000, 09:15 PM
#24
Thread Starter
Fanatic Member
John,
Put this line of code into your project:
Online = InternetGetConnectedState(0&, 0& )
The problem here is being caused because UBB's formatting is converting the right part of the statement into a smiley face: 0&). If you copy and paste the line above into your project as it is, it will work.
All the best.
-
Mar 1st, 2000, 09:25 PM
#25
Lively Member
Danke
That'll do it
-
May 23rd, 2000, 03:24 AM
#26
New Member
Problem with using the Registry to track active connections
I am using Win 2000 and have found that the Registry method of tracking active connections doesn't work. It appears that win2k doesn't make that entry in the registry. However, I have use the API InternetGetConnectedStateEx and have found that it works.
I am now faced with a new issue. I need to find a way of reading all the Dial UP Networking connection available on the machine. Anyone have any bright ideas?
-
Aug 31st, 2000, 12:08 PM
#27
Hyperactive Member
I tried to read out most of this long thread and wasn't able to find out what's not working here. All I want to know is if the current computer is connected to the net, here's what I got:
Private Declare Function InternetAutodial Lib "wininet.dll" (ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Const INTERNET_AUTODIAL_FORCE_ONLINE = 1
Private Const INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
Private Declare Function InternetAutodialHangup Lib "wininet.dll" (ByVal dwReserved As Long) As Long
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpSFlags As Long, ByVal dwReserved As Long) As Long
Whenever I try this following line of code:
MsgBox InternetGetConnectedState(0&, 0&)
It will return '1', always. Why is that?
-
May 14th, 2003, 04:50 PM
#28
Member
Have you tried this with your computer connected to a LAN then checking the return value when you unplug the network cable??? When I did it, it still returned a TRUE.
Thanks.
Originally posted by Hutchie
[B]OK.
After getting so much greif I've managed to do ALL of what you require. My former code will work by adding this to the module:
Public Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Public Const INTERNET_CONNECTION_CONFIGURED As Long = &H40
Public Function Online() As Boolean
'If your online it will return true, if not as you guessed False
Online = InternetGetConnectedState(0&, 0&)
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|