Skip navigation

VBScripting Solutions: Exploring the HKEY_LOCAL_MACHINE Registry Subtree

Downloads
20507.zip

The HKEY_LOCAL_MACHINE registry subtree contains all the persistent information specific to the local machine, including software settings that don't change in response to the currently logged-on user and hardware settings. For example, in Windows 2000, the HKEY_LOCAL_MACHINE subtree includes information about the computer's Start menu and CPU.

Start Menu Settings
Win2K's Start menu has many customizable elements. Figure 1 shows the dialog box you can activate by selecting Settings, Taskbar from the Start menu. On the Advanced tab, you can modify what the Start menu displays. For example, by selecting the Expand Control Panel check box in the Start Menu Settings list, the Start menu will include a cascading menu that lists all the Control Panel applets you've installed on the machine.

The HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER registry subtrees store the settings for the Start menu. The registry stores these settings in two subtrees because the Start menu is part of the machine (hence, the HKEY_LOCAL_MACHINE subtree), yet the menu is subject to change depending on the currently logged-on user (hence, the HKEY_CURRENT_USER subtree). The HKEY_LOCAL_MACHINE subtree exposes information about the structure of the settings (e.g., number and type of settings) because this information doesn't change for different users. However, because the particular value of each setting can change for different users, the HKEY_CURRENT_USER subtree stores the actual values. The link between the subtrees is maintained through three registry entries—HkeyRoot, RegPath, and ValueName—that combine to identify a registry path (more on these entries shortly).

The main root for the Start menu settings is the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartMenu subkey. Under the StartMenu subkey, a child subkey exists for each setting in the Start Menu Settings list that Figure 1 shows. Table 1 lists all the settings and their respective subkeys.

Each subkey in Table 1 contains a collection of registry entries. As Table 2 shows, these entries describe the subkey's current-status value (CheckedValue, UncheckedValue, and Default-Value entries), registry path (HkeyRoot, RegPath, and ValueName entries), display type (Type entry), and display text (Text entry).

Current-status value. Together, the CheckedValue and UncheckedValue entries define the value that the system stores when you select or clear a check box in the Start Menu Settings list. Two common sets of values are Yes and No, and 1 and 0. For example, if you select the Expand Control Panel check box, the CheckedValue entry has a value of Yes and the UncheckedValue entry has a value of No. Conversely, if the check box is clear, the CheckedValue entry has a value of No and the UncheckedValue entry has a value of Yes. The DefaultValue entry defines the value that the system uses when the CheckedValue and UncheckedValue entries don't contain any values.

Registry path. Together, the values in the HkeyRoot, RegPath, and ValueName entries specify where the registry stores an item's current-status value. The HkeyRoot entry points to one of three registry subtrees: HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, or HKEY_CLASSES_ROOT. The RegPath entry points to the registry subkey under HkeyRoot. The ValueName entry is the name of the registry entry. The third column in Table 1 lists the ValueNames for the various subkeys.

Display type. The Type entry specifies how the OS must display a Start menu item. Typically, the display type is one of the following:

  • Check box—an item associated with a Boolean state (i.e., True or False)
  • Radio button—an item whose value comes from a fixed set of mutually exclusive values
  • Group of items—a group that defines a submenu of either check boxes or radio buttons

Display text. The Text entry specifies the display name for a Start menu item. The registry stores the values for the Text entry and all the other entries in the HKEY_CURRENT_USER subtree.

Now that you know how the registry stores the settings for the Start menu, you can programmatically manipulate those settings. Let's look at how you can programmatically read and change the current-status values for Start menu items.

Reading a Value
Suppose that you want to programmatically learn the current status of the Expand Control Panel setting on the Start menu. Thus, as the script ReadCurStat.vbs in Listing 1 shows, you need to build the registry path before you can use it to read the current-status value.

To build the registry path, you need three pieces of information: the subtree, the subkey path, and the entry name. You can hard-code the subtree information ("HKCU\") because, by default, the HKEY_CURRENT_USER subtree stores the current-status value. If you want to adapt the script to read a different subkey but you're unsure of the subtree, you can read the subtree information from the registry instead of hard-coding the subtree. For more information about reading subtree information, see the Web-exclusive sidebar "Adapting the Scripts" on the Windows Scripting Solutions Web site (http://www.winscriptingsolutions.com).

To obtain the subkey and entry information, you use the WshShell object's RegRead method to read the RegPath and ValueName entries under the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\ CurrentVersion\Explorer\StartMenu\CascadeControlPanel subkey.

You concatenate these three pieces of information so that, by the end of the script, the Path variable contains the complete registry path. You then use that path with the RegRead method to read the current-status value. Finally, you use the MsgBox function to display the returned value.

Changing a Value
You can easily adapt ReadCurStat.vbs so that it not only reads but also changes the current-status value. The script ChangeCurStat.vbs in Listing 2 shows the adaptations.

ChangeCurStat.vbs starts the same way as ReadCurStat.vbs. However, instead of simply displaying the value that the RegRead method returns, ChangeCurStat.vbs assigns that value to the CurrentValue variable. The script then creates a dialog box that displays Yes and No buttons. The dialog box displays the value in CurrentValue and asks whether you'd like to change it. If you click No, the script stops executing. If you click Yes, an If...Then...Else statement reads the values for the CheckedValue and UncheckedValue entries and assigns those values to the Chk and Unchk variables, respectively.

Using another If...Then...Else statement, the script compares the value in Chk against the value in CurrentValue. If the values match, the script uses the WshShell object's RegWrite method to write the value in Unchk to the CascadeControlPanel subkey. Otherwise, the script writes the value in Chk to the subkey.

The Start menu doesn't reflect this change immediately; you need to restart the machine to see the change. However, the dialog box that Figure 1 shows will reflect the change immediately, without a restart.

CPU Settings
Machine-specific information, such as the machine's CPU type, is available through the HKEY_LOCAL_MACHINE subtree. The registry stores CPU information in the HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor subkey. Under the CentralProcessor subkey, you'll find child subkeys with names such 0, 1, and 2. The 0 subkey equates to the first processor, the 1 subkey evaluates to the second processor, and so on. Thus, if you have only one processor, you'll have only the 0 subkey.

The 0 subkey has several entries, including VendorIdentifier, Identifier, and ~MHz. These entries contain the name of the vendor, the CPU's description, and an estimation of the speed in MHz, respectively. To programmatically view these values, you can use the script ReadCPU.vbs in Listing 3, page 11.

The Registry's the Limit
Using the scripts I've provided, you can programmatically read and write to the StartMenu and CentralProcessor subkeys in the HKEY_LOCAL_MACHINE subtree. But that's just the beginning. You can easily adapt these scripts to work with other subkeys. For example, you can access the HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion subkey so that you can read OS information, such as its version number. (See the Web-exclusive sidebar "Adapting the Scripts" for details.) With these scripts and a little ingenuity, the registry's the limit.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish