After declaring the working variables and enabling VB's error handler,
List_HOSTS_MouseDown checks whether the user right-clicked the mouse
button. If a right-click activated the subroutine, List_HOSTS_MouseDown sets the
List_HOSTS.ListIndex property to the line the user clicked. You determine
this line by dividing the height coordinate of the mouse pointer (Y) by
the result of the height of the list box divided by 22 (the number of lines the
list box displays).
At A in Listing 2, the subroutine checks the first character of the line to
determine whether it's a numeric value. If the first character is numeric, the
user has clicked a line that contains a host entry (a host entry must always
begin in the first position of the HOSTS file). Otherwise, the user has clicked
a comment line. If the user has clicked a host entry line, the Ping option
appears in the pop-up menu. At B in Listing 2, the subroutine displays the
pop-up menu, Menu_Popup.
If the user right-clicks a line past the end of the file, the error handler
is invoked. The error handler transfers control to the end of the subroutine and
bypasses the code that displays the pop-up menu.
The HOSTS File Edit utility handles each of the pop-up menu's eight options
(Insert, Edit, Cut, Copy, Paste, Delete, Ping, and Cancel) using VB's standard
Menu_Click subroutine. For instance, selecting the Insert option executes the
Menu_Insert_Click subroutine, selecting the Edit option executes the Menu_Edit_Click subroutine, and so on. Listing 3 shows the Menu_Edit_Click subroutine.
At A in Listing 3, Menu_Edit_Click checks the first character of the line
for a number. If the first character is a number, the user has clicked a host
entry line; otherwise, the user has clicked a comment line. At D in Listing 3,
page 208, you can see the code that executes if the selected line is a comment
line: The subroutine moves the line into the Text_Comments text box on the
Form_Comment form, and displays the form.
If the line is a host entry, the subroutine executes the code that starts
at B in Listing 3. The subroutine extracts the IP address from the string that
contains the selected list item text and moves the IP address into the sTemp
string variable. Then the SelText method of the MaskEdBox_IPAddress masked edit
text box loads the contents of sTemp into the masked edit text box on Form_Edit.
This box contains the code for the Edit HOSTS File Entry dialog box in Screen 2.
Form_Edit uses a masked edit text box to ensure that the TCP/IP address is in
the correct xxx.xxx.xxx.xxx format.
The code at C in Listing 3 checks whether a comment resides on the line
with the host entry. (A comment must begin with the pound character, #.) If
there is a comment, the subroutine moves it into the Text_Comment text box on
Form_Edit.
Next, the subroutine moves the TCP/IP host name into the Text_HostName text
box on Form_Edit. Finally, Menu_Edit_Click displays Form_Edit as the Edit HOSTS
File Entry dialog box.
The HOSTS File Edit program uses the Edit HOSTS File Entry dialog box for
making new entries in the HOSTS file as well as for modifying existing entries.
Depending on whether the user selects the Insert option or the Edit option on
the main editing screen, clicking OK in the Edit HOSTS File Entry dialog box
either inserts a new entry or replaces an existing entry in the HOSTS file. In
both cases, the HOSTS File Edit program concatenates the contents of the
MaskEdBox_IPAddress, Text_HostName, and Text_Comment objects into a
string and adds it to the list.
The Ping Option
The Edit HOSTS File Entry dialog box includes the Ping button that lets the
user ping a remote host and verify that the host can be contacted. Clicking Ping
displays Screen 3 and activates the Command_Ping list button, which initiates
the Ping_List subroutine in Listing 4.
At A in Listing 4, Ping_List uses a For ... Next loop to ping the target
system four times. Within the loop at B in Listing 4, you can see where the
subroutine calls the ping function declared in ping32.dll to ping the remote
system. The first parameter of the ping function, string variable sIPAddress,
contains the IP address entered in the masked edit text box on the Edit HOSTS
File Entry screen. The second parameter, string variable sIP, returns the IP
address from the remote host. Before issuing the ping, the subroutine sizes sIP
to 16, the maximum size of the returned IP address. The third parameter
specifies the size of the data packet that will be pinged. In this example,
32-byte packets are sent back and forth between the local and remote TCP/IP
systems. The fourth parameter, long variable lPingTime, returns the amount of
time required to complete the ping function. The fifth parameter specifies the
maximum timeout value in milliseconds. In this case, the ping times out after 2
seconds (2000 milliseconds). The last parameter of the ping function controls
whether the function will be executed in verbose or silent mode. A 0 value
specifies silent mode; a 1 specifies verbose mode, which is useful for debugging
the execution of the ping function. After the ping function completes,
subroutine Ping_List checks the return code for an error. If no error occurred,
the subroutine adds an item to the List_PingStatus list showing the time
required to complete the ping. If an error occurred, the subroutine adds a
failure message and the ping return code to the List_PingStatus list.
At C in Listing 4, the subroutine posts the summary status of the ping
results in the List_PingStatus list. If no errors occurred in any of the four
ping attempts, Ping_List posts a success message. Similarly, if none of the ping
attempts succeeded, Ping_List posts a failure message. Otherwise, some ping
attempts succeeded and others failed. In this case, Ping_List posts the message
"Host Verified with error" to the List_PingStatus list.
After completing all HOSTS file changes, the user can select the Save
option from the File menu to write the changes to the HOSTS file. The utility
does not save changes until the user selects the Save option. Clicking the Close
button in the window's upper right corner or the program icon in the upper left
corner closes the HOSTS File Edit program and discards changes. Listing 5 shows
the Menu_Save_Click subroutine, which writes the file to the disk.
In the Menu_Save_Click subroutine, the Open function opens the HOSTS file
for output. The sHostsFilePath string variable (set during the initial form load
in Listing 1) contains the filename and directory path to the HOSTS file. After
opening the HOSTS file, the subroutine employs a Do While loop to traverse the
List_HOSTS list (which contains the updated contents of the HOSTS file) and to
write each entry to the HOSTS file. After the Do While loop completes, the
subroutine closes the file.
Easy HOSTS File Editing
The HOSTS File Edit utility provides a safe, easy-to-use mechanism for
updating a local HOSTS file. Unlike a standard text editor, the HOSTS File Edit
utility lets you verify the TCP/IP addresses written to the HOSTS file, as well
as ensure that the data written to the HOSTS file is correctly formatted. Stay
tuned to Windows NT Magazine for other VB utilities that help you solve
common business problems.