Subscribe to Windows IT Pro
November 01, 1997 12:00 AM

Building an Automated Mass Ping Utility

Windows IT Pro
InstantDoc ID #562
Rating: (1)
Downloads
562.zip

At C in Listing 1, you can see that the subroutine calls the ping function within the loop. Before calling the ping function, the subroutine uses VB's Space function to make sure that the sIP string variable is adequately sized to contain the returned IP address. The returned IP address has the form 999.999.999.999, and it can contain a maximum of 16 bytes (15 bytes plus the null character). Like any string variable that an external DLL returns, the return variable must be the right size. If it is too small, the application generates a memory violation error.

Next, the ping function performs the task of pinging the remote system. The first parameter of the ping function is the List_IP.List(i) variable, which contains the text from the current list entry. The second parameter, sIP, will contain the returned IP address. The third parameter specifies the size of the data buffer that will be sent to (and returned by) the remote system. This example uses a data buffer size of 64, which means that the utility will ping 64 bytes between the local and remote systems. The fourth parameter, lPingTime, will contain the time in milliseconds that it took ping32.dll to complete the ping function. The fifth parameter sets the ping timeout value to 2000 milliseconds (2 seconds). Finally, the code sets the sixth parameter to 0, which specifies that the ping function will run in silent mode and will not display any message boxes.

After the ping function completes, the subroutine adds the values from the returned variables (sIP and lPingTime) to the list. Because the sIP variable returns as a C-style null terminated string, the code at D in Listing 1 strips the non-displayable null character, Chr$(0), from the string before adding the string to the List_PingStatus list. If the ping function fails, the subroutine checks the Beep setting and optionally beeps the user. The program displays a failed message and shows the return code as an entry in the ping status list. After updating the list, List_PingStatus.ListIndex positions the list entry to ensure that the current ping status entry is always visible.

Timing Is Everything
Now that we've analyzed the Ping_List subroutine, which pings the remote TCP/IP hosts, you might want to know how Automated Mass Ping executes that code at regular intervals. VB's built-in timer function makes this functionality extremely easy to implement. When the user clicks Start Ping on the Ping Status tab, the utility executes the Command_StartPing_Click subroutine, shown in Listing 2.

As you can see in Listing 2, the first thing Command_StartPing_Click does is call the Ping_List subroutine, which immediately initiates the first ping process. Next, Command_StartPing_Click sets the Timer1.Interval property to the interval value from the Settings tab, and sets the Timer1.Enabled property to True to activate the timer. The timer will fire at the interval specified in the Timer1.Interval property. Finally, the subroutine disables the Start Ping button, which prevents the user from starting another timer process, and enables the Stop Ping button.

When the timer interval has elapsed, the timer control then fires the Timer1_
Timer subroutine, which you see in Listing 3, page 202. The timed execution of Timer1_Timer runs the ping process at regular intervals. This subroutine is simple: All it does is call the Ping_List subroutine.

Saving and Retrieving Settings
The final important part of the Automated Mass Ping utility handles saving and retrieving configuration settings. The utility saves the settings when the user clicks Save Settings on the Settings tab; clicking Save Settings executes the Command_Save_Click subroutine, which you see in Listing 4.

Automated Mass Ping uses VB's built-in SaveSetting function three times to write the setting values to the Registry. The SaveSetting function makes the Registry emulate an INI file. The SaveSetting function is easy to use, but it saves the settings in only the HKEY_CURRENT_USER\Software\VB and VBA Program Settings key.

The first, second, and third parameters of the SaveSetting function uniquely identify the Registry entry, and the fourth parameter contains the value to be saved. The first parameter specifies the application's name; for Automated Mass Ping, I used the string "Automated Ping". The next parameter specifies the section or subkey of the Registry that will be used. For this parameter, I specified "Startup" to denote that the values will be used when the application starts. The third parameter specifies a unique setting for the application. Each SaveSetting statement in Listing 4 uses a different key name for the third parameter: "Interval", "Beep", and "List". As you might guess, the Interval key stores the timer interval from the Settings tab. The Beep key stores the value from the Beep on Ping check box, and the List key stores the list of systems entered on the Settings tab. The List2String function extracts the entries from the list of systems (List_IP) and places them in the sList string. A comma separates each TCP/IP host entry in the sList string.

When the Automated Mass Ping utility first starts, the Form_Load subroutine (shown in Listing 5) retrieves any previously saved Registry settings. As you can see in Listing 5, the Form_Load subroutine uses VB's GetSetting function to retrieve the values from the Registry. GetSetting works very much like SaveSetting. The first three parameters of the GetSetting function identify the key to be retrieved; the optional fourth parameter supplies a default value that's used if the key cannot be retrieved. The GetSetting function returns the retrieved Registry value to the variable that's on the left side of the assignment operator (=). The String2List function in Listing 5 extracts each TCP/IP host name from the comma-delimited string containing the list of systems to be pinged, and adds the host name to the List_IP list.

Putting the Utility to Work
You can use the Automated Mass Ping utility to provide a continuously updated report on the availability of a list of TCP/IP-attached systems. These systems can connect across the Internet, or they can connect across your private LAN or WAN network. Although Automated Mass Ping simply displays the status of the last ping attempt, you can easily modify this utility to perform other actions, such as sending a network message to notify you when an important system goes down or recording the ping results in a database file to track the availability of your systems. In next month's VB Solutions column, I'll explore how to build a graphical HOSTS file editing utility that puts Automated Mass Ping to work.

We Want Your VB Code!
Windows NT Magazine wants to publish your VB solutions. Send us any interesting and useful VB solutions you've created for your business's problems. If we agree that your VB solutions are valuable to our readers, we'll publish your code and pay you $100. You can send contributions or ideas for VB solutions to me at mikeo@teca.com.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Mar 09, 2005

    Give me

  • Ahmed Fraz Mamoon
    8 years ago
    Mar 10, 2004

    Hi there, I really wanna appreciate the writer for writing such a useful code. I actually want the same thing being a network adming of a big network. And as have also done Graduation in Computer Sciences my boss wants me to write the code for such an application. And u know u have saved much of my time. Michael Otey Thank you very much.

    Regards,

  • John Mishefske
    9 years ago
    Nov 26, 2003

    There is a small error in the code behind the form when a ping fails -
    the printing of the IP address in the List_PingStatus box doesn't get
    formatted correctly. In the Ping_List procedure:

    iPingResults = ping(List_IP.List(i), sIP, 64, lPingTime, 2000, 0)
    If iPingResults = 0 Then
    iTemp = InStr(sIP, Chr$(0))
    List_PingStatus.AddItem " Reply from: " & Mid$(sIP, 1, iTemp - 1) & _
    " took: " & Str(lPingTime) & " ms"
    Else
    iTemp = InStr(sIP, Chr$(0)) ' <---- ADD THIS LINE **************
    'If SSCheck_Beep.Value = True Then Beep
    List_PingStatus.AddItem " Reply from: " & Mid$(sIP, 1, iTemp - 1) & _
    " FAILED! Return code: " & Str$(iPingResults)
    End If

  • David Ivory
    11 years ago
    May 23, 2001

    Is there a problem using the ping32.dll to ping by IP? When pinging by name the funcution executes fine. When I use it to ping by IP the response time is fine but the funciton takes up to 30 seconds to return? Similar slow results when running the massping.exe provided in the download.

  • Mike Matheny
    13 years ago
    Aug 10, 1999

    In Mike Otey’s November 1997 VB Solutions column about a timer routine, he set the timer interval to text_time.text * 1000. In the example in Screen 2, he shows an entry of 300, which means you are passing a value of 300,000 to the timer interval. I don’t know what version of VB he’s running (I’m, running Visual Basic—VB—5.0, Service Pack—SP 2), but the largest interval my timer control will take is 65535, which in 1/1000s of a second (the timer resolution) is a little over a minute. Is the 300 entry a misprint, or is it possible? I have seen numerous routines to make the timer extend past 1 minute. You usually set a variable for the number of minutes and then set the timer interval for 60000 (1 minute). When the timer fires, increment the minute variable. When it reaches the desired value, do the routine and disable the timer.
    —Mike Matheny



    You are absolutely right. The largest possible ping interval is 65 seconds. Thanks for pointing this error out. I will post the updated version of the Mass Ping Utility on the Windows NT Magazine Web site at http://www.winntmag.com.

    --Mike Otey

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.