Search Tools Links Login

Use VB6 to ping a remote computer


When your application makes connections to remote computers, it is usually a good idea to check to see if the remote machine is up and responding prior to attempting a connection to it. Pinging a machine is one way to do it.

Visual Basic 6 doesn't have a built in method to perform an ICMP echo request (ie, Ping). But thankfully, we can leverage the Windows API to make it happen. The following function takes an IP address as input, then returns a boolean success/fail depending on the results of the ping.

Option Explicit

Private Declare Function GetRTTAndHopCount Lib "iphlpapi.dll" _
    (ByVal lDestIPAddr As Long, _
    ByRef lHopCount As Long, _
    ByVal lMaxHops As Long, _
    ByRef lRTT As Long) As Long
         
Private Declare Function inet_addr Lib "wsock32.dll" _
    (ByVal cp As String) As Long

Public Function SimplePing(sIPadr As String) As Boolean

    Dim lIPadr      As Long
    Dim lHopsCount  As Long
    Dim lRTT        As Long
    Dim lMaxHops    As Long
    Dim lResult     As Long
        
    Const SUCCESS = 1
        
    lMaxHops = 20
    lIPadr = inet_addr(sIPadr)
    SimplePing = (GetRTTAndHopCount(lIPadr, lHopsCount, lMaxHops, lRTT) = SUCCESS)
    
End Function

Sample usage:

ret = SimplePing("192.168.1.100")

About this post

Posted: 2017-05-19
By: vb6boy
Viewed: 6,579 times

Categories

Tutorials

Networking

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.