February 23rd, 2018

How do I make sure that my shell extension is at the top of the context menu?

A customer wanted the items for their context menu shell extension to appear at the very top of the context menu. They did this by ignoring the recommended insertion point passed to the IContext­Menu::Query­Context­Menu function and just inserting their items at the top anyway:

HRESULT MyContextMenu::QueryContextMenu(
    HMENU hmenu, UINT indexMenu,
    UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
{
    ...
    // Insert at the top (position 0), ignoring the indexMenu
    InsertMenuItem(hMenu, 0, TRUE, &mii);
    ...
}

However, they found that this didn’t work. Their context menu item showed up in the middle.

Well, sure, their context menu extension broke the rules and put their menu item at the top, but that just gets them to the top of the context menu so-far. But there are other shell extensions, and they may end up going on top of yours. And of course there’s the shell itself, which has final say over where things go, and it might decide to put things on top of yours as well.

At the end of the day, it’s the context menu host that decides where the menu items go. If you break the rules, you may be able to trick the host for a little while, but you’re living on borrowed time.

(And of course there’s also the question “What if two programs did this?”

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.