Monitor the availability of your network
systems
[Editor's Note: VB Solutions is about using Visual Basic (VB) to build a
variety 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 Ping
Status tab of the Automated Mass Ping window, which appears when you execute the
utility.
Using Automated Mass Ping is very easy. You simply click Start Ping on the
Ping Status tab to start the timed ping process for the list of systems. To stop
the automated ping process, you click Stop Ping. However, before you begin using
Automated Mass Ping, you need to define the list of systems that you want to
monitor 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 box
at the top of the Settings tab, and then click Add to add the new entry to the
list of systems to be pinged. If you want to ping a TCP/IP host by name, you
must have an entry for that host in your system's HOSTS file, or you must be
attached to a Domain Name System (DNS) server that has an entry for the target
host name. If the systems that you are pinging connect across the Internet, you
can use your Internet Service Provider's (ISP's) DNS server to resolve host
names. To remove an entry from the list, you must select the entry and then
click Remove. If you want to clear the entire list, you click Clear.
The Ping interval setting controls how often (in seconds) Automated
Mass Ping will begin pinging the list of systems. In Screen 2, you can see that
the Ping interval is set to 300 seconds, which means that Automated Mass
Ping will begin pinging the list of systems at 5-minute intervals.
The Beep on Ping check box controls whether Automated Mass Ping
will beep if an error occurs during the ping process. Selecting the check box
turns on the beep; clearing the check box lets Automated Mass Ping function
silently.
You can save the values on the Settings tab to the Registry by clicking
Save Settings. If you save the settings, they will be automatically loaded the
next time Automated Mass Ping starts. Otherwise, the settings persist only until
the application ends.
Inside Automated Mass Ping
Now that you've seen how to use Automated Mass Ping, let's take a closer
look at how to build this utility. At the heart of Automated Mass Ping is
ping32.dll, a DLL that I built based on the ping.c sample Microsoft distributes
as a part of its Winsock development sample. The original Win32 command-line
version of ping.c operates from only a text-oriented command line. The original
ping.c does not operate under program control, and it cannot return the ping
information to another application. I converted this sample command-line utility
to a DLL that any development environment that supports external DLLs (such as
VB) can call. Converting the Microsoft example to a DLL lets you call the ping
function 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 Automated
Mass Ping from the Windows NT Magazine Web site at
http://www.winntmag.com.)
Before you can use the functions that an external DLL contains, you must
declare the functions using a .bas or .cls module. Ping32.dll contains one
function (ping), which is declared in the ping.bas file. The VB declaration for
the 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 either
the name or the IP address of the system to be pinged. The second parameter is a
string that the ping function returns. This string contains the IP address of
the pinged system and lets the calling program retrieve the IP address for a
given TCP/IP host name. The third parameter specifies the size of the data
packet sent with the ping. The maximum data length that ping32.dll allows is
1024 bytes. The fourth parameter, a long variable, returns the elapsed
milliseconds the ping function required to execute. The fifth parameter contains
the maximum time in milliseconds that the ping function will wait for a response
for sent or received packets. The sixth parameter specifies the mode of
operation for the ping function. If you set this parameter to True, ping32.dll
will run in verbose mode, the mode that displays all ping results and errors in
a message box. Setting the sixth parameter to False runs ping32.dll in silent
mode, the mode that reports any ping results and errors in the ping function's
return code and output parameters.
Pinging Away
Next, let's look at how to incorporate the ping function into the
application. At the core of Automated Mass Ping is the Ping_List
subroutine presented in Listing 1.
The Ping_List subroutine is the workhorse of the utility. Ping_List
displays the progress of the ping process and the status of each ping, and calls
the ping function. The first few lines of code in Listing 1 declare Ping_List's
working variables. At callout A in Listing 1, the subroutine clears the
List_PingStatus list of any old status entries. The subroutine then retrieves
the number of items in the List_IP name list using the ListCount method; at B in
Listing 1, you can see the code that loops through the items in the list. As
soon as the subroutine reads an entry from the list, it adds the entry to the
List_PingStatus list; the subroutine uses the List_PingStatus.ListIndex method
to make the newly added item the current selection. The ListIndex method lets
the list scroll as the subroutine adds each item, which keeps the current ping
status in view.