Solved

VB5 Connection status

Posted on 1998-09-15
5
169 Views
Last Modified: 2013-11-13
Hopefully someone knows this and will answer even though the point offering is low (I'm practically out at the moment).
How do you make your program recodnize when windows is connected to the net so it can auto launch from the task bar and remain dormant if your system is not connected?
0
Comment
Question by:ChrisK
  • 3
5 Comments
 
LVL 2

Author Comment

by:ChrisK
ID: 1435172
actually slight correction, it'll be in the system tray and launch from there.  Just like ICQ or AIM for example.
0
 
LVL 14

Accepted Solution

by:
waty earned 20 total points
ID: 1435173
Here is the function you need

' *** Used to know all active connections
Private Const ERROR_SUCCESS = 0&
Private Const APINULL = 0&
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private 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


Public Function ActiveConnection() As Boolean
   ' *** How can I detect if there is an active internet connection?
   ' *** For programs that rely on connecting to the internet,
   ' *** it is very useful to know whether or not the computer has an active connection.
   ' *** Whenever Windows logs on to a dial-up connection, it changes a value in the registry.

   'Here is an example of how to use the ActiveConnection function.
   '
   'If ActiveConnection = True Then
   '    Call MsgBox("You have an active connection.", vbInformation)
   'Else
   '    Call MsgBox("You have no active connections.", vbInformation)
   'End If

   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
   Dim nRet             As Long

   ActiveConnection = False
   lpSubKey = "System\CurrentControlSet\Services\RemoteAccess"
   nRet = RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, phkResult)

   If nRet = ERROR_SUCCESS Then
      hKey = phkResult
      lpValueName = "Remote Connection"
      lpReserved = APINULL
      lpType = APINULL
      lpData = APINULL
      lpcbData = APINULL
      nRet = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, ByVal lpData, lpcbData)
      lpcbData = Len(lpData)
      nRet = RegQueryValueEx(hKey, lpValueName, lpReserved, lpType, lpData, lpcbData)

      If nRet = ERROR_SUCCESS Then
         If lpData = 0 Then
            ActiveConnection = False
         Else
            ActiveConnection = True
         End If
      End If

      RegCloseKey (hKey)
   End If

End Function

0
 
LVL 2

Author Comment

by:ChrisK
ID: 1435174
excellent and thanks....I'll pay ya back some how, starting with an A of course :)
0
 

Expert Comment

by:bennys
ID: 1435175
an easyer way to do this will be to use the internet transfer control and try to connect to your application server (or any other server you want to work with.)
when you get a response , you are connected.
you will want to use error trapping and not display a message if you find out you're not connected.
with this control you can even set the timeout period for you connection trials.
0
 
LVL 2

Author Comment

by:ChrisK
ID: 1435176
That control isn't that great from what I've seen, and constantly sending out winsock requests to check the status and using error trapping is taxing on the system resources where as the code provided by waty simply grabs the connect value from the registry which uses practically no resources and doesn't require a winsock control to even be loaded...plus it's API so it's very quick.  But thanks for the input.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

21 Experts available now in Live!

Get 1:1 Help Now