Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

December 01, 1997 12:00 AM

Building a Graphical HOSTS File Edit Utility

Windows IT Pro
InstantDoc ID #561
Rating: (0)
Downloads
561.zip

Make editing your system's HOSTS file easy and safe

[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.]

Windows NT uses either a Domain Name System (DNS) server or a HOSTS file to let TCP/IP applications reference other systems in your network via meaningful names, instead of hard-to-remember numeric IP addresses. If a network has a DNS server, NT uses it rather than the HOSTS file for TCP/IP name resolution. However, many smaller networks don't have a DNS server and must rely on the HOSTS file to resolve TCP/IP host names to their IP addresses.

The HOSTS file is a standard text file that you can edit using a text editor such as Notepad. Although this method works, editing the HOSTS file with a text editor has some serious limitations. First, the HOSTS file has some subtle but important formatting rules that standard text editors don't enforce. Second, the IP addresses and TCP/IP host names that you enter in the HOSTS file must be correct; a standard text editor has no mechanism to ensure that you enter valid values. This month's VB Solutions utility, HOSTS File Edit, makes editing the HOSTS file a less error-prone process because it correctly formats the HOSTS file and lets you verify host names and IP addresses.

The Utility
Screen 1, page 206, shows the HOSTS File Edit utility's main editing window. When you start HOSTS File Edit, the utility automatically displays the contents of the HOSTS file in this window. Unlike a standard ASCII text editor, HOSTS File Edit doesn't let you directly type over the HOSTS file lines. Instead, you right-click the line you want to modify, and the program displays the pop-up menu you see in Screen 1. The menu options let you insert a new line; edit an existing line; or cut, copy, paste, or delete a line. If you right-click a line with an active IP address and host name, you also get the option to ping the remote host.

To add a new entry to the HOSTS file, you right-click the place where you want to insert the line and then select the menu's Insert option to display the Edit HOSTS File Entry dialog box shown in Screen 2, page 206. The utility formats the IP address, host name, and optional comment that you enter in this dialog box. (You must enter the IP address in the standard xxx.xxx.xxx.xxx format.) Clicking OK inserts the new entry in the HOSTS file and closes the dialog box; clicking Cancel discards the new entry and closes the dialog box. When the utility writes the TCP/IP host entry line to the HOSTS file, it places the entry in the first position, which most TCP/IP applications that use the HOSTS file require. If you click Ping, the utility pings the remote host name you entered and verifies that the IP address is correct. (Clicking Ping is optional; thus, you can enter systems into the HOSTS file regardless of whether you are connected to them.) Screen 3, page 206, shows the Ping Host verification screen that displays when you click Ping. If the ping is not successful, the utility displays a warning message.

Inside the HOSTS File Edit Utility
Let's examine how to build the HOSTS File Edit utility. One of the key ways in which the utility differs from a standard text editor is that the utility lets you ping remote hosts and verify the host name and IP address. HOSTS File Edit implements this ping capability using ping32.dll, a DLL based on the ping.c sample that Microsoft distributes in its Winsock development sample. (For details about ping32.dll, see VB Solutions: "Building an Automated Mass Ping Utility," November 1997.) Ping32.dll contains one function (ping) declared in the ping.bas file. The VB declaration for the ping function follows:

Declare Function ping _

Lib "ping32.dll" _

(ByVal sHostName$, _

ByVal sIPAddr$, _

ByVal iDataLen%, _

lPingTime&, _

ByVal iTimeout%, _

ByVal iVerbose%) As Integer

The first parameter of the ping function is a string containing 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. This parameter 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 waits 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 runs 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. (You can download ping32.dll, its source code, and the code for the HOSTS File Edit utility from the Windows NT Magazine Web site at http://www.winntmag.com.)

When HOSTS File Edit starts, it executes the Form_Load subroutine in Listing 1. At callout A in Listing 1, you can see where the GetSystem32Dir function returns a string containing the path of the NT system directory, \winnt\system32. The GetSystem32Dir function uses the Win32 GetSystemDirectoryA API to dynamically retrieve the directory's path. The code at A appends the HOSTS file path to the system directory path and assigns this result to the sHostsFilePath string variable.

At B in Listing 1, the Open function opens the HOSTS file for input. Form_ Load uses the Line Input function to read each line of the HOSTS file and move the contents into the sTextLine variable. The AddItem method adds each line to the List_HOSTS list box. When Form_Load has read all the lines in the HOST file, the Close function closes the file and displays the HOSTS File Edit utility's main window (Screen 1).

As I mentioned previously, to edit the HOSTS file, you right-click in the HOSTS file list and select an option from the pop-up menu. Unfortunately, handling right-clicks in a list box isn't quite as easy in VB as handling left-clicks. To display the pop-up menu after the right-click, I use the MouseDown function of the List_HOSTS list box control. Listing 2 shows the List_HOSTS_MouseDown subroutine.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

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