Solved

VB5 Connection status

Posted on 1998-09-15
5
171 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

813 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