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.