VBScripting Solutions: Writing to the HKEY_CURRENT_USER Subtree

The HKEY_CURRENT_USER subtree might not seem too interesting because it reads like a list of installed applications. However, by writing to this subtree, you can customize your applications beyond the options available through their UIs.

Dino Esposito

March 25, 2001

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


Windows Shell Scripting

The registry's HKEY_CURRENT_USER subtree contains persistent information about the user who's currently logged on. This information includes mostly configuration settings for various applications and a few system settings. Thus, when you use a registry editor to view this virtual subtree, you see lists of installed applications. (For information about the subtree's virtual nature, see "A Virtual Reality," page 4.) For this reason, HKEY_CURRENT_USER might not seem as interesting as other registry subtrees. However, applications save many more settings to the registry than their UI suggests. Thus, exploring this subtree can be not only interesting but also useful. If you're knowledgeable about an application's registry settings, you can achieve effects not possible through the application's UI. In other words, you can customize applications' appearance and behavior by writing to the HKEY_CURRENT_USER subtree.

Writing to the registry can be a potential source of trouble if you write the wrong information or write information in the wrong place. However, experimenting with HKEY_CURRENT_USER is less risky than experimenting with other subtrees because it stores only a small amount of system information. Although inadvertent changes might break existing applications, they won't require you to reformat or reinstall the OS. Moreover, many applications let you restore standard registry settings directly from the setup executable, without the need to reinstall the entire product. You simply run the setup executable with a /y switch

setup.exe /y

at the command line to activate a version of the setup program that doesn't copy files but just refreshes the registry settings. Although this feature is common in applications, many systems administrators don't know about it.

With minimal risk, you can investigate the HKEY_CURRENT_USER subtree and experiment with changing application settings to customize the behavior or appearance of your applications. (Experimentation is often necessary because few customizations are documented.) Let's look at several examples of how you can make such customizations.

Customizing Notepad
Suppose you want a script to launch Notepad. However, you want Notepad to use the Courier New font instead of the default font. In addition, you want the Word Wrap mode enabled.

Notepad stores its persistent configuration settings in the subkey HKEY_CURRENT_USERSoftwareMicrosoftNotepad. The code in Listing 1 changes the necessary registry settings in the Notepad subkey, then launches Notepad. Here's how the code works. First, the code sets the Notepad subkey's fWrap entry. This entry stores a value of type REG_DWORD that specifies whether the Word Wrap mode is disabled (value of 0) or enabled (value of 1). By default, the Word Wrap mode is disabled, so the code sets fWrap to 1. Although you won't find this information documented anywhere, you can easily deduce it from looking at the Notepad subkey's entries and their values.

Next, the code sets the lfFaceName entry. This entry contains a value of type REG_SZ that specifies Notepad's font. By default, Notepad uses the Fixedsys font, so the script sets lfFaceName to "Courier New".

Finally, the code launches Notepad. Like many other programs, Notepad reads its current settings from the registry before building its UI. When Notepad exits, the system saves any new registry settings. Thus, if you don't want Notepad to always open with the Courier New font and Word Wrap mode enabled, you need to change these settings back to their default values.

You can use Notepad's UI to set the font and Word Wrap mode. However, Notepad has more options that you can set only through the subkey HKEY_CURRENT_USERSoftwareMicrosoftNotepad. For example, Notepad's UI doesn't include an underline option, but the Notepad subkey includes the lfUnderline entry. This entry suggests that you can use underlined fonts in Notepad—a feature most users would think impossible based on the UI. However, if you set the lfUnderline entry value to 1 with the code in Listing 2 and then run Notepad, you can obtain results like those in Figure 1. Having underlined text in Notepad probably isn't a feature you've longed for, but this example illustrates how a good understanding of the registry can help you achieve effects not possible through a program's UI.

Perhaps a more useful feature is having Notepad open at a certain position and with a certain window size. In Windows 2000, you can set the iWindowsPosX and iWindowsPosY entries to specify the position of the window's top-left corner and the iWindowsPosDX and iWindowsPosDY entries to specify the width and height, respectively. All values are in pixels.

Customizing Outlook
A couple of years ago, I had a machine with two hard disks, one of which was removable. I chose this configuration because I was using different OSs. This configuration made using my favorite email program, Microsoft Outlook Express 5.0, rather inconvenient because each OS required a different configuration and thus a different message store. By default, Outlook Express 5.0 stores messages in a store folder on the C drive, which, in my case, was on a different hard disk, depending on the OS in use. Thus, any message sent or received from a partition was invisible to the other—definitely not an ideal situation.

Outlook Express 5.0's UI doesn't let you select the store folder. However, by exploring the registry, I discovered that I could solve the problem by changing just one value in the registry: the value for the Store Root entry under the HKEY_CURRENT_USER{Identity GUID}SoftwareMicrosoftOutlook Express5.0 subkey, where {Identity GUID} is Outlook Express's globally unique ID (GUID). The Store Root entry contains the store folder's path. Using the code in Listing 3, I changed the default path to the path of a common folder on a shared hard disk, and my troubles went away. (Outlook Express 5.01 and later lets you change this path from the UI's Options dialog box.)

When I changed my setup from a multidisk machine to a LAN, I had to access the same store folder from different machines. Once again, the Store Root entry proved useful. I used the LAN code in Listing 3 to change the store folder's path. (In this instance, you can't use the Options dialog box in Outlook Express 5.01 and later to change the Store Root entry's value because the dialog box doesn't let you select a folder over a network.)

With the LAN setup, two connected machines work with the same database of email. Thus, I can send, retrieve, or delete messages from both machines without having to synchronize them. The only slight inconvenience is that the machine accessing the mail as a remote resource can't cache records locally, so if I change the selected folder (e.g., from Inbox to Outbox), all its records are read back from the disk.

Customizing the Places Bar
Let's look at a more complex example of how to use the HKEY_CURRENT_USER subtree to customize your Windows system. If you have Win2K, you've probably noticed that in all system applications, the Open dialog box has a new UI element. Along the left side, you have a vertical toolbar with buttons pointing to frequently used folders. This toolbar is called the Places Bar. By default, the Places Bar contains links to History, My Documents, Desktop, My Computer, and My Network Places. The only way you can change these links is to write to the registry.

The registry subkey you work with is HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionPoliciesComDlg32Placesbar. You won't see this subkey in the registry editor. By default, the Placesbar subkey doesn't exist. However, if you create this subkey, the OS recognizes and supports it. In other words, if the OS detects a Placesbar subkey, it uses that Places Bar instead of the default Places Bar.

Listing 4 contains code that creates the Placesbar subkey. Each custom link, or place, is represented by an entry named PlaceX, where X is an integer from 0 through 4. Place0 corresponds to the first entry, Place1 corresponds to the second entry, and so on. You can't have more than five places. You can use a string (REG_SZ) to identify the target folder for a PlaceX entry. You must specify the folder's full pathname. You can also use a numeric index (REG_DWORD) to specify the target folder if it's a system folder. For example, the index for My Documents is 5 and the index for Favorites is 6. (For a complete list of indexes, go to http://msdn.microsoft.com/library/default.asp?url=/library/psdk/shellcc/shell/objects/shell/shellspecialfolderconstants.htm.)

If you run the code in Listing 4, all the system applications throughout your Win2K system will feature an Open dialog box similar to the one that Figure 2 shows. If you want to go back to the default Places Bar, you simply delete the Placesbar subkey you created.

The code in Listing 4 changes the Places Bar in all system and user applications that run under Win2K and that take advantage of the common Open and Save dialog boxes. This code won't work with applications that use a homemade version of these dialog boxes, such as Microsoft Office 2000 applications. To customize the Places Bar in Office 2000 applications, you need to access the HKEY_CURRENT_USERSoftwareMicrosoftOffice9.0CommonOpen FindPlaces subkey. You can customize the Places Bar in Office 2000 applications regardless of the underlying OS because Office 2000 doesn't rely on any built-in system service for its Places Bar. Thus, the Places Bar is independent from the OS. Microsoft provides a Places Bar add-on for Office 2000 applications. For more information about this add-on, see the Microsoft article "INFO: Places COM Add-In for Office 2000 Available for Download" (http://support.microsoft.com/support/kb/articles/q260/1/90.asp).

Next Month
By writing to the HKEY_CURRENT_USER subtree, you can customize the behavior and appearance of applications beyond the options available through their UIs. Such customizations are often the offspring of curiosity and investigation because few are documented.

Next month, I discuss another core part of the registry: the HKEY_LOCAL_MACHINE subtree. This subtree stores important data about characteristics of the current machine.

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