Solved

Losing network connection

Posted on 2001-07-12
8
302 Views
Last Modified: 2010-05-18
I have a vb application that I need to determine a way to verify a network connection still exist.  I need to do this because if the network goes down, the application still needs to be able to run.

The only way I found to do this was to use WSOCK32.dll.  In the app, I have created a timer.  Everytime this timer triggers, I check the connection using WSOCK32.DLL.  This works fine when there is a connection.  The problem is when the network cable is unplugged (or there is not a network connection) this process takes at least two minutes to check and it still doesn't return the value I expect.  The code I am using is as follows:

In a module:
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, lpWSAData As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal hostname$) As Long

'****************************
'*  Constant Declarations        *
'****************************
Private Const WS_VERSION_REQD = &H101
Private Const WS_VERSION_MAJOR = WS_VERSION_REQD \ &H100 And &HFF
Private Const WS_VERSION_MINOR = WS_VERSION_REQD And &HFF&
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADescription_Len = 256
Private Const WSASYS_Status_Len = 128

'****************************
'*  Type Declarations        *
'****************************
Private Type HOSTENT
   hName As Long
   hAliases As Long
   hAddrType As Integer
   hLength As Integer
   hAddrList As Long
End Type

Private Type WSADATA
   wversion As Integer
   sHighVersion As Integer
   szDescription(0 To WSADescription_Len) As Byte
   szSystemStatus(0 To WSASYS_Status_Len) As Byte
   iMaxSockets As Integer
   iMaxUdpDg As Integer
   ipszVendorInfo As Long
End Type

'*** Call this routine at startup
Public Sub InitializeWinsockAPI()
    On Error Resume Next
    Dim lStartWinsock As Long
    Dim WSad As WSADATA
    lStartWinsock = WSAStartup(WS_VERSION_REQD, WSad)
End Sub

'***Call this routine on exit
Public Sub CleanupWinsockAPI()
    On Error Resume Next
    Dim lWinSockCleanup As Long
    lWinSockCleanup = WSACleanup()
End Sub

Public Function GetHostIP(sPCName As String) As String
   Dim hostent_addr As Long
   Dim lStartWinsock As Long
   Dim WSad As WSADATA
   Dim lWinSockCleanup As Long
   
   lStartWinsock = WSAStartup(WS_VERSION_REQD, WSad)
   
   hostent_addr = gethostbyname(Trim$(sPCName))
   
   If hostent_addr = 0 Then
      GetHostIP = False
   Else
      GetHostIP = True
   End If
   
   lWinSockCleanup = WSACleanup()
End Function


In my form:

If GetHostIP(gNetwork) Then
   AS400.Active = True
Else
   AS400.Active = False
   Exit Sub
End If

If I am running the application and unplug the network cable, I expect the value of the following code to be 0 and it isn't:
If hostent_addr = 0 Then
      GetHostIP = False
   Else
      GetHostIP = True
   End If

If I start the application when I am not connected to the network, the value of hostent_addr is 0.  I am not sure why this is.  

My biggest issue, though, is the length of time it takes to perform this check when I don't have a network connection.  I would like to know if anyone has a suggestion of a better way to check for a network connection or if there is any way I could speed up my current process.  

Thanks in advance for the help.
   
 
0
Comment
Question by:DebeG
8 Comments
 
LVL 11

Expert Comment

by:Otana
ID: 6277782
there's an easy ping function that uses a few API calls. Unfortunately, I don't have it here. If you don't have an answer in say, three hours, I'll try to post it.
0
 
LVL 20

Accepted Solution

by:
hes earned 100 total points
ID: 6277879
Declare this

Private Declare Function IsNetworkAlive Lib "Sensapi" _
  (lpdwFlags As Long) As Long

To check the connection

Dim tmp As Long
Dim Status As Boolean
   
Status = IsNetworkAlive(tmp)
Returns True if Network Connection is active
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278011
Sensapi?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278018
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6278021
0
 
LVL 20

Expert Comment

by:hes
ID: 6278035
Yep Sensapi.dll
0
 

Author Comment

by:DebeG
ID: 6280588
Thank you.  the "Sensapi" is what I was looking for, although I did not use IsNetworkAlive.  I wanted to check a specific IP address so here is what I used:

Public Declare Function IsDestinationReachable Lib "Sensapi.dll" _
   Alias "IsDestinationReachableA" _
  (ByVal lpszDestination As String, _
   lpQOCInfo As QOCINFO) As Long

The other posting gave an example of basically what my code stated I was doing by using the wsock.dll.  That was not acceptable because it took too long when there was not a network connection.  

The sensapi.dll works much faster.  Thanks again!
0
 
LVL 20

Expert Comment

by:hes
ID: 6280664
Glad to have helped
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 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

12 Experts available now in Live!

Get 1:1 Help Now