I'd like to create a dynamic verb (menu item) for the Windows shell context/shortcut menu. I want it to appear only for items under some (configurable) paths, and I would like to execute some code that communicates with a process through a named pipe.
How do I best achieve that nowadays?
In the documentation, they list the preferred dynamic verb methods:
Preferred Dynamic Verb Methods
The following dynamic verb methods are preferred:
Verb type Description Static verb (listed in the previous table) + Advanced Query Syntax (AQS) This choice gets dynamic verb visibility. Windows 7 and later: IExplorerCommandThis choice enables a common implementation of verbs and explorer commands that are displayed in the command module in Windows Explorer. Windows 7 and later: IExplorerCommandState+ static verbThis choice also gets dynamic verb visibility. It is a hybrid model where a simple in-process handler is used to compute if a given static verb should be displayed. This can be applied to all of the static verb implementation methods to achieve dynamic behavior and minimize the exposure of the in-process logic. IExplorerCommandStatehas the advantage of running on a background thread, and thereby avoids UI hangs. It is considerably simpler thanIContextMenu.
They go on to discourage implementing IContextMenu:
Discouraged Dynamic Verb Methods
IContextMenuis the most powerful but also the most complicated method to implement. It is based on in-process COM objects that run on the thread of the caller, which usually Windows Explorer but can be any application hosting the items.IContextMenusupports verb visibility, ordering, and custom drawing. Some of these features have been added to the static verb features, such as an icon to be associated with a command, andIExplorerCommandto deal with visibility.
Later in the documentation, they have a page about "Customizing a Shortcut Menu Using Dynamic Verbs" which only treats with the IContextMenu family, the one that was discouraged above, and without any mention of the aforementioned encouraged methods.
Lately, Microsoft has also communicated that IExplorerCommand will be required in Windows 11 for an item to appear in the new context menu.
I have tried implementing IExplorerCommand, for which documentation is very poor, and here are my notes:
It seems to require a static verb (registered in
shell, notshellex), which it will modify.As a modifier of a static verb, it can change the title, the icon, and whether the item is grayed out or not, but it doesn't allow to hide the item (
ECS_HIDDENonly grays out the item).
Is there really no way of hiding the menu item with IExplorerCommand?
Could IExplorerCommandProvider prevent from having to use a static verb? If so, how should it be implemented?
There is practically no information available on that interface. All I've found is a sample where it is implemented as an object that is returned to IShellFolder::CreateViewObject(). I haven't dug further, but IShellFolder seems tedious to implement, and I don't how such an implementation would be registered. Anyway, that sample also implements IContextMenu.
How would multiple commands be handled by a single DLL with IExplorerCommand?
What is the current stance on using .NET (Core/5/6) for creating shell extensions? Is it still discouraged to use such runtimes for inproc servers?