·5 min read
My right-click menu came up empty: pywin32 has no IContextMenu3
A fake desktop gives itself away in about a minute, and the right-click menu is how. A hand-made menu with Open, Copy and Delete looks wrong next to the real one, which has Send to, Open with, New and whatever your installed apps added. I wanted the real menu, so DiskDesk asks the Windows shell to build it. In Python that request goes through a COM interface, and the Python bindings stop one step short of what the shell needs.
The history here is thin and I will say so up front: the repository has four commits, all on Sep 2, 2026, and the menu code arrived whole in the first one. So this story is told from the code and its own comments, not from a trail of attempts.
- Project
- DiskDesk
- Date
- Sep 2026
- Stack
- Python, ctypes, pywin32, COM
- Commit
- 1f46004
What broke
The first interface, IContextMenu, is enough to fill a menu with its top level items: you ask the shell for a folder's menu, it adds entries to a menu handle, and you invoke whichever one the user picks. That part works with pywin32.
What it does not give you is the submenus. New, Send to, Open with and every shell extension that builds its menu on demand do not fill themselves when the menu is created. They wait for a window message, WM_INITMENUPOPUP, to say that the submenu is about to open, and they expect to get it through IContextMenu2 or IContextMenu3. Owner-drawn items also need WM_DRAWITEM and WM_MEASUREITEM, and keyboard shortcuts need WM_MENUCHAR. The docstring at the top of shellmenu.py states what happens without them: the dynamic entries come up empty.
pywin32 has IContextMenu and nothing for the other two. There was no wrapper to import, so the choice was to give up on the real menu or go below the bindings.
What the code showed
COM objects are tables of function pointers. A pointer to an interface points at an object whose first field points at the vtable, and each method is a slot in that table. IUnknown takes slots 0 to 2, IContextMenu adds QueryContextMenu, InvokeCommand and GetCommandString at 3 to 5, IContextMenu2 adds HandleMenuMsg at 6, and IContextMenu3 adds HandleMenuMsg2 at 7. The module's constants say exactly that:
_QUERY_INTERFACE, _RELEASE = 0, 2
_CM_QUERY, _CM_INVOKE, _CM_GET_STRING = 3, 4, 5
_CM2_HANDLE_MENU_MSG, _CM3_HANDLE_MENU_MSG2 = 6, 7So ctypes can do what the missing wrapper would have done. A small ComPtr class reads the vtable, builds a function type for a slot, and calls it with the object pointer as the first argument:
def _slot(self, index: int, restype, argtypes):
vtable = ctypes.cast(self.ptr, POINTER(POINTER(c_void_p)))[0]
return ctypes.WINFUNCTYPE(restype, c_void_p, *argtypes)(vtable[index])ContextMenu then asks the base interface for the two newer ones with QueryInterface, and keeps whichever the shell object supports.
A second trap sits next to this one, and the comment on it is blunt: pointer-returning shell calls must be declared, or ctypes truncates 64-bit PIDLs to a plain int and the shell reads a garbage address. So SHParseDisplayName, ILFindLastID and ILFree all have explicit argtypes and restype set at the top of the file.
The fix
Calling the slots was half of it. The shell fills submenus by sending messages to the window that owns the popup, so something has to receive those messages and pass them on. handle_menu_msg is where they go. It tries IContextMenu3 first and falls back to IContextMenu2:
if self.cm3 is not None:
...
hr = self.cm3.call(_CM3_HANDLE_MENU_MSG2, c_long,
[c_uint, ctypes.c_size_t, ctypes.c_ssize_t,
POINTER(ctypes.c_ssize_t)],
msg, wparam, lparam, byref(result))
if hr == S_OK:
return result.value
if self.cm2 is not None:
hr = self.cm2.call(_CM2_HANDLE_MENU_MSG, ...)The second half is a window. DiskDesk's own windows belong to Qt, and the menu code does not use them for this. _host_window registers a tiny window class, DiskDeskShellMenuHost, and creates one hidden window from it. Its window procedure forwards WM_INITMENUPOPUP, WM_DRAWITEM, WM_MEASUREITEM and WM_MENUCHAR to whichever menus are currently active, and hands everything else to DefWindowProc. popup makes that hidden window the owner of TrackPopupMenu, so the shell's messages arrive where the forwarder is waiting.
The menu is rebuilt on every right-click, so an app installed a minute ago appears without a restart. Two menus are merged, the target folder's own and the entries only the real desktop offers, and folder-scoped verbs such as New and Paste are never taken from the desktop menu. If they were, Paste would act on the user's real Desktop folder instead of the DiskDesk one. If the menu cannot be built at all, popup returns -1 and the caller falls back to a Qt menu.
What I would do differently
I would test this part against more than my own machine. The whole design depends on shell extensions behaving, and they are other people's code running in my process. handle_menu_msg returns None when neither interface answers, which is the right behavior, but I have not run it against every extension that adds a menu.
I would also write down the slot numbers with their source. The constants are correct, but a reader has to trust me, and the real value is in the interface definitions in the Windows headers. A comment with the header name next to each group would make the next person's review a two-minute job.
And I would put the vtable layer in its own module. ComPtr is generic, and it sits in a file about menus. thumbs.py also makes COM calls through ctypes, so one small shared COM helper would have been cleaner than a helper that grew inside a menu file.
Where this fits
This is the hardest single piece of DiskDesk, and it follows the rule the whole project is built on: where Windows has an API, call it. The full DiskDesk case study covers thumbnails, file operations through the shell, the updater and the release workflow around it.
See how the rest of DiskDesk leans on the Windows shell, from thumbnails to silent updates.
Read the full case study#python#com#ctypes#windows
Keep reading
Have a project in mind?