
Some of the main topics in this chapter are
You've learned many tips and tricks in this book that involve adding keys and value entries to the Registry. You'll find many more on the Internet and in places such as the Microsoft Knowledge Base.
Great for you, but what about all the other users on the network? If you're responsible for more users than just yourself, you'll be happy to know that you can easily distribute those Registry changes to them through REG and INF files.
REG files are text files that you can easily import into the Registry. In fact, when you double-click a REG file, Windows automatically imports it into the Registry by default. This makes it easy for you to distribute the REG file. When the user opens it, Windows NT automatically makes the changes that the REG file contains.
INF files are intended for use as setup information files. Windows supports a large number of keywords in INF files that vendors can use to help automate the setup and installation of software, device drivers, hardware, and so on. There are a few keywords, however, that are specific to the Registry. You can use those keywords to create "scripts" for making Registry changes.
CAUTION: Don't double-click a REG file unless you know for sure what it contains. Double-clicking a REG file merges its contents into the Registry without any confirmation.
REG files resemble INI files. Take a look at Listing 45.1. The first line always contains REGEDIT4 (4 representing the version). This indicates that the file was created by REGEDIT. The remainder of the listing contains keys and value entries you wouldd normally find under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\FS Templates.
REGEDIT4 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\FS Templates] @=îServerî [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\FS Templates\Desktop] @=îDesktop computerî [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\FS Templates\Mobile] @=îMobile or docking systemî ìPathCacheî=hex:10,00,00,00 ìNameCacheî=hex:51,01,00,00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\FS Templates\Server] @=îNetwork serverî ìPathCacheî=hex:40,00,00,00 ìNameCacheî=hex:a9,0a,00,00
The file is split into multiple sections, with each Registry key in its own section. It doesn't contain every key in the Registry, only a subset. The file gives the fully qualified name of the Registry file key--that is, you see the entire name of the path to that key, beginning with the name of the root key--between two brackets. Each value entry for a key is listed under that key's section. The value entry's name is quoted, except for default value entries, which REGEDIT represents with the at sign (@). The value entry's data looks different depending on its type, as shown in Table 45.1.
| Type | Example |
| String | "This is a string value" |
| DWORD | DWORD:00000001 |
| HEX | HEX:FF,00,FF,00,FF,00,FF,00,FF,00,FF,00 |
Creating a REG file manually is easy enough, and you should learn how to do it so that you better understand what's in a REG file; however, there are better ways to make this file that you'll learn about in the next section. Create a new file with the REG extension by using your favorite text editor; then follow these steps:
2. Add a section for each key that you want to add to or change in the Registry. Put each key name in square brackets, like this: [Keyname]. Make sure that you use the fully qualified path to the key, beginning with the root key.
3. Put an entry under each key for each value entry that you want to add or change. Each entry has the form "Name"=Value, where Name is the name of the value entry that you're adding or changing. If you're changing the default value entry, use the at sign (@) for Name, without the quotes. Value is the value to which you want to set the value entry. Make sure that it follows one of the forms shown earlier in Table 45.1.
4. Save your changes to the REG file.
NOTE: You can't use a REG file to remove a key from the Registry. If you need to create a script to do this, consider building an INF file. The Windows NT Server 4.0 Resource Kit contains complete information about building INF files.
Creating REG files by hand seems easy enough, but it's not recommended. There are entirely too many chances for errors--especially if you're a bad typist. Thus, you should use REGEDIT if at all possible. Chapter 43, "Editing the Registry," shows you how to export a branch of the Registry to a REG file. For your convenience, however, those instructions are repeated here:
2. Choose Registry, Export Registry File to reach the dialog box shown in Figure 45.1.
3. Select Selected Branch. REGEDIT automatically fills in the key you selected in Step 1.
4. Type the filename into which you want to export the Registry in the File Name text box, or browse your computer for a folder and filename. Click Save.
If you don't type a file extension in the File Name text box, REGEDIT uses the default file extension (REG).
You should be aware of a couple of issues when using this technique:
The INF file's filetype describes INF files as "Setup Information" files. This is a much more generic description than Microsoft usually uses for INF files, which is that INF files contain device driver information. In fact, you can put much more in an INF file than device driver information. You can put software setup information. More important, to this book anyway, you can put lines in an INF file that add, change, and remove Registry entries when the user installs the INF file.
INF files are pretty complex. Microsoft has created a whole language for INF files and has defined intricate rules that link various sections of the INF file together. Most of the statements and sections that programmers use in INF files are irrelevant to the topic at hand. That is, you don't need to worry about all the complexity to use an INF file to script changes to the Registry.
The sections that follow describe how to create such an INF file from scratch. INF files look similar to INI files. They're divided into sections, and each section contains a number of entries. Each of the following sections describes a single section within the INF file.
The first section in all INF files is the [Version] section. For the purpose of editing the Registry, this section always looks like the following. Note that the Class item, which you normally see in INF files in the [Version] section, isn't required to create a file to edit the Registry. $CHICAGO$ identifies that the INF file is for Windows 95 (Chicago was the beta name for Windows 95). It also works for Windows NT 4.0.
[Version] signature="$CHICAGO$"
The [DefaultInstall] section contains the names of other sections, which specify the changes you're making to the Registry within the INF file. For example, you might have a section that contains Registry entries you're adding and another section that contains Registry entries you're removing. Here's what a typical [DefaultInstall] section looks like:
[DefaultInstall] AddReg=MyAddReg DelReg=MyDelReg
You must type the name of each item (on the left side of each equal sign), as shown in the preceding example. You can name the section (on the right side) anything you like, however. The AddReg item identifies the section that contains new Registry entries. The DelReg item identifies Registry entries that you're deleting.
For each item you put in the [DefaultInstall] section, you have to create another section. The name of the section will be the value on the right side of each item. Thus, carrying on the example, you'd create two sections, named [MyAddReg] and [MyDelReg]. The items listed in [MyAddReg] are added to the Registry. The items listed in [MyDelReg] are removed from the Registry.
Each line within these sections has a similar format, shown in the following example. You use all the parameters shown if you're adding a Registry entry. If you're removing a Registry entry, you only use the first three.
HKEY, Subkey, Name, Type, Value
Here's a description of each part of this line:
| HKEY | One of the abbreviations for the root keys shown in Table 45.2. |
| Subkey | The subkey under the root key, not including the name of the root key itself. |
| Name | The name of the value entry you're adding. Leaving this item blank implies you're working with the default value entry. |
| Type | The type of value entry (0 for string or 1 for binary). |
| Value | The data for the value entry. Use the appropriate format for the type you specified. That is, strings should be quoted, and binary values should be in hexadecimal notation with each byte separated by a comma. |
| Abbreviation | Root Key |
| HKCR | HKEY_CLASSES_ROOT |
| HKCU | HKEY_CURRENT_USER |
| HKLM | HKEY_LOCAL_MACHINE |
| HKU | HKEY_USERS |
| HKCC | HKEY_CURRENT_CONFIG |
| HKDD | HKEY_DYN_DATA |
For example, the following lines would change the value entry named Howdy found in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\MyEntry\ to "Hello World":
[MyAddReg] HKLM,Software\Microsoft\Windows\MyEntry,Howdy,0,"Hello World"
The next lines remove the value entry named Howdy from HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\MyEntry.
[MyDelReg] HKLM,Software\Microsoft\Windows\MyEntry,Howdy
CAUTION: If you don't specify the name of a value entry when removing a value entry with an INF file, Windows removes the entry branch.
So far, you've seen each section of an INF file described, but you haven't seen a completed INF file. The following is a simple example of an INF file that adds the value entry Howdy to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\MyEntry and sets its value to "Hello World". It also removes the entire branch, starting with the subkey named TestEntry from HKEY_CURRENT_USER\Software and removes the value entry named TestValue from HKEY_LOCAL_MACHINE\Software\TestEntry.
[Version] signature="$CHICAGO$" [DefaultInstall] AddReg=MyAddReg DelReg=MyDelReg [MyAddReg] HKLM,Software\Microsoft\Windows\MyEntry,Howdy,0,"Hello World" [MyDelReg] HKCU,Software\TestEntry HKLM,Software\TestEntry,TestValue
Here's how to test this example:
2. Create a new subkey named HKEY_CURRENT_USER\Software\TestEntry. Add a few subkeys underneath this one.
3. Create a new subkey named HKEY_LOCAL_MACHINE\Software\TestEntry and add a value entry named TestValue. Set the value entry to anything you want.
4. In Windows Explorer, right-click the INF file and choose Install. Open the Registry Editor to observe the changes.
The previous example is a bit contrived. The following listing shows you a more useful example. The [Version] and [DefaultInstall] sections are the same as the previous examples.
[Version]
signature="$CHICAGO$"
[DefaultInstall]
AddReg=MyAddReg
DelReg=MyDelReg
[MyAddReg]
HKCU,Software\Microsoft\Windows\CurrentVersion\Policies\Explorer,NoSaveSettings,1,1
HKCU,Control Panel\Desktop\WindowMetrics,MinAnimate,1,0
[MyDelReg]
HKLM,SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{FBF23B42-E3F0-101B-8488-00AA003E56F8}
The [MyAddReg] section adds or changes two binary value entries in HKEY_CURRENT_ USER. The first line changes the NoSaveSettings value entry to 1 so that Windows Explorer will no longer save the size and position of desktop windows when you shut down Windows. The second line changes the MinAnimate value entry to 0 so that you don't see window animations when you restore, maximize, or minimize windows (lines drawn from the taskbar to the window to indicate its location).
The [MyDelReg] section removes a subkey from the HKEY_LOCAL_MACHINE root key in the Registry. In this case, it removes {FBF23B42-E3F0-101B-8488-00AA003E56F8}, which removes the icon titled "The Internet" from the user's desktop. Because this line doesn't specify a value entry, Windows removes the entire branch.
A good way to impress the users that you support is to distribute fixes to them without their asking. Better yet, make it as simple as possible, and they'll really adore you.
If you have an intranet in your organization, you can make REG and INF files available on a Web page. Lacking an intranet, you can also distribute REG files in e-mail messages or in the user's logon script. You'll learn about each method in the remaining sections.
Remember, the default Windows NT action for a REG file is to import it. The default action for an INF file is to open it in a text editor as opposed to installing it, however, so you won't be able to distribute an INF file on a Web page (you can use e-mail or other methods, as shown later). You can put a link to a REG file on a Web page. Then, when a user clicks that link, the browser will download the file to the user's computer and automatically apply the change that it contains. This works fine for Internet Explorer users, but Netscape users will have to register a helper application for REG files. Figure 45.2 shows you an example of a Web page that contains a few REG files. When you build such a Web page, it's recommended that you follow these suggestions:
Make sure that your page fits in with the overall Web site, so users don't feel they've dropped into Dr. Jekyll's laboratory.
If you don't have an intranet in your organization, the next best thing to do is distribute a REG or INF file in an e-mail message. Attach the file to the message and instruct the user how to apply the file. In most e-mail programs, the user just has to double-click the attachment to apply the REG file changes, or right-click an INF file and choose Install.
Follow the same recommendations given for Web pages in the preceding section. Tell the recipients whether they need to apply the REG file, tell them what it's going to do to their computer, tell them how to view the file, and make sure that they know how to reach you. Figure 45.3 shows an example of such an e-mail message.
It takes a bit of work, but you can also put a command in each user's logon script that automatically applies the REG or INF file when the user logs on to the network. If you've set up a logon script for multiple users, you can slip the command into that script instead. All you have to do is put the following command in the script (filename is the name of the REG file):
start filename.reg
The one problem with this method is that every time the user logs on to the network, the logon script will apply the REG file. You can avoid this problem by putting the previously listed command in a batch file. Then the logon script can check for the existence of this batch file on the user's computer. If it doesn't exist, the logon script copies the batch file to the user's computer and executes it. Otherwise, it just ignores it.
You can also post a REG file on a Web page and e-mail a notice containing its description and URL, instead of including the REG file in the e-mail message.
The command line for INF files is a bit more convoluted because the default action for an INF file isn't to install it. Thus, you must actually specify the command line that Windows uses for the Install action of the INF file type, like this (filename is the name of the INF file):
C:\WINDOWS\rundll.exe setupx.dll,InstallHinfSection DefaultInstall 132 filename.inf
© Copyright, Macmillan Computer Publishing. All rights reserved.