{"record":{"id":"2c50496d2b4a5484","repo":"Flow-Launcher/Flow.Launcher","slug":"invokecommand-failed-with-code-hr-x","errorCode":null,"errorMessage":"InvokeCommand failed with code {hr:X}","messagePattern":"InvokeCommand failed with code (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Plugins/Flow.Launcher.Plugin.Explorer/Helper/ShellContextMenuDisplayHelper.cs","lineNumber":221,"sourceCode":"            contextMenu.QueryContextMenu(hMenu, 0, ContextMenuStartId, ContextMenuEndId, (uint)ContextMenuFlags.Explore);\n\n            var directory = Path.GetDirectoryName(fileName);\n            var invokeCommandInfo = new CMINVOKECOMMANDINFO\n            {\n                cbSize = (uint)Marshal.SizeOf(typeof(CMINVOKECOMMANDINFO)),\n                fMask = (uint)ContextMenuInvokeCommandFlags.Unicode,\n                hwnd = IntPtr.Zero,\n                lpVerb = (IntPtr)(menuItemId - ContextMenuStartId),\n                lpParameters = null,\n                lpDirectory = null,\n                nShow = 1,\n                hIcon = IntPtr.Zero,\n            };\n\n            hr = contextMenu.InvokeCommand(ref invokeCommandInfo);\n            if (hr != 0)\n            {\n                throw new Exception($\"InvokeCommand failed with code {hr:X}\");\n            }\n        }\n        finally\n        {\n            if (hMenu != IntPtr.Zero)\n                DestroyMenu(hMenu);\n\n            if (contextMenu != null)\n                Marshal.ReleaseComObject(contextMenu);\n\n            if (pContextMenu != IntPtr.Zero)\n                Marshal.Release(pContextMenu);\n\n            if (shellFolder != null)\n                Marshal.ReleaseComObject(shellFolder);\n\n            if (pShellFolder != IntPtr.Zero)\n                Marshal.Release(pShellFolder);","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/Flow-Launcher/Flow.Launcher/blob/7fc63b07bbaa10a0f0b2601487913fcba9ed24b0/Plugins/Flow.Launcher.Plugin.Explorer/Helper/ShellContextMenuDisplayHelper.cs#L203-L239","documentation":"Thrown when IContextMenu.InvokeCommand returns a non-zero HRESULT while executing the user-selected verb. Unlike errors 20-22 the HRESULT is included in the message (hex), so the verb, item, and failure code are all observable. The verb is passed as an offset (menuItemId - ContextMenuStartId); a wrong offset or a verb the item does not implement is the usual cause.","triggerScenarios":"The menuItemId was captured from a stale menu built against a different file, so the offset points at the wrong verb; the underlying verb handler (shell extension) returned an error; the verb requires a parent HWND (lpVerb window owner) but hwnd is IntPtr.Zero so a UAC/owner prompt fails; the file was deleted between menu construction and invocation.","commonSituations":"User opened the menu, deleted the file in another app, then clicked the verb; an elevation-requiring verb (e.g. 'run as administrator') fails because the host window is not supplied; a 32-bit shell extension is loaded into a 64-bit process or vice-versa; antivirus blocked the operation and the verb handler returned E_FAIL.","solutions":["Use CMINVOKECOMMANDINFOEX with the verb *name* (lpVerbW) and the CMIC_MASK_UNICODE flag instead of an integer offset to survive menu reordering.","Pass a real HWND (the Flow Launcher main window handle) so prompts and elevation dialogs have an owner.","Translate the common HRESULTs (E_INVALIDARG, E_ACCESSDENIED, CO_E_CLASSSTRING) into user-facing messages.","Rebuild the menu right before invoking if any time has elapsed since GetContextMenuWithIcons was called."],"exampleFix":"// before\nlpVerb = (IntPtr)(menuItemId - ContextMenuStartId),\n...\nhr = contextMenu.InvokeCommand(ref invokeCommandInfo);\nif (hr != 0) throw new Exception($\"InvokeCommand failed with code {hr:X}\");\n\n// after - pass verb by name and own HWND\nvar info = new CMINVOKECOMMANDINFOEX {\n    cbSize = (uint)Marshal.SizeOf(typeof(CMINVOKECOMMANDINFOEX)),\n    fMask = (uint)(ContextMenuInvokeCommandFlags.Unicode | ContextMenuInvokeCommandFlags.FlagNoUi),\n    hwnd = mainWindowHandle,\n    lpVerbW = verbName,\n    nShow = 1,\n};\nhr = contextMenu.InvokeCommand(ref Unsafe.As<CMINVOKECOMMANDINFOEX, CMINVOKECOMMANDINFO>(ref info));\nif (hr != 0) throw new COMException($\"InvokeCommand failed for verb '{verbName}'\", hr);","handlingStrategy":"try-catch","validationCode":"if (menuItemId < ContextMenuStartId || menuItemId > ContextMenuEndId)\n    throw new ArgumentOutOfRangeException(nameof(menuItemId));","typeGuard":"static bool IsValidMenuId(uint id) => id >= 0x0001 && id <= 0x7FFF;","tryCatchPattern":"try { ShellContextMenuDisplayHelper.ExecuteContextMenuItem(fileName, menuItemId); }\ncatch (Exception ex) when (ex.Message.StartsWith(\"InvokeCommand failed\"))\n{\n    var hr = ParseHResultFromMessage(ex.Message);\n    App.API.ShowMsgError($\"Action could not be performed (0x{hr:X}).\");\n}","preventionTips":["Invoke verbs by name (CMINVOKECOMMANDINFOEX.lpVerbW), not by integer offset.","Pass a real HWND so elevation/owner prompts work.","Rebuild the menu right before invoking to avoid stale offsets.","Map common HRESULTs to user-facing messages."],"tags":["win32","shell-com","context-menu","invokecommand","csharp"],"backgroundTag":null,"analyzedSha":"7fc63b07bbaa10a0f0b2601487913fcba9ed24b0","analyzedAt":"2026-08-13T14:54:20.914Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}