Skip to content

BUG: log spam from COM exception #3812

@dcog989

Description

@dcog989

Checks

  • I have checked that this issue has not already been reported.

  • I am using the latest version of Flow Launcher.

  • I am using the prerelease version of Flow Launcher.

Problem Description

My logs are being spammed with:

Exception full name: System.Runtime.InteropServices.COMException
Error status: UNKNOWN
Class name: IShellLinkW
Calling method: retrieveTargetPath
Program path: C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Windows Media Player Legacy.lnk
InnerException number: 1
Exception message: Error HRESULT E_FAIL has been returned from a call to a COM component.
Exception error type: HResult -2147467259
Exception thrown in called method: Void GetDescription(Windows.Win32.Foundation.PWSTR, Int32)
Possible interpretation of the error: Error caused likely due to trying to get the description of the program
Possible resolution: Not yet known
01:44:34.6510+01:00 - ERROR -  - ------------- END Flow.Launcher.Plugin.Program exception -------------
01:44:43.9120+01:00 - ERROR -  - ------------- BEGIN Flow.Launcher.Plugin.Program exception -------------
01:44:43.9120+01:00 - ERROR -  -

The AI gave me this update to ShellLinkHelper.cs as a fix:

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using Flow.Launcher.Plugin.Program.Logger;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;
using Windows.Win32.Storage.FileSystem;

namespace Flow.Launcher.Plugin.Program.Programs
{
    public class ShellLinkHelper
    {
        // Reference : http://www.pinvoke.net/default.aspx/Interfaces.IShellLinkW
        [ComImport(), Guid("00021401-0000-0000-C000-000000000046")]
        public class ShellLink
        {
        }

        // To initialize the app description
        public string description = string.Empty;
        public string arguments = string.Empty;

        // Retrieve the target path using Shell Link
        public unsafe string retrieveTargetPath(string path)
        {
            var link = new ShellLink();
            const int STGM_READ = 0;
            ((IPersistFile)link).Load(path, STGM_READ);
            var hwnd = new HWND(IntPtr.Zero);
            // Use SLR_NO_UI to avoid showing any UI during resolution, like Problem with Shortcut dialogs
            // https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ishelllinka-resolve
            ((IShellLinkW)link).Resolve(hwnd, (uint)SLR_FLAGS.SLR_NO_UI);

            const int MAX_PATH = 260;
            Span<char> buffer = stackalloc char[MAX_PATH];

            var data = new WIN32_FIND_DATAW();
            var target = string.Empty;
            try
            {
                fixed (char* bufferPtr = buffer)
                {
                    ((IShellLinkW)link).GetPath((PWSTR)bufferPtr, MAX_PATH, &data, (uint)SLGP_FLAGS.SLGP_SHORTPATH);
                    target = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bufferPtr).ToString();
                }
            }
            catch (COMException e)
            {
                ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" +
                "|Error occurred while getting program path", e);
            }

            // To set the app description
            if (!string.IsNullOrEmpty(target))
            {
                try
                {
                    fixed (char* bufferPtr = buffer)
                    {
                        ((IShellLinkW)link).GetDescription(bufferPtr, MAX_PATH);
                        description = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bufferPtr).ToString();
                    }
                }
                catch (COMException e)
                {
                    // E_FAIL is a generic error, but in this context it's likely that the shortcut has no description.
                    // It's safe to ignore it, the description will be empty.
                    // This happens for some system shortcuts like "Windows Media Player Legacy.lnk".
                    if (e.HResult != -2147467259) // E_FAIL
                    {
                        ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" +
                                                   "|Error caused likely due to trying to get the description of the program",
                            e);
                    }
                }

                try
                {
                    fixed (char* bufferPtr = buffer)
                    {
                        ((IShellLinkW)link).GetArguments(bufferPtr, MAX_PATH);
                        arguments = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bufferPtr).ToString();
                    }
                }
                catch (COMException e)
                {
                    ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" +
                                               "|Error occurred while getting program arguments", e);
                }
            }

            // To release unmanaged memory
            Marshal.ReleaseComObject(link);

            return target;
        }
    }
}

To Reproduce

No response

Screenshots

No response

Flow Launcher Version

1.20.1

Windows Build Number

26100.4484

Error Log

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions