Homepage image
Sample of the Day RSS Feed

Sample Downloads: http://code.msdn.microsoft.com/CppShellExtContextMenuHandl-410a709a 

Today’s sample demonstrates demonstrates creating a Shell context menu handler with C++.

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage http://1code.codeplex.com/.

 

Introduction

The code sample demonstrates creating a Shell context menu handler with C++.

A context menu handler is a shell extension handler that adds commands to an existing context menu. Context menu handlers are associated with a particular file class and are called any time a context menu is displayed for a member of the class. While you can add items to a file class context menu with the registry, the items will be the same for all members of the class. By implementing and registering such a handler, you can dynamically add items to an object's context menu, customized for the particular object.
 
Context menu handler is the most powerful but also the most complicated method to implement. It is strongly encouraged that you implement a context menu using one of the static verb methods if applicable:
 http://msdn.microsoft.com/en-us/library/dd758091.aspx

The example context menu handler has the class ID (CLSID):
   {BFD98515-CD74-48A4-98E2-13D209E3EE4F}
 
It adds the menu item "Display File Name (C++)" with icon to the context menu when you right-click a .cpp file in the Windows Explorer. Clicking the menu item brings up a message box that displays the full path of the .cpp file.

Setup and Removal

A. Setup
 
If you are going to use the Shell extension in a x64 Windows system, please configure the Visual C++ project to target 64-bit platforms using project configurations (http://msdn.microsoft.com/en-us/library/9yb4317s.aspx).'>http://msdn.microsoft.com/en-us/library/9yb4317s.aspx). Only
64-bit extension DLLs can be loaded in the 64-bit Windows Shell.

If the extension is to be loaded in a 32-bit Windows system, you can use the default Win32 project configuration to build the project.
 
In a command prompt running as administrator, navigate to the folder that contains the build result CppShellExtContextMenuHandler.dll and enter the command:
 
   Regsvr32.exe CppShellExtContextMenuHandler.dll
 
The context menu handler is registered successfully if you see a message box saying:
 
   "DllRegisterServer in CppShellExtContextMenuHandler.dll succeeded."
 
B. Removal
 
In a command prompt running as administrator, navigate to the folder that contains the build result CppShellExtContextMenuHandler.dll and enter the command:
 
   Regsvr32.exe /u CppShellExtContextMenuHandler.dll
 
The context menu handler is unregistered successfully if you see a message box saying:
 
   "DllUnregisterServer in CppShellExtContextMenuHandler.dll succeeded."
 

Running the Sample

The following steps walk through a demonstration of the context menu handler code sample.
 
Step1. If you are going to use the Shell extension in a x64 Windows system, please configure the Visual C++ project to target 64-bit platforms using project configurations http://msdn.microsoft.com/en-us/library/9yb4317s.aspx.  Only 64-bit extension DLLs can be loaded in the 64-bit Windows Shell.

If the extension is to be loaded in a 32-bit Windows system, you can use the default Win32 project configuration.
 
Step2. After you successfully build the sample project in Visual Studio 2010, you will get a DLL: CppShellExtContextMenuHandler.dll. Start a command prompt as administrator, navigate to the folder that contains the file and enter the command:
 
   Regsvr32.exe CppShellExtContextMenuHandler.dll
 
The context menu handler is registered successfully if you see a message box saying:
 
   "DllRegisterServer in CppShellExtContextMenuHandler.dll succeeded."
 
Step3. Find a .cpp file in the Windows Explorer (e.g. FileContextMenuExt.cpp in the sample folder), and right click it. You would see the "Display File Name (C++)" menu item with icon in the context menu and a menu seperator below it. Clicking the menu item brings up a message box that displays the full path of the .cpp file.
 
The "Display File Name (C++)" menu item is added and displayed when only one .cpp file is selected and right-clicked. If more than one file are selected in the Windows Explorer, you will not see the context menu item.
 
Step4. In the same command prompt, run the command

   Regsvr32.exe /u CppShellExtContextMenuHandler.dll
 
to unregister the Shell context menu handler.
 

More Information

MSDN: Initializing Shell Extensions
 http://msdn.microsoft.com/en-us/library/cc144105.aspx
 
MSDN: Creating Context Menu Handlers
 http://msdn.microsoft.com/en-us/library/bb776881.aspx
 
MSDN: Implementing the Context Menu COM Object
 http://msdn.microsoft.com/en-us/library/ms677106.aspx
 
MSDN: Extending Shortcut Menus
 http://msdn.microsoft.com/en-us/library/cc144101.aspx
 
MSDN: Choosing a Static or Dynamic Shortcut Menu Method
 http://msdn.microsoft.com/en-us/library/dd758091.aspx'>http://msdn.microsoft.com/en-us/library/dd758091.aspx
 
The Complete Idiot's Guide to Writing Shell Extensions
 http://www.codeproject.com/KB/shell/shellextguide1.aspx
 http://www.codeproject.com/KB/shell/shellextguide2.aspx
 http://www.codeproject.com/KB/shell/shellextguide7.aspx
 
How to Use Submenus in a Context Menu Shell Extension
 http://www.codeproject.com/KB/shell/ctxextsubmenu.aspx