Shell Namespace Extension: Adding Custom Command Module Items
Overview
In my earlier post, I showed how to create a Shell Namespace Extension using the default IShellView implementation (also known as DefView). One thing you might have noticed from the sample is that the Commands Module is void of even the default items such as “Organize” and “Views”.
If you are developing your own Namespace and want to include the default buttons and/or add your own elements to the Commands Module as well, how do you accomplish this? The answer: you must implement the IExplorerCommand, IExplorerCommandProvider and IEnumExplorerCommand interfaces.
Below I describe what each of the interfaces is used for and what you need to do to properly implement them in your code.
IExplorerCommandProvider
After your Namespace is loaded, the Shell will ask your Namespace for an instance of an IExplorerCommandProvider. This interface exposes two methods: GetCommand and GetCommands. The Shell calls the GetCommand method to retrieve a specific IExplorerCommand object that matches the supplied GUID (Each command is identified by a GUID). The GetCommands method is called to retrieve an IEnumExplorerCommand instance. This object enumerates all the IExplorerCommand instances for the namespace.
You Namespace is queried for its implementation of IExplorerCommandProvider via its IShellFolder's implementation of CreateViewObject. The REFIID passed to CreateViewObject will be IID_IExplorerCommandProvider. The Shell will then use this interface to retrieve your Commands Module items – which are implementations of the IExplorerCommand interface.
IExplorerCommand
The IExplorerCommand interface is used to provide the appearance and behavior of the item to Explorer’s Commands Module. Most of the methods pass an IShellItemArray which identifies what items are currently selected in the view. This information can be used by the implementation to customize the behavior for different selections or if no items are selected. For example, if no items are selected, you may wish to set the state to ECS_DISABLED or ECS_HIDDEN to grey-out or remove the item completely from the Commands Module, respectively.
The methods exposed by this interface are described below.
|
Method |
Description |
|
EnumSubCommands |
Returns an IEnumExplorerCommand instance used to enumerate sub commands of the current command. The Shell will only call this method if the GetFlags method returns ECF_HASSUBCOMMANDS . It is important to note here that while it is possible to do so, sub commands that have sub commands themselves are not encouraged. |
|
GetCanonicalName |
Retrieves the globally unique identifier (GUID) associated with this command |
|
GetFlags |
Retrieves the flags associated with this command. These flags specify the appearance and behavior of the command on the Command Module. |
|
GetIcon |
Retrieves an icon resource string. This string is in the form of “myfile.dll,id”. For example: “shell32.dll,-101” |
|
GetState |
Retrieves the state of the command item. This is the first method of the interface that is called. The state that is returned affects the appearance and behavior of the command item. This method also passes a BOOL to let the command implementation know if the slow response rule is in effect. |
|
GetTitle |
Retrieves the string to display for the command item. |
|
GetToolTip |
Retrieves the string to use in the ToolTip associated with this command item. |
|
Invoke |
Called by the Shell when the user has activated a command on the Commands Module. |
The above methods GetState and GetFlags return values that determine the appearance and behavior of the item in the Command Module. In the table below are screenshots of the command resulting from the combinations of the flags/states values.
|
|
ECS_ENABLED |
ECS_DISABLED |
ECS_HIDDEN |
|
NONE |
|
|
|
|
ECF_HASSUBCOMMANDS |
|
|
|
|
ECF_HASSPLITBUTTON |
|
|
|
|
ECF_HIDELABEL |
|
|
|
|
ECF_ISSEPARATOR |
|
N/A |
|
|
ECF_HASLUASHIELD |
|
|
|
|
|
ECS_CHECKBOX |
ECS_CHECKBOX| ECS_CHECKED |
|
NONE |
|
|
|
ECF_HASSUBCOMMANDS |
|
|
|
ECF_HASSPLITBUTTON |
|
|
|
ECF_HIDELABEL |
|
|
|
ECF_ISSEPARATOR |
N/A |
N/A |
|
ECF_HASLUASHIELD |
|
|
*ECS_CHECKBOX and ECS_CHECKED only apply to sub items
** ECF_ISSEPARATOR can only be added as a sub item
Slow Response Rule
The second parameter of the GetState method is a BOOL (fOkToBeSlow) that lets the implementation of IExplorerCommand know if the “slow response rule” is in effect. If this value is FALSE, and your implementation performs slow operations (such as I/O, network calls or calls to out of thread COM objects) then your command should return E_PENDING. This will cause the Commands Module to call the command’s GetState (and other methods) on a background thread with fOkToBeSlow set to TRUE. This prevents slow operations in your implementation from running on the UI thread, thus preventing performance issues.
Command Item Ordering
It is important to note here that the developer of the Namespace Extension has no control over the ordering of command items. For example, you cannot force your command items before the default View and Organize command items or have the View or Organize command items renamed/removed. Items are simply appended to the end of the Commands Module in the order returned from the IEnumExplorerCommand instance.
IEnumExplorerCommand
The IEnumExplorerCommand is returned to the Shell by the GetCommands method of the IExplorerCommandProvider interface. As the name suggests, it permits enumeration of the IExplorerCommand instances by the Shell.
FolderViewImpl Sample Code
The above interfaces have been implemented in the attached Shell Namespace Extension sample code derived from the FolderViewImpl SDK sample. The structure of the command items (as well as sub items) is data-driven from an array of structures that define the command items (see the array of FVCOMMANDITEMs in fvcommands.cpp). This approach was used to make it easy to experiment with the hierarchy of command items as well as their behavior/appearance. The way you implement commands in your Namespace Extension may differ.
The above is a screenshot of the modified SDK sample. Notice we now have the default command items (Organize and Views) as well as our custom items. The Display command item performs the same function as right-clicking items in the view and selecting “Display” from the context menu. The Settings button is used to demonstrate a command item with sub items. All it does is display a message box with the name of the sub item when the user invokes the specified sub item.
It should also be noted that if you are using a custom view in your Namespace by implementing your own IShellView (instead of using DefView) you will need to include an implementation of IFolderView::GetFolder. In your GetFolder implementation you will need to include the ability to QueryService for SID_SFolderView.
Building the FolderView SDK Sample
- To build the FolderViewImpl sample, be sure to download and install the Windows SDK.
- Download the modified FolderView SDK sample
- Launch FolderViewImpl.sln in Visual Studio
- Open the properties for the project
- Add a path to the SDK includes to the C/C++ - General page
- Add a path to the SDK libs to the Linker – General page
- Build
Installing the FolderView SDK Sample
- Once you have built the sample, copy the FolderViewImpl.dll and FolderViewImpl.propdesc to the same directory
- From an elevated cmd window, regsvr32 FolderViewImpl.dll
- Restart explorer
- Open explorer to Computer
- There should be a list item named “FolderView SDK Sample”
Written by chrdavis. Read more great feeds at is source WEBSITE
1 comment.
Read more articles on shell and namespace and Coding and API and Programming and vista and Windows Vista.
- [+] Digg: Feature this article
- [+] Del.icio.us: Bookmark this article
- [+] Furl: Bookmark this article


#1. August 4th, 2009, at 6:05 AM.
Hi
I was wondering if you can upload the sample to different site as ShellRevealed is out of order for quite some time now.
Also the images does not load anymore which I think is also because they were attached to a deprecated photo sharing server or something….
I hope someone reads this comment as this article as important as it is seems dead