July 27th, 2012

Psychic debugging: Why your IContextMenu::InvokeCommand never gets called

A customer reported a problem with their shell context menu extension.

I have implemented the IContext­Menu shell extension, but when the user selects my custom menu item, my IContext­Menu::Invoke­Command is never called. Can anyone please let me know what the problem could be and how to fix it?

Since there really isn’t much information provided in this request, I was forced to invoke my psychic powers. Actually, given what you know about shell context menu hosting, you probably know the answer too.

My psychic powers tell me that you gave your menu item the wrong ID, or you returned the wrong value from IContext­Menu::Query­Context­Menu.

If the menu IDs do not lie in the range you described by the return value from IContext­Menu::Query­Context­Menu, then when the user chooses the menu item, the item ID will not map to your shell extension. In our sample composite context menu, observe that CComposite­Context­Menu::Reduce­Ordinal relies on the component context menu handlers putting their menu IDs in the range idCmd­First through idCmd­First - return_value - 1. If the two don’t line up, then CComposite­Context­Menu::Reduce­Ordinal won’t realize that the menu item the user selected corresponds to you. We never did hear back from the customer, so the world may never know whether my psychic prediction was correct.

Bonus chatter: When possible, use a static verb registration instead of an IContext­Menu handler. They are much simpler to implement while still providing a good amount of expressive power.

You can provide additional information in your registration to control things like the conditions under which your verb should be shown. You can even register cascading submenus statically.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.