Register/Unregister Your Executables from Explorer

If you frequently work with ActiveX (COM) components, you probably have had to manually register and unregister your components on occasion.
However, performing this task from the command line or the Run dialog becomes tedious quickly.
Wouldn’t it be nice if you could just locate the file in Explorer and do it from there? Well, you can.

One approach you can take is to add a shortcut to the regsvr32 program to your SendTo menu.
That works great for registering a program, but shortcuts provide no obvious mechanism for passing the "/u" argument,
so you have to create a batch command file to perform the unregister and then put a shortcut to that file into your SendTo menu.

The SendTo approach works fine, but it isn’t very elegant. Every time you perform an unregister operation,
the batch program opens up a DOS window and then displays the results dialog. It just looks clunky.

A more elegant and convenient solution is to add Register and Unregister items to the right-click menu for executable files.
This approach is easy to use, it’s elegant, and it will impress your developer friends. So what’s the trick?

The trick is to add a few new keys and values to your registry. This article explains the theory behind how it works.
At the end of the article is a link to a download file that contains a sample reg file you can import into your own registry.
The reg file sets up Register and Unregister items for any file that has an exe, dll, or ocx extension.

Explorer File Types

Your first inclination may be to use Explorer’s Options dialog to add the popup menu items.
You can get to the Options dialog by choosing the Options item from the View menu.
The Options dialog has a File Types tab that lists all of the registered file types on your system.
If you edit one of the file types, the Edit File Type dialog shows you a list of "actions" that have been defined for that file type.
The actions for a file type define what items appear in the popup menu and what command should be executed when the action is chosen.

However, there are a couple of problems with using the Options dialog to set up your Register/Unregister commands.
The first problem is that the file type list is organized by file type, not file extension.
There is no way to search for a particular extension and determine its associated file type.
You actually have to step through the list of types until you see the extension you are after.
Another problem is that not all file types appear in the list. For example, dll files belong to the file type Application Extension,
but you won’t see that file type in the list. Additionally, not all file types are editable: exe files belong to the Application file type,
which appears in the list, but the edit button is disabled.

Unfortunately, the File Types tab was rendered "end-user safe" to such a degree that it is virtually useless.
Until someone writes an improved file type manager, your only recourse is to go straight to the registry.

Working with the Registry

Luckily, Microsoft provides a decent tool for working with registry entries.
If you have developed Windows applications for any length of time, you are probably already familiar with the RegEdit program.
To run the program, choose Run from the Windows Start menu and enter "regedit."
If you use the program frequently, you may want to set up a shortcut for it.

This article focuses on what you need to do in your registry, not on how you accomplish it with RegEdit.
Refer to the online help if you need more information on how to use the program. Using RegEdit makes some people nervous.
This reaction is probably due to the fact that inadvertent changes can have dire consequences.
If you have any doubts, back up your registry before you start making changes.

Finding File Types in the Registry

All of the information you find in the File Types tab of the Explorer Options dialog is stored in the registry somewhere.
Even the file types you don’t see in the Options dialog are stored in the registry. In fact, once you know what you are doing,
you may find it easier to modify file types using RegEdit instead of the Options dialog.
The techniques you are about to learn can be applied to any file type, so you can create popup menu items for any file type you want
not just executable files.

First, you need to determine which registry entries are involved. You need to be concerned with two main keys:
the file extension key and the file type key. Both of these keys can be found under HKEY_CLASSES_ROOT.

The file extension keys are obvious as soon as you open HKEY_CLASSES_ROOT because they are usually at the top of the list.
Example 1 shows the extension key for an exe file, which is .exe. Once you locate the appropriate extension key,
you can determine the associated file type by examining the default value for the key.
The default value for the .exe key is "exefile."

Using RegEdit to find a file extension key
Example 1: Using RegEdit to find a file extension key

Once you know the file type name, you can look for the corresponding file type key, which is also under HKEY_CLASSES_ROOT.
You will find the exefile key farther down in the list because keys are displayed alphabetically.
Example 2 shows that the default value for the exefile key is "Application," which should ring a bell.
This value is the name that appears in Explorer’s file types list. The EditFlags value controls how the Options dialog works with the file type.
You can ignore this value for now because it doesn’t affect the popup menu.

Example 2: Using RegEdit to find a file type key
Example 2: Using RegEdit to find a file type key

Defining Actions

Under the exefile key, there is a shell key. The shell key contains additional keys.
Each key under the shell key identifies a popup menu item (or "action" as the Edit File Type dialog calls them).
To create a new popup menu item, all you have to do is add a new action under the shell key.
Example 3 shows that the exefile file type has three actions: open, register, and unregister.
You use the default value of the key to control the text that appears in the popup menu and the corresponding accelerator key.
In the example, the text is "&Register." The ampersand denotes that the subsequent character,
in this case "R," is the accelerator key.

Example 3: Using RegEdit to create actions
Example 3: Using RegEdit to create actions

The last step in setting up an action is to create the command key for that action.
The default value of the command key provides the command line for the associated action.
Example 4 shows the command line for the "register" action. Note that you can use "%1" as a placeholder
for the file currently selected in Explorer. Note also that the file name placeholder is enclosed in double-quotes,
which allows the command line to work properly with long file names.

Example 4: Using RegEdit to set up an action's command line
Example 4: Using RegEdit to set up an action’s command line

The command line can include a program name, a file placeholder, and any arguments that should be passed to the program.
The command line in Example 4 runs the currently selected file passing the /REGSERVER argument, which tells the executable to register itself.

The command line for dll and ocx files is a little different because the files are registered with an external program (regsvr32.exe).
Example 5 shows you the command line for the "unregister" action of the dllfile file type.
The command line tells Explorer to run regsvr32 passing the "/u" argument followed by the path of the currently selected file.

Example 5: Setting up the command line for the dllfile unregister action
Example 5: Setting up the command line for the dllfile unregister action

Creating File Types from Scratch

You probably won’t have any keys in your registry related to ocx files, but you can create what you need from scratch easily.
You can use the dllfile file type as a model for the necessary keys.

First you need to create a file extension key. In RegEdit, click on the HKEY_CLASSES_ROOT key and choose Edit, New, Key from the menu.
Type ".ocx" for the key name (don’t forget the dot in front of "ocx"). You automatically get a default value for your new key.
Double-click the "(Default)" value in the right pane and enter "ocxfile" for the string value.
The default value you just entered specifies the name of the corresponding file type key.

To be consistent with the .dll key definition, you should create a Content Type key for ocx files.
Choose Edit, New, String Value from the menu and enter "Content Type" for the name. Double-click the name
and enter "application/x-msdownload" for the string value. This value defines the MIME content type for programs that need to know,
such as Internet Explorer.

Example 6 shows what your .ocx key should look like when you are done.

Example 6: Creating an ocx file extension key
Example 6: Creating an ocx file extension key

Now create the file type key. Click on HKEY_CLASSES_ROOT again and choose Edit, New, Key from the menu. Enter "ocxfile" for the key name.
Edit the default value and enter "Custom Control." The default value specifies the name that appears in the Options dialog for the file type.

Under the ocxfile key, you need to create a shell key. Under the shell key,
you add the keys for the register and unregister actions. Use the same technique you used for the dll file type.
Example 7 shows the ocxfile key with all of its components.

Example 7: Creating an ocx file type key
Example 7: Creating an ocx file type key

Using Actions Programmatically

One cool side effect of creating your own file type actions is that you can use them from inside your own programs.
The trick here is to use the ShellExecute function, which allows you to specify a target file and an action without concern for how the action is carried out.

This capability gives you the freedom to change the definition for an action without affecting your program.
It also lets you perform actions against certain types of files using whatever tools are available on the user’s machine.
For example, your program can use the open action against an HTML file on any user’s system,
and the command line defined for that action determines what browser will service the request.

Conclusion

Hopefully you now have a better feel for what Explorer is up to when you right-click on a file and select an action from the popup menu.
All the information Explorer needs is stored right in the registry where you can get at it.
Adding your own actions is a piece of cake, now that you know how to do it.

Free Reg/Unreg Download

If fussing around in the registry is not your idea of having fun, you can download a "reg" file (see link below)
that contains all the keys and values you need.

The RegUnreg.zip file contains a single file named RegUnreg.reg. This file contains keys and values that you can import
into the registry on your machine. The registry entries in this file configure your system so you can register and unregister exe, dll,
and ocx files from the popup menu in Explorer.

System Requirements

  • RegUnreg works on 32-bit Windows systems only (Win95/98/NT/ME/XP).
  • RegUnreg invokes RegSvr32.exe, which usually resides in your system directory.
    If your machine has Visual Basic, then RegSvr32.exe should already be available
    to you.

Installation

  1. Download the RegUnreg.zip file.
  2. Use your favorite unzip utility to extract the contents of RegUnreg.zip into a temporary directory.
  3. You can probably just double-click on the file and Windows will load the reg file into your registry for you. If that works, skip over the next step.
  4. Run the RegEdit program and choose Registry, Import Registry File from the menu. Use the Import Registry File dialog to select and open the unzipped RegUnreg.reg file. RegEdit will load RegUnreg.reg into your registry for you. You can then close RegEdit.
  5. Once you load RegUnreg.reg, you can delete the file.