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.

Comments (12)
  1. kog999 says:

    "We never did hear back from the customer, so the world may never know whether my psychic prediction was correct."

    I think when you never hear back from a customer you can assume that your psychic predictions were correct.

  2. jader3rd says:

    I wish that Bonus chatter existed in more documentation. It always seems like there's four api's for accomplishing something, and odds are I'm going to pick the overly complicated one.

  3. Maurits says:

    (Making the dangerous assumption that the conversation was verbatim as reported)

    Did you expect a response?

    They asked a question; you answered it. If you want to hear back, I would suggest stating your answer in the form of a question.

    Exempli gratia: "Did you give your menu item the right ID, and are you returning the right value from IContextMenu::QueryContextMenu?"

    [Conversations are rarely reported verbatim here; I need to strip out identifying information and disguise what's left. But even if it were, wouldn't a "Thanks, the customer says that worked" or even just a "Thanks" be expected? -Raymond]
  4. Antonio Rodríguez says:

    @jader3rd: the probability of using the over-complicated solution, ironically, is even greater if you do your homework and google the answer: there are many blogs and programming sites that explain the complicated (and, to be fair, more general) way of solving a problem, instead of the simple one that works most of the time and requires half the code. It's the same as recommending Nero burning ROM, with its ten-page disk burning wizard capable of mixed CD-DA/CD-ROM disks and other authoring extravagances, to a friend who only needs to backup his Documents folder.

  5. Joshua says:

    [Conversations are rarely reported verbatim here; I need to strip out identifying information and disguise what's left. But even if it were, wouldn't a "Thanks, the customer says that worked" or even just a "Thanks" be expected? -Raymond]

    Yes. I originally thought that its missing was due to an artifact of such messages not being relayed back to the engineers. Now I know better.

  6. kog999 says:

    "But even if it were, wouldn't a "Thanks, the customer says that worked" or even just a "Thanks" be expected? -Raymond"

    Chances are the customer liaison was just as in the dark about the accuracy of your psychic predictions as you were. They probably never heard back from the customer either.

  7. Maurits says:

    Raymond, I want to personally thank you on behalf of all the customers who have not. You're a brilliant individual with phenomenal powers of software problem diagnosis.

  8. Joshua says:

    I have taken to using your phrase "Psychic debugging"

  9. Danny says:

    @Antonio Rodríguez

    According to your own advice you would not recommend Windows either as OS since 70% of it's capabilities are not used by more then 50% of the population. Heck, you would recommend DOS for that matter.

  10. JK says:

    I originally parsed the sentence beginning "If the menu IDs do not lie.." as 'If the menu IDs do not tell a false hood..', rather than 'If the menu IDs do not exist in the range..' which of course give the sentence almost exactly the opposite meaning. I find these sort of ambiguities in natural language fascinating.

  11. 640k says:

    For me "Psychic debugging" is to notice, and remember, strange behavior. And later put that knowledge to use. Naive and sloppy people are often stunned by how you could know stuff, which they could have learned themselves if they were less ignorant.

  12. I originally parsed the sentence beginning "If the menu IDs do not lie.." as 'If the menu IDs do not tell a false hood..',

    Double meaning of the word 'lie' also happens in other languages. In Russian, these words are related. In German, these are also pretty close.

Comments are closed.