Sure, we do that: Context menu edition

Raymond Chen

A customer reported a problem that occurred only when they installed a particular application. If they uninstalled it, then the problem went away. After installing the application, the “Run As” context menu option stopped working. The customer didn’t provide any other details, but we were able to make an educated guess as to what was going on. A common programming error in context menu extensions occurs in extensions which add only one menu item. These extensions ignore the parameters to the IContextMenu::InvokeCommand and simply assume that the only reason the method can be called is if the user selected their menu item. After all, if you have only one invokable item, there’s no need to figure out which one the user selected, because you have only one to begin with! The problem is that a context menu extension can be invoked not because the user selected an item under its control but because a verb is being invoked programmatically, and each handler is being asked, “Do you know how to do this?” The result is that the context menu host calls the extension to say, “If you know how to do runas, then please do so,” and the the extension says “Sure, we do that” and starts doing its thing. If you are unlucky and the grabby extension is asked the question before the actual runas extension, the runas command winds up being hijacked by the grabby extension. (This is the same mistake that causes the Copy To and Move To commands to behave strangely if you add them to the context menu: They assume that the only reason they are invoked is that the user invoked their command, because they weren’t designed to be hosted by context menus to begin with! They were designed to go into the toolbar, and the toolbar hosting code never invoked commands by name. It’s like taking a ladder and using it as a bridge between two tall buildings. Sure, you can now cross from one building to another, but you also run a serious risk of falling to your death.) A variation on the initial problem is “I found that after installing a particular program, I can’t run anything from the Start menu.” I know of at least two programs which install context menu extensions which steal the “open” command on executables. This problem is sufficiently prevalent that there is a special compatibility flag that can be set on a shell extension to say, “This is a grabby shell extension that steals commands. Never ask it if it supports anything, because it will always say yes!”

Notice that the “MoveTo CopyTo Context Menu” is on the list, which I find interesting because MoveTo/CopyTo was never meant to go on the context menu in the first place. Going back to our analogy, it’d be as if the ladder company issued a safety bulletin to warn people of problems that can occur if you use it as a bridge between two tall buildings!

0 comments

Discussion is closed.

Feedback usabilla icon