|
The HKEY_CLASSES_ROOT key governs Windows 95 file types.The Windows 95 Registry is a unified binary object that stores your system's configuration. As Part 1 of this column (PC Magazine, October 24, 1995) described, the Registry Editor (C:\Windows\Regedit.exe) displays the Registry in a hierarchical tree that resembles the Windows Explorer interface; keys are the analogs of folders, while value entries are the analogs of files. Each value entry contains a name and a piece of data (a string, a binary value, or a 4-byte D-word). Keys contain a varying number of subkeys and value entries, but each one must have a Default value entry (this may be empty or it may hold a string). When you first open the Registry Editor, it displays six root keys, and all other keys are nested within those first six. In this column--the second of three describing the Registry--we focus on a root key called HKEY_CLASSES_ROOT (HKCR). For reasons of easy accessibility, HKCR and two other root keys are duplicates of subkeys buried elsewhere in the Registry. HKCR points to the same data as the HKEY_LOCAL_MACHINE\SOFTWARE\ Classes subkey. Both contain information that used to reside in the Windows 3.1 registration database. In fact, if an application tries to write to the registration database, it ends up writing to HKCR. In Part 3, which will appear in an upcoming issue, we will discuss several other root keys and show you how to save and restore the positions of your desktop icons. Registry BackupBefore editing your Registry, do not forget to make a backup of its original settings. To save your configuration to an ASCII file (Save.reg, for instance), select Registry | Export Registry File within the Registry Editor. If you subsequently make a mistake while editing the Registry and impair your system, you can restore your original settings by selecting Registry | Import Registry File or by typing Regedit Save.reg at a real-mode DOS command prompt. You can also make a binary copy of the Registry by backing up two files called User.dat and System.dat (each time your system boots, it builds the Registry from these two files). Both backup methods work well, but an ASCII file is easier to search when you want to locate a particular key or value entry. HKCR and File TypesWith Windows 95, you can open a document simply by double-clicking on it. This feature is accomplished via Windows 95 File Types, which associate applications with file extensions. If you select View | Options within Explorer and move to the File Types tab, you can browse, edit, or create file types. The roots of these file types, however, lie in the Registry. HKCR contains a ProgID subkey for each file type (see Figure 1), a subkey for each extension that is linked to a file type (see Figure 2), and a pair of special subkeys called * and CLSID.
FIGURE 1: The Registry's HKEY_CLASSES_ROOT (HKCR)
key contains a ProgID subkey for each of your system's
file types.
A PROGID represents a file type.
A PROGID'S Default value supplies a file type's name.
A DEFAULTICON subkey supplies a file type's icon.
EDITFLAGS configure a file type's edit buttons.
A SHELL SUBKEY describes a file type's actions.
A SHELLEX SUBKEY links to a file type's handlers.
FIGURE 2: If an HKEY_CLASSES_ROOT (HKCR) extension
key contains a ShellNew subkey, then a document of
that extension is represented in the Windows 95 New
menu.
A COMMAND VALUE runs an executable when the New Menu is
accessed.
AN EXTENSION KEY usually links to a ProgID.
A SHELLNEW SUBKEY defines an extension's New menu
entry.
A FILENAME VALUE copies a specified file when the New menu
is accessed.
The Default value of an extension key contains the name of an associated ProgID, while a ProgID holds a file type's name and its characteristics. Batch files, for instance, use the .bat extension. The Registry contains a key called .bat, which points to the batfile ProgID. The Default value in batfile reads MS-DOS Batch File (the name that describes the file type in the File Types tab). When you double-click on a batch file, therefore, the system accesses the .bat subkey and moves to the batfile subkey, where it is told to run the batch file in a DOS session. Not all ProgIDs, however, are linked to extensions. Such system file types include AudioCD, Drive, File Folder, and File. The * key defines shell extensions and context menu commands for all of your files, while CLSID lists all of your system objects according to specific ID numbers. Subkeys for Extensions and ProgIDsMost extension keys contain only the name of the ProgID that they are associated with, but some may have a ShellNew subkey (see Figure 2). This subkey generates an entry in the Windows 95 New menu and may contain executables or macros (see Figure 3).
FIGURE 3: This Word Basic macro asks you to rename
an invoice file that has just been created via the New
menu, saves it with a .doc extension, and deletes the
original file.
Macro for an Invoice Template in the New Menu
Sub MAIN
CurrentFile$ = FileName$()
NewDir$ = "D:\My Documents\Invoices"
Prompt$ = "Create a new invoice in " + NewDir$ +
"called:"
On Error Goto Bye
FName$ = InputBox$(Prompt$, "New Invoice Namer",
"invoice.doc")
While Files$(NewDir$ + FName$) <> ""
EPrompt$ = "A file called " + NewDir$ + FName$
EPrompt$ = EPrompt$ + " already exists. Please choose a new
name."
FName$ = InputBox$(EPrompt$, "New Invoice Namer",
"invoice.doc")
Wend
FileClose 1
On Error Goto BadName
Name CurrentFile$ As NewDir$ + FName$
FileOpen NewDir$ + FName$
Goto Bye
BadName:
Msg$ = "Program failed. Either " + CurrentFile$
Msg$ = Msg$ + " could not be found, or " + NewDir$ + FName$
Msg$ = Msg$ + " is not a valid file name."
MsgBox Msg$, "New Invoice Namer", 16
Bye:
End Sub
A ProgID defines a particular file type via a vast array of subkeys (see Figure 1). A DefaultIcon subkey contains a file type's icon (each document that has the file type's associated extension is identified by this icon). A shell subkey lists a file type's actions. All actions are listed in a document's context menu (accessed by right-clicking on the document). The Default action--which results from double-clicking on a document--is listed in bold (see Figures 4 and 5). Finally, the shellex subkey links to any special handlers that a file type may require. In some rare instances, a ProgID will contain other subkeys, but we will focus on these three. ShellNew SubkeyTo access the New menu, select New after right-clicking on the Desktop, an open folder, or Explorer. This menu--which lets you create new documents for registered applications--is defined by the ShellNew subkeys in HKCR. A ShellNew key is usually an extension's only subkey (if it has a subkey at all). If an extension has multiple New menu entries, however, it contains several nested ShellNew keys. The .doc key, for instance, contains three separate keys that represent three different entries; Word.Document .6, WordDocument, and Wordpad.Document.1 each have their own ShellNew commands. Some extensions may contain nested ShellNew keys even if they have only one New menu entry. A ShellNew key contains one or more of the following value entries:
* a NullFile entry that contains an empty string (""),
* a Data entry that contains a binary value,
* a FileName entry that contains the name of a file, and
* a Command entry that contains a path to an executable and
any required arguments.
Each of these entries performs a different task when you choose the New menu item that it defines:
* NullFile creates a new empty file.
* FileName creates a copy of the specified file (if there is
no NullFile entry). If this value does not contain a path,
then the system looks for the file in C:\Windows\ShellNew.
* Data copies the specified binary value into a new file (if
there is neither a NullFile nor a FileName entry).
* Command calls an executable when you create a new file.
Windows creates your new file at the point where you right-clicked--either on the Desktop, in a Folder, or in Explorer. The new file's name always adheres to the following format:
New Publicname.ext
Publicname is the string stored in a ProgID's Default value entry, and .ext is the associated extension key. If you create a text file, for instance, Windows calls it New Text Document.txt; the Default value entry in the txtfile ProgID reads Text Document, and .txt is the associated extension key. To demonstrate the power of HKCR, we created a New menu item for an invoice template. This example may seem circuitous, but it will help you understand several Registry concepts. First, create an .inv key that points to an InvoiceFile ProgID. This ProgID should have a Default value that reads Invoice. In order for Invoice to appear in the New menu, however, you must also associate an executable with the .inv extension. To do so, simply insert a path to WinWord within HKCR\InvoiceFile\Shell\Open\Command. Finally, copy your invoice template into C:\Windows\ShellNew before creating a ShellNew key under .inv that contains a FileName entry and a Command entry (see Figure 2). When you select this item from the New menu, the command entry opens a new invoice file called New Invoice.inv via WinWord and then runs the Word Basic macro that is printed in Figure 3. The macro asks you to rename the file, saves it with a .doc extension in a specified directory, and deletes the .inv file. The DefaultIcon SubkeyThe DefaultIcon associates an icon with a particular file or application. Most DefaultIcon value entries adhere to the strict format:
C:\Windows\Filename,N
Filename is a file that holds one or more icons (usually an .exe, .dll, or .ico file) and N is an integer that identifies which icon should be used (the first icon is identified by a 0, the second by a 1, and so on). In some cases, N is a negative value; such values are resource IDs rather than icon numbers (all resources in an executable have a binary ID). To display the icons in a particular file, access a shortcut's property box, click on Change Icon, and choose the file that you wish to view. Some DefaultIcon value entries contain a %1 string. In this case, the file that uses the icon generates the icon itself (within HKCR, %1 is a variable that links to a key's associated file). For this to work, either the system or the file has to supply an IconHandler. The system supplies IconHandlers for .exe, .ico, .cur, .ani, and .bmp files. Hyperterminal (whose Registry key is listed as htfile) is an example of a file that supplies its own IconHandler. The system uses the %1 string for .exe, .ico, .cur, and .ani files, but never for .bmp files. If you want a .bmp file's icon to be a thumbnail of the actual bitmap, you can change the value entry in HKCR\Paint.Picture\DefaultIcon to %1. Unfortunately, if the system has to read a particularly large bitmap, you may encounter a long delay each time your screen refreshes. When a ProgID does not contain a DefaultIcon subkey, Windows 95 must supply an icon from somewhere else. If an executable is associated with the ProgID, the executable's icon is shrunk and placed inside a document icon (a blank sheet of paper). If there is no associated executable, the system uses the generic Windows 95 logo icon. The Shell SubkeyA ProgID's Shell subkey contains all the actions that are associated with a particular file type. The batfile ProgID, which handles batch files, has a subkey for an edit action, an open action, and a print action (see Figure 1). Each action is defined under the MS-DOS Batch File entry listed in the File Types tab (see Figure 4) and in the context menu that results from right-clicking on a batch file (see Figure 5). Each action has a command subkey. This subkey's Default value always contains a command line that includes a %1 string. This string points to the file on which the action is performed. For instance, the command line for the edit action under MS-DOS Batch File is the following:
C:\Windows\Notepad.exe %1
When you right-click on a batch file and select Edit, the system opens the file via Notepad. If you are using long filenames, you should enclose most %1 strings in quotes. Without the quotes, an application may misinterpret spaces that are embedded in a filename.
FIGURE 4: The File Types tab (se-lect View |
Options under Explorer) lets you create, edit, or
remove Windows 95 file types.
FIGURE 5: When you right-click on a file, its
context menu lists one or more actions you can perform
on that file.
When you access a file's context menu, an action usually has the same name as its corresponding subkey. This only occurs, however, when the subkey's Default value contains an empty string. If the string is not empty, then it becomes the name of the action. This allows international versions of Windows to use English names on Registry keys (for example, open) and foreign names in context menus. For reasons of compatibility with certain Windows 3.x applications, you may find shell subkeys under an extension key rather than a ProgID, but this is extremely rare. CLSIDsJust as we all need Social Security numbers to distinguish us from other citizens, a software object requires a GUID--a globally unique identifier. Under Windows 95, GUIDs are referred to as CLSIDs (or Class IDs), which were first used in the OLE specification. A CLSID is a 16-byte value that contains 32 different Hex digits presented in an 8-4-4-4-12 format (8 digits, a dash, 4 digits, a dash, and so on). A software vendor can obtain a CLSID for an object by requesting it from Microsoft or by running programs that are included in the Microsoft SDK (Uuuidgen.exe) and the DDK (Guidgen.exe). When you run such a program, the first eight Hex digits are randomly generated, the next four are built out of the date and time, and the last 20 are built from your machine's hard ware characteristics. The chance of the program generating the same number twice is infinitesimal. CLSIDs are simply names that an operating system uses to refer to specific objects. The Briefcase ProgID, for instance, must use an independent object when creating a new Briefcase. Therefore, it contains its own CLSID subkey that reads:
{85BBD920-42A0-1069-A2E4-08002B30309D}
The Briefcase ProgID, therefore, contains a CLSID subkey whose Default value contains the above string. All CLSIDs are listed under the HKCR\CLSID subkey. The Default value entry of each CLSID key contains the English name of an object, such as CD Audio Track or Microsoft Excel Worksheet. These keys also contain other subkeys that define the object. Two of the most common are ImprocServer and ImproServer32, which contain the dynamic link library (DLL) that is called when OLE invokes the object's handlers. The Shellex Subkey and the * SubkeyFor developers, Windows 95's most appealing feature is its extensibility--third parties can add special handlers to the shell. To set up a handler, obtain a CLSID, assign it to a DLL in an InProcServer subkey, and reference the CLSID elsewhere in HKCR. Such handlers are usually referenced in the shellex subkey of a ProgID. The following are some typical handlers:
* ContextMenuHandler provides an alternate method for adding
items to an object's context menu. WinZip's Add to Zip is an
example.
* IconHandler, as discussed above, allows a file type to
generate its own icon. For instance, .lnk and .pif files
point to a CLSID named shortcut. This handler superimposes a
shortcut arrow at the base of the file's icon.
* DragAndDropHandler lets you add items to a context menu
when an object is right-dragged to a folder. An example is
the Navigator item that Norton Navigator installs.
* PropertySheetHandler lets you add pages to a file's
Property Sheets. Norton Navigator adds one to all files that
display additional date information.
* DropHandler allows data files to react to
drag-and-drop--for example, when WinZip adds a file to a zip
when it is dropped on the zip. This is done via a DropHandler
for the key winzip, which is the ProgID associated to .zip.
A special subkey, HKCR\*, contains handlers that apply to all your objects. It contains a shellex subkey that adheres to the same format as those in ProgIDs. Edit FlagsThe File Types tab in Explorer's View | Options menu lets you edit most of your file types, but certain settings cannot be changed. The default action for a batch file, for instance, runs the batch file instead of opening it via Notepad or Wordpad. Thus, when you double-click on AUTOEXEC.BAT, a DOS window opens, and the file executes. If you want to change this default action and edit a batch file when you double-click on it, however, the File Types tab does not let you do so; the Set Default button for the file type called MS-DOS Batch File is always grayed out. The button is grayed out because HKCR's batfile key contains an EditFlag value entry. Such entries are used throughout the Registry to prevent novice users from altering certain system settings. The binary data in batfile's EditFlag reads d0 04 00 00. If you change this value to 00 00 00 00, you can then change any of the batch file settings. Do not, however, indiscriminately zero out EditFlag; if you do so in a system ProgID such as Drive or AudioCD, it completely disappears from the File Types list. For ProgIDs that are linked to extensions, set all EditFlags to 00 00 00 00. For system ProgIDs, replace EditFlag data with 02 00 00 00. If you wish to have access to some buttons while leaving others grayed out, you must know the function of each EditFlag bit. The last two bytes of data are always zero, but most bits within the first two bytes have a specific effect:
* Byte 1, bit 1: Removes the file type from the master list
in the File Types tab (select View | Options under Explorer)
if it has an associated extension.
* Byte 1, bit 2: Adds the file type to the File Types tab if
it does not have an associated extension.
* Byte 1, bit 3: Identifies a type with no associated
extension.
* Byte 1, bit 4: Grays out the Edit button in the File Types
tab.
* Byte 1, bit 5: Grays out the Remove button in the File
Types tab.
* Byte 1, bit 6: Grays out the New button in the Edit File
Type dialog (select the Edit button in the File Types tab).
* Byte 1, bit 7: Grays out the Edit button in the Edit File
Type dialog.
* Byte 1, bit 8: Grays out the Remove button in the Edit
File Type dialog.
* Byte 2, bit 1: Prevents you from editing a file type's
description in the Edit File Type dialog.
* Byte 2, bit 2: Grays out the Change Icon button in the
Edit File Type dialog.
* Byte 2, bit 3: Grays out the SetDefault button in the Edit
File Type dialog.
* Byte 2, bit 4: Prevents you from editing an action's
description in the Edit Action dialog (select the Edit button
in the Edit File Type dialog).
* Byte 2, bit 5: Prevents you from editing the command line
in the Edit Action dialog.
* Byte 2, bit 6: Prevents you from setting DDE (Dynamic Data
Exchange) fields in the Edit Action dialog.
The EditFlags value for Drive, for instance, is d2 01 00 00 in Hex (1101 0010 0000 0001 in binary). Bits 2, 5, 7, and 8 are on in byte 1, and bit 1 is on in byte 2. The EditFlag for batfile is d0 04 00 00 in Hex or 1101 0000 0000 0100 in binary. In this case, bits 5, 7, and 8 are on in byte 1, and bit 3 is on in byte 2. Bits 4, 5, and 6 of byte 2 apply only to actions that are protected. EditFlags with action keys (such as HKCR\batfile\shell\open) determine protection. If byte 1, bit 1 of such an EditFlag is 0 (or if there is no EditFlag), then the action is protected. If byte 1, bit 1 is 1, then the action is unprotected. Before you change system EditFlags, be very careful. These values are internal to the operating system, are not officially documented, and may change in subsequent versions. Other HKCR CharacteristicsThree of the system ProgIDs--Folder, Drive, and Directory--contain a few additional differences. Folder and Drive define the Folder and Drive file types, respectively, while Directory defines the File Folder file type (its Default value entry reads File Folder). Drive and File Folder should be thought of as children of Folder. Every action assigned to Folder applies to system folders (such as My Computer) as well as File Folders (such as C:\Windows) and Drives (such as A:). HKCR also contains an Unknown subkey that handles unregistered extensions. Normally, the Open With dialog pops up when you click on an unregistered extension, and you can choose from a list of applications. You can also create new file types in this display by clicking on the Always Use This Program to Open This File check box. If you want to open all unknown files in ASCII format and bypass the Open With dialog, add a subkey called HKCR\Unknown\Shell\Notepad\command whose Default value reads C:\Windows\Notepad.exe %1. Registry's HKCR root key contains a great deal of information. It can be a powerful means of customizing your Windows 95 File Types, but be very careful when editing its components. Make sure that you have made binary and ASCII copies of the Registry before making any changes.
|