Use VB6 to ping a remote computer

Posted On 2017-05-19 by VB6Boy
Tags: VB6 Miscellaneous Tutorial Networking 
Views: 585

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

About the Author

has posted a total of 101 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links