Context Menu Template (for Visual Studio 2008)
The goal of this project is to eliminate the overhead and annoyance involved in writing context menus for Windows. Once you have installed this template (the current version works for Visual Studio 2008), you can create a functional menu handler to base your project on right from the Visual Studio 2008 menus in under a minute. Since the point of this is to eliminate overhead, I will get straight to the installation instructions. If you are interested in other details, consult the notes below and the references that follow.
Installation
- Download the template source.
- Extract the contents and move the entire folder
MenuXtn to the right part of the visual studio C++ appwizards directory. This depends on where you installed visual studio, but with a typical installation it will be something like this:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCWizards\CodeWiz\ATL
Make sure it goes into the ATL subdirectory.
- Take the files
MenuXtn.ico and MenuXtn.vsz (from the root of the .zip archive) and move them to the VCAddClass directory, which will be something like:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCAddClass
- Finally, (and this is perhaps the most disheartening part of the hack) open the file
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCAddClass\VCAddClass.vsdir and then paste into it the following line, on its own line:
MenuXtn.vsz|0|Context Menu|300|Context Menu Hander Shell Extension|0|0|8224|MyMenu
I'm pretty sure you can add this line anywhere in the file, but I think you may need to ensure there is a blank line at the end of the file. I've included a copy of my VCAddClass.vsdir file for your reference. Note: on Vista, the program files directory may require administrative rights to write to it, so run your text editor (e.g. notepad, or visual studio) as an administrator, and open the file from there.
Okay! The worst is over. We should now have the template installed and we can get to the fun part.
Usage
- Open Visual Studio 2008. Note: if you are using Vista, open Visual Studio as an administrator so that the registration step at the end of the build goes through.
- Go to File--->New--->Project from the menus (or hit Ctrl+Shift+N).
- The "New Project" dialog should appear. Under Visual C++, select "ATL Project". Give it a name, (maybe "shellexttest" or something) click OK, and then Finish, keeping all the default settings.
- Now from the Project Menu, select "Add Class..." and another dialog should appear. On the left panel, select Visual C++, and then on the right panel, somewhere near the bottom you should see an item called "Context Menu", which is our template. Select "Context Menu" and click "Add".
- Type an identifier of some sort for the "short name", perhaps something like "test" and the other fields will be populated for you.
- At the bottom, you'll see an entry for the file class- this determines the type of files you want the extension to operate on- it defaults to txtfile (text files). Just leave it alone for this test.
- Click "Finish", and in a few moments, all the code you need will be generated and the project settings will be updated.
- If you have a 64 bit OS, create a build configuration for x64 and select that configuration. Otherwise, proceed to the next step.
- Build the solution (F7). Note that there might be a few compiler warnings because I still use the old-school string manipulation functions.
That's about all there is to it. To test it out, open up explorer and browse around for some text files. When you right click, you ought to see a new menu item that looks like this:

The menu items just echo which file(s) were selected along with the sub-menu that was invoked, but it should be pretty easy to modify my code for your purposes. Just browse the .cpp file for the new class (in our example it would have been named testMenu.cpp). The comments I've put in with each member function briefly explain their role in the shell's context menu interface. If you want to learn more, visit the references. Hopefully this saves you some time.
Notes
- There are a few other options in the add class wizard that I've left in from the ATL template I hacked. I left this here just in case someone who really knows what they are doing wants to take advantage of it, but do so at your own risk- I designed most of the template code to work with the defaults I provided.
- If you want your extension to work on other types of files, you need to know the file class identifier. To figure that out, open the registry, browse around under HKCR\ to find the extension you have in mind, and then look at the default entry. If you want your extension to be invoked for all files, just use "*" for the file class. Update: For some reason, the default value of txtfile persists- for now, you'll have to change the registry script, but it should be straightforward.
- If you at some point want to get rid of this menu extension, do the following: open a command prompt (as an administrator, if you are using Vista) and then type:
regsvr32 /u "[full path to the .dll output by your build]"
You should then see a message box about the result, and subsequently your menu should no longer be visible to the windows shell.
- A tip for testing: Go to windows explorer and then to Tools--->Folder Options--->View and select "Launch folder windows in a separate process". The shell will load your dll when it first displays one of your menus and it will keep a handle to it for the lifetime of the process, as far as I can tell. If there is only one explorer process, this means your dll will not be available for writing until a reboot, and until then all of your builds will fail. For this same reason, don't test your extension out on the desktop window.
References
- If you want to learn more about writing shell extensions, rather than just blindly using my auto-generated code, I would warmly recommend Michael Dunn's articles.
- If you want to know more about hacking Visual Studio templates, look here.