Easy - Reg/Unreg DLL for VB6
The next thing that would happen in the Windows explorer is probably you clicking on one of the menu items. Let's use Open as an example. Go back to the Registry Editor and expand the Open subkey under shell. You'll see another subkey called command. If you click on the command subkey, you'll see that the (Default) value has a value. If you double-click (Default), the Edit String dialog will open. If you're using the default text file editor installed by Windows, you'll see Notepad.exe somewhere in the string value. That means that Windows will use the Notepad.exe application to open files with the .txt extension. But how does Windows know what file to open? Well, the other thing you'll see, along with notepad.exe, is %1. %1 is a token that will be replaced on the fly with the name of the file you right-clicked on. So, let's say you were in the root on your C drive and you right-clicked on a file called foo.txt. When you click the Open item, the shell launches notepad passing c:\foo.txt on the notepad command line, which instructs notepad to open the file. That's all there is to it! Take a quick look at the print subkey under shell. This item provides the command line for printing text files. You see how it's done. The Shell calls notepad and passes a /p and %1 (the file name) on the command line. The /p instructs notepad to print the file it opens. Piece of cake right?
But Why?
By now, you're probably wondering what all this has to do with VB. Well, I use this information to help me work with files that we VBers typically work with. Let's say you create quite a few DLLs in you current assignment. If you do, you know that quite often, you find yourself registering and unregistering DLLs while you test. You may open a DOS box or you may open the Run dialog, but you end up somewhere typing in
regsvr32 C:\the very long\path to a DLL\I wrote in VB\yesterday\foo.dll
What a drag eh? Well, never again. What did we just learn? How to get the Shell to execute a command for us, based on the file extension. Ah, I see the lights coming on. Piece of cake right? Open the Registry Editor (if it isn't still open from before) and navigate your way to
HKEY_CLASSES_ROOT\dllfile
If there isn't already a subkey called shell under dllfile, create one. Under shell, create two new subkeys; Register and Unregister. Create a command subkey under each of these. In the (Default) value for the shell\Register\command subkey, enter
regsvr32 "%1"
For the shell\Unregister\command (Default) value, enter
regsvr32 -u "%1"
These command lines, as I'm sure you know, register and unregister COM DLLs. To try them out, open the Windows Explorer and locate one of your VB DLLs. Right click on it and, if we've done things right, Register and Unregister will appear in the context menu. Click Register and you'll see the familiar DllRegisterServer...succeeded message. You did it! You'll never have to type that command out again.
Other Ideas
Now that you have the basic idea, what else could you do? Well, I often find myself opening project files or forms in Notepad to look through them for something when I don't really feel like opening a new instance of VB. So what I did was add a new shell subkey for all my VB-related files like .cls, .frm, .vbp, and .vbg. As a quick refresher:
- navigate to HKEY_CLASSES_ROOT\.cls (for example)
- you'll see the (Default) value says VisualBasic.ClassModule
- navigate to HKEY_CLASSES_ROOT\VisualBasic.ClassModule\shell
- add a new subkey called Open with Notepad
- add a subkey under this called command
- set the (Default) value to
notepad.exe "%1"
You Can Take It From Here
You can apply this same set of steps to just about any file type in your system. Then, once you've added all the necessary subkeys and commands, you can just right click a file to get at your most commonly used commands! If you ever need to remove one of the commands you added, just delete the subkey under shell and it will go away.
This week's article may not be directly related to VB, but it is directly related to your productivity. The faster you can get things done, the more productive you'll be. It doesn't matter whether it's coding that new DLL, or registering it and unregistering quickly while you're testing; both add to your productivity!
Original
And Now, For Something a Little Different