Flow-Launcher/Flow.Launcher · error · ShellContextMenuException

Failed to get the desktop shell folder

Error message

Failed to get the desktop shell folder

What it means

Thrown as ShellContextMenuException when SHGetDesktopFolder(out pUnkownDesktopFolder) returns anything other than S_OK on first access, meaning the Windows shell's desktop IShellFolder could not be obtained. The desktop folder is the root of the shell namespace and is required to build any context menu; without it the entire shell-context-menu feature cannot operate.

Source

Thrown at Plugins/Flow.Launcher.Plugin.Explorer/Helper/ShellContextMenu.cs:228

        #endregion

        #region GetDesktopFolder()

        /// <summary>
        /// Gets the desktop folder
        /// </summary>
        /// <returns>IShellFolder for desktop folder</returns>
        private IShellFolder GetDesktopFolder()
        {
            IntPtr pUnkownDesktopFolder = IntPtr.Zero;

            if (null == _oDesktopFolder)
            {
                // Get desktop IShellFolder
                int nResult = SHGetDesktopFolder(out pUnkownDesktopFolder);
                if (S_OK != nResult)
                {
                    throw new ShellContextMenuException("Failed to get the desktop shell folder");
                }
                _oDesktopFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(pUnkownDesktopFolder, typeof(IShellFolder));
            }

            return _oDesktopFolder;
        }

        #endregion

        #region GetParentFolder()

        /// <summary>
        /// Gets the parent folder
        /// </summary>
        /// <param name="folderName">Folder path</param>
        /// <returns>IShellFolder for the folder (relative from the desktop)</returns>
        private IShellFolder GetParentFolder(string folderName)
        {

View on GitHub (pinned to 7fc63b07bb)

Solutions

  1. Run 'sfc /scannow' and 'DISM /Online /Cleanup-Image /RestoreHealth' to repair shell/system files.
  2. Disable recently-installed shell extensions (shell extension managers, context-menu adders) and retry to isolate a faulty one.
  3. Ensure the code runs in an interactive user session with Explorer running.
  4. Catch ShellContextMenuException at the caller and degrade to a basic menu or a Process.Start fallback.
  5. If persistent, create a new user profile to rule out per-profile shell corruption.

Example fix

// before
int nResult = SHGetDesktopFolder(out pUnkownDesktopFolder);
if (S_OK != nResult)
    throw new ShellContextMenuException("Failed to get the desktop shell folder");

// after — include HRESULT and free the partial pointer
int nResult = SHGetDesktopFolder(out pUnkownDesktopFolder);
if (S_OK != nResult)
{
    if (pUnkownDesktopFolder != IntPtr.Zero) Marshal.Release(pUnkownDesktopFolder);
    throw new ShellContextMenuException(
        $"Failed to get the desktop shell folder (HRESULT 0x{nResult:X})");
}
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try { var menu = new ShellContextMenu(); menu.ShowContextMenu(...); }
catch (ShellContextMenuException ex) { Log.Warn(...); // fall back to a basic menu }

Prevention

When it happens

Trigger: The shell is in a broken state (Explorer crashed/corrupt shell registration); the call is made from a context without a valid desktop session (e.g. a service session, or before shell initialization); a shell extension registered for the desktop namespace crashed during initialization causing SHGetDesktopFolder to fail; insufficient privileges to access the shell namespace.

Common situations: Explorer/shell corruption requiring 'sfc /scannow' or a user-profile rebuild; a buggy third-party shell extension loaded into the desktop namespace failing its initialization; running the code in a non-interactive/session-0 context; severe registry corruption of shell namespace keys.

Related errors


AI-assisted analysis of Flow-Launcher/Flow.Launcher@7fc63b07bb (2026-08-13). Data as JSON: /api/errors/f7d8b13590d41d17. Report an issue: GitHub.