From 5303e132b8e70467620553b0a754cdfb485bde81 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sun, 21 Jan 2024 17:37:04 -0600 Subject: [PATCH] minor fix --- Flow.Launcher/MainWindow.axaml | 6 +- .../Programs/ShellLinkHelper.cs | 99 ++++++++++++------- Plugins/Flow.Launcher.Plugin.Sys/Main.cs | 2 - 3 files changed, 69 insertions(+), 38 deletions(-) diff --git a/Flow.Launcher/MainWindow.axaml b/Flow.Launcher/MainWindow.axaml index 6c6efe924..47f9da896 100644 --- a/Flow.Launcher/MainWindow.axaml +++ b/Flow.Launcher/MainWindow.axaml @@ -196,12 +196,12 @@ - + - + @@ -229,7 +229,7 @@ x:Name="PluginActivationIcon" HorizontalAlignment="Right" VerticalAlignment="Center" - Source="{Binding PluginIconPath}" + Source="{Binding PluginIcon}" Stretch="Uniform" /> Retrieves the path and file name of a Shell link object - void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, ref WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags); + void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, + ref WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags); + /// Retrieves the list of item identifiers for a Shell link object void GetIDList(out IntPtr ppidl); + /// Sets the pointer to an item identifier list (PIDL) for a Shell link object. void SetIDList(IntPtr pidl); + /// Retrieves the description string for a Shell link object void GetDescription([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); + /// Sets the description for a Shell link object. The description can be any application-defined string void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); + /// Retrieves the name of the working directory for a Shell link object void GetWorkingDirectory([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); + /// Sets the name of the working directory for a Shell link object void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); + /// Retrieves the command-line arguments associated with a Shell link object void GetArguments([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); + /// Sets the command-line arguments for a Shell link object void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); + /// Retrieves the hot key for a Shell link object void GetHotkey(out short pwHotkey); + /// Sets a hot key for a Shell link object void SetHotkey(short wHotkey); + /// Retrieves the show command for a Shell link object void GetShowCmd(out int piShowCmd); + /// Sets the show command for a Shell link object. The show command sets the initial show state of the window. void SetShowCmd(int iShowCmd); + /// Retrieves the location (path and index) of the icon for a Shell link object void GetIconLocation([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon); + /// Sets the location (path and index) of the icon for a Shell link object void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); + /// Sets the relative path to the Shell link object void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); + /// Attempts to find the target of a Shell link, even if it has been moved or renamed void Resolve(ref Accessibility._RemotableHandle hwnd, SLR_FLAGS fFlags); + /// Sets the path and file name of a Shell link object void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); } @@ -105,44 +125,57 @@ namespace Flow.Launcher.Plugin.Program.Programs public string retrieveTargetPath(string path) { var link = new ShellLink(); - const int STGM_READ = 0; - ((IPersistFile)link).Load(path, STGM_READ); - var hwnd = new _RemotableHandle(); - ((IShellLinkW)link).Resolve(ref hwnd, 0); - const int MAX_PATH = 260; - StringBuilder buffer = new StringBuilder(MAX_PATH); - - var data = new WIN32_FIND_DATAW(); - ((IShellLinkW)link).GetPath(buffer, buffer.Capacity, ref data, SLGP_FLAGS.SLGP_SHORTPATH); - var target = buffer.ToString(); - - // To set the app description - if (!String.IsNullOrEmpty(target)) + try { - try + const int STGM_READ = 0; + ((IPersistFile)link).Load(path, STGM_READ); + var hwnd = new _RemotableHandle(); + ((IShellLinkW)link).Resolve(ref hwnd, 0); + + const int MAX_PATH = 260; + StringBuilder buffer = new StringBuilder(MAX_PATH); + + var data = new WIN32_FIND_DATAW(); + ((IShellLinkW)link).GetPath(buffer, buffer.Capacity, ref data, SLGP_FLAGS.SLGP_SHORTPATH); + var target = buffer.ToString(); + + // To set the app description + if (!String.IsNullOrEmpty(target)) { - buffer = new StringBuilder(MAX_PATH); - ((IShellLinkW)link).GetDescription(buffer, MAX_PATH); - description = buffer.ToString(); - } - catch (COMException e) - { - // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception - ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" + - "|Error caused likely due to trying to get the description of the program", - e); + try + { + buffer = new StringBuilder(MAX_PATH); + ((IShellLinkW)link).GetDescription(buffer, MAX_PATH); + description = buffer.ToString(); + } + catch (COMException e) + { + // C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\MiracastView.lnk always cause exception + ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" + + "|Error caused likely due to trying to get the description of the program", + e); + } + + buffer.Clear(); + ((IShellLinkW)link).GetArguments(buffer, MAX_PATH); + arguments = buffer.ToString(); } - buffer.Clear(); - ((IShellLinkW)link).GetArguments(buffer, MAX_PATH); - arguments = buffer.ToString(); + + return target; + } + catch (Exception e) + { + ProgramLogger.LogException($"|IShellLinkW|retrieveTargetPath|{path}" + + "|Error caused likely due to trying to get the target path of the program", + e); + return string.Empty; + } + finally + { + Marshal.ReleaseComObject(link); } - - // To release unmanaged memory - Marshal.ReleaseComObject(link); - - return target; } } } diff --git a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs index 853078029..074ffcc1f 100644 --- a/Plugins/Flow.Launcher.Plugin.Sys/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Sys/Main.cs @@ -309,8 +309,6 @@ namespace Flow.Launcher.Plugin.Sys Action = c => { // Hide the window first then show msg after done because sometimes the reload could take a while, so not to make user think it's frozen. - Application.Current.MainWindow.Hide(); - _ = context.API.ReloadAllPluginData().ContinueWith(_ => context.API.ShowMsg( context.API.GetTranslation("flowlauncher_plugin_sys_dlgtitle_success"),