Use VB6 to ping a remote computer

By | 2017-05-19

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")

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.