Building an Automated Mass Ping Utility

Here's a simple utility that you can build with VB to monitor your network systems and provide a continuously updated report on available systems and systems you cannot contact.

Michael Otey

October 31, 1997

10 Min Read
ITPro Today logo in a gray background | ITPro Today


Monitor the availability of your networksystems

[Editor's Note: VB Solutions is about using Visual Basic (VB) to build avariety of solutions to specific business problems. This column doesn't teach you how to write VB, but how to use VB as a tool to provide quick,easy-to-implement solutions that you can use right away.]

In this month's VB Solutions column, I'll show how you can use Visual Basic (VB) to build the Automated Mass Ping utility. You can use this utility to monitor your network systems and quickly identify available systems and systems you cannot contact. You can define a list of networked system names or IP addresses to Automated Mass Ping and specify a time interval for testing availability. Automated Mass Ping periodically wakes up and pings each of the networked systems and reports the results. Screen 1, page 200, presents the PingStatus tab of the Automated Mass Ping window, which appears when you execute theutility.

Using Automated Mass Ping is very easy. You simply click Start Ping on thePing Status tab to start the timed ping process for the list of systems. To stopthe automated ping process, you click Stop Ping. However, before you begin usingAutomated Mass Ping, you need to define the list of systems that you want tomonitor and specify the ping interval on the Settings tab, as shown in Screen 2,page 200.

You can enter either TCP/IP host names or TCP/IP addresses in the text boxat the top of the Settings tab, and then click Add to add the new entry to thelist of systems to be pinged. If you want to ping a TCP/IP host by name, youmust have an entry for that host in your system's HOSTS file, or you must beattached to a Domain Name System (DNS) server that has an entry for the targethost name. If the systems that you are pinging connect across the Internet, youcan use your Internet Service Provider's (ISP's) DNS server to resolve hostnames. To remove an entry from the list, you must select the entry and thenclick Remove. If you want to clear the entire list, you click Clear.

The Ping interval setting controls how often (in seconds) AutomatedMass Ping will begin pinging the list of systems. In Screen 2, you can see thatthe Ping interval is set to 300 seconds, which means that Automated MassPing will begin pinging the list of systems at 5-minute intervals.

The Beep on Ping check box controls whether Automated Mass Pingwill beep if an error occurs during the ping process. Selecting the check boxturns on the beep; clearing the check box lets Automated Mass Ping functionsilently.

You can save the values on the Settings tab to the Registry by clickingSave Settings. If you save the settings, they will be automatically loaded thenext time Automated Mass Ping starts. Otherwise, the settings persist only untilthe application ends.

Inside Automated Mass Ping
Now that you've seen how to use Automated Mass Ping, let's take a closerlook at how to build this utility. At the heart of Automated Mass Ping isping32.dll, a DLL that I built based on the ping.c sample Microsoft distributesas a part of its Winsock development sample. The original Win32 command-lineversion of ping.c operates from only a text-oriented command line. The originalping.c does not operate under program control, and it cannot return the pinginformation to another application. I converted this sample command-line utilityto a DLL that any development environment that supports external DLLs (such asVB) can call. Converting the Microsoft example to a DLL lets you call the pingfunction from VB and return the results of the ping to the calling applications.(You can download ping32.dll and its source code with the code for AutomatedMass Ping from the Windows NT Magazine Web site athttp://www.winntmag.com.)

Before you can use the functions that an external DLL contains, you mustdeclare the functions using a .bas or .cls module. Ping32.dll contains onefunction (ping), which is declared in the ping.bas file. The VB declaration forthe ping function follows:

Declare Function ping _Lib "ping32.dll" _(ByValsHostName$, _ByVal sIPAddr$, _ByVal iDataLen%, _lPingTime&,_ByVal iTimeout%, _ByVal iVerbose%) As Integer

The first parameter of the ping function is a string that contains eitherthe name or the IP address of the system to be pinged. The second parameter is astring that the ping function returns. This string contains the IP address ofthe pinged system and lets the calling program retrieve the IP address for agiven TCP/IP host name. The third parameter specifies the size of the datapacket sent with the ping. The maximum data length that ping32.dll allows is1024 bytes. The fourth parameter, a long variable, returns the elapsedmilliseconds the ping function required to execute. The fifth parameter containsthe maximum time in milliseconds that the ping function will wait for a responsefor sent or received packets. The sixth parameter specifies the mode ofoperation for the ping function. If you set this parameter to True, ping32.dllwill run in verbose mode, the mode that displays all ping results and errors ina message box. Setting the sixth parameter to False runs ping32.dll in silentmode, the mode that reports any ping results and errors in the ping function'sreturn code and output parameters.

Pinging Away
Next, let's look at how to incorporate the ping function into theapplication. At the core of Automated Mass Ping is the Ping_Listsubroutine presented in Listing 1.

The Ping_List subroutine is the workhorse of the utility. Ping_Listdisplays the progress of the ping process and the status of each ping, and callsthe ping function. The first few lines of code in Listing 1 declare Ping_List'sworking variables. At callout A in Listing 1, the subroutine clears theList_PingStatus list of any old status entries. The subroutine then retrievesthe number of items in the List_IP name list using the ListCount method; at B inListing 1, you can see the code that loops through the items in the list. Assoon as the subroutine reads an entry from the list, it adds the entry to theList_PingStatus list; the subroutine uses the List_PingStatus.ListIndex methodto make the newly added item the current selection. The ListIndex method letsthe list scroll as the subroutine adds each item, which keeps the current pingstatus in view.

At C in Listing 1, you can see that the subroutine calls the ping functionwithin the loop. Before calling the ping function, the subroutine uses VB'sSpace function to make sure that the sIP string variable is adequately sized tocontain 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 nullcharacter). Like any string variable that an external DLL returns, the returnvariable must be the right size. If it is too small, the application generates amemory violation error.

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

After the ping function completes, the subroutine adds the values from thereturned variables (sIP and lPingTime) to the list. Because the sIP variablereturns as a C-style null terminated string, the code at D in Listing 1 stripsthe non-displayable null character, Chr$(0), from the string before adding thestring to the List_PingStatus list. If the ping function fails, the subroutinechecks the Beep setting and optionally beeps the user. The program displays afailed 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 toensure that the current ping status entry is always visible.

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

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

When the timer interval has elapsed, the timer control then fires theTimer1_
Timer subroutine, which you see in Listing 3, page 202. The timedexecution of Timer1_Timer runs the ping process at regular intervals. Thissubroutine 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 savingand retrieving configuration settings. The utility saves the settings when theuser clicks Save Settings on the Settings tab; clicking Save Settings executesthe Command_Save_Click subroutine, which you see in Listing 4.

Automated Mass Ping uses VB's built-in SaveSetting function three times towrite the setting values to the Registry. The SaveSetting function makes theRegistry emulate an INI file. The SaveSetting function is easy to use, but itsaves the settings in only the HKEY_CURRENT_USERSoftwareVB and VBAProgram Settings key.

The first, second, and third parameters of the SaveSetting functionuniquely identify the Registry entry, and the fourth parameter contains thevalue to be saved. The first parameter specifies the application's name; forAutomated Mass Ping, I used the string "Automated Ping". The nextparameter specifies the section or subkey of the Registry that will be used. Forthis parameter, I specified "Startup" to denote that the values willbe used when the application starts. The third parameter specifies a uniquesetting for the application. Each SaveSetting statement in Listing 4 uses adifferent key name for the third parameter: "Interval", "Beep",and "List". As you might guess, the Interval key stores the timerinterval from the Settings tab. The Beep key stores the value from the Beepon Ping check box, and the List key stores the list of systems entered onthe Settings tab. The List2String function extracts the entries from the list ofsystems (List_IP) and places them in the sList string. A comma separates eachTCP/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 youcan see in Listing 5, the Form_Load subroutine uses VB's GetSetting function toretrieve the values from the Registry. GetSetting works very much likeSaveSetting. The first three parameters of the GetSetting function identify thekey to be retrieved; the optional fourth parameter supplies a default valuethat's used if the key cannot be retrieved. The GetSetting function returns theretrieved Registry value to the variable that's on the left side of theassignment operator (=). The String2List function in Listing 5 extracts eachTCP/IP host name from the comma-delimited string containing the list of systemsto 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 continuouslyupdated report on the availability of a list of TCP/IP-attached systems. Thesesystems can connect across the Internet, or they can connect across your privateLAN or WAN network. Although Automated Mass Ping simply displays the status ofthe last ping attempt, you can easily modify this utility to perform otheractions, such as sending a network message to notify you when an importantsystem goes down or recording the ping results in a database file to track theavailability of your systems. In next month's VB Solutions column, I'll explorehow to build a graphical HOSTS file editing utility that puts Automated MassPing to work.

We Want Your VB Code!
Windows NT Magazine wants to publish your VB solutions. Send usany interesting and useful VB solutions you've created for your business'sproblems. 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 [email protected].

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like