Hi.
How can I tell if a PC is currently connected to the Internet through VB?
Thanks.
Printable View
Hi.
How can I tell if a PC is currently connected to the Internet through VB?
Thanks.
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
Hutchie,
It works like a charm! :D
Thanks again.
Edited by OneSource on 02-26-2000 at 10:21 AM
Hutchie does this also act like a ping program
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
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).Quote:
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.
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.
Thanks for the info, guys.
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.
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
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.
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
Hutchie do you know of a ping code I can learn off of.
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.
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:
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.Quote:
... will your code work if not using Dial-Up Networking?
Please correct me if I'm wrong.:(
Thanks.
Edited by OneSource on 02-28-2000 at 07:42 PM
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.
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? :confused:
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.
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
This is from Arkadiy OlovyannikovCode:'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
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.
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
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! :D 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:
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.Quote:
How can I tell if a PC is currently connected to the Internet through VB?
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!Quote:
You just can't win ;P
Thanks again Hutchie and to all others who contributed to this thread! :D
Edited by OneSource on 03-02-2000 at 09:18 AM
Well, we got tons done.
:)
Online = InternetGetConnectedState(0& ,0& ;)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
that line gives me a syntax error. What am I missing?
hehe... you had a typing error:
taLONCode: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
Edited by taLON on 03-02-2000 at 09:06 AM
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.
That'll do it :)
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?
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?
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.
Quote:
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