PowerToys/src/modules/launcher/Plugins/Microsoft.Plugin.Program/Programs/ShellLinkHelper.cs

180 lines
7.5 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Accessibility;
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
using Microsoft.Plugin.Program.Logger;
namespace Microsoft.Plugin.Program.Programs
{
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
public class ShellLinkHelper : IShellLinkHelper
{
2020-08-14 06:31:14 +08:00
[Flags]
Enable Static Analysis on programs plugin (#5847) * Removing unused ProgramSettings constructor paramaters. * Fix for: Severity Code Description Project File Line Suppression State Error CA1829 Use the "Count" property instead of Enumerable.Count(). Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 182 Active Severity Code Description Project File Line Suppression State Error CA1827 Count() is used where Any() could be used instead to improve performance. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 287 Active * Fixes for CA1031: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * More fixes(Suppression) for CA1031 (Catching base exception) https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * Fix for nested types being externally visible. CA1034 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Fix for CA1028 If possible, make the underlying type of Hresult System.Int32 instead of uint. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Programs\UWP.cs 236 Active * Fix for CA2227 Collection properties should be readonly. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019 Fix rror CA2211 Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * CA2208: Instantiate argument exceptions correctly https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208?view=vs-2019 * Win32.cs Static analysis fixes: CA2200: Rethrow to preserve stack details https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200?view=vs-2019 CA1825: Avoid zero-length array allocations https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1825?view=vs-2019 CA2211: Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * More Win32.cs static analysis fixes: CA1307: Specify StringComparison https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019 Verifying query is not null before using it. CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 * UWPApplication. CA2007: Do not directly await a Task https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019 * Error CA2010 Consume the hresult returned by method 'Microsoft.Plugin.Program.Programs.AppxPackageHelper.IAppxManifestApplication.GetStringValue(string, out string)' and call Marshal.ThrowExceptionForHR. Note: CA2010 has been removed from future FXCop versions as it can provide incorrect advice for methods that don't return hresult (possibly when [PreserveSig] has been incorrectly labeled.) I've verified the methods in question do still return hresults. * More fixes for Error CA2010 Consume the hresult returned by method and call Marshal.ThrowExceptionForHR. * Fixes for: Error CA2000 Call System.IDisposable.Dispose on object created before all references to it are out of scope. * Suppress CA1031:Do not catch general exception types * Fixes for Error CA1034 Do not nest type ... * Fixing Unit tests that were broken as per a previous fix. * Fix for: CA1034: Nested types should not be visible: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Suppressing CA1707 for native/com types. CA1707:Identifiers should not contain underscores * Fix for Error CA1307 The behavior of 'string.Replace(string, string?)' could vary based on the current user's locale settings. * Fixes for Error CA1825 Avoid unnecessary zero-length array allocations. Use Array.Empty<string>() instead. * Fix for: Error CA1823 Unused field 'IndexLock'. * Fixes for CA1822: Mark members as static: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019 * Fix for Error CA1819 Properties should not return arrays * Fix for Error CA1806 ContextMenus calls Trim but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary. * Fix for Error CA1801 Parameter settings of method UpdateSettings is never used. Remove the parameter or use it in the method body. * Fix for Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. * Includes FxCop for static analysis * Fix for Error CA1717 Only FlagsAttribute enums should have plural names * Suppress Stgm flags: Error CA1714 Flags enums should have plural names Microsoft.Plugin.Program * Rename Win32 to Win32Program Fix for: Error CA1724 The type name Win32 conflicts in whole or in part with the namespace name 'Microsoft.Win32' defined in the .NET Framework. Rename the type to eliminate the conflict. * Fixes for Error CA1507 Use nameof in place of string literal * Using ordinal instead of invariant culture when not symbolic comparison * CA1308: Normalize strings to uppercase https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1308?view=vs-2019 * CA1304: Specify CultureInfo CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1304?view=vs-2019 * CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 * CA1056: URI properties should not be strings https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1056?view=vs-2019 Fix: Making LogoURi private, as it is only used internally and operated on as a string * Warning CA1052 Type 'AppxPackageHelper' is a static holder type but is neither static nor NotInheritable * CA1060: Move P/Invokes to NativeMethods class https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019 * Programs Plugin - Treat warnings as errors true * CA1031:Do not catch general exception types Missed release only compile issue. Making debug and release behave the same. * Renaming 'Create' to 'CreateWin32Program' based on PR feedback.
2020-08-12 00:08:44 +08:00
private enum SLGP_FLAGS
{
SLGP_SHORTPATH = 0x1,
SLGP_UNCPRIORITY = 0x2,
SLGP_RAWPATH = 0x4,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
Enable Static Analysis on programs plugin (#5847) * Removing unused ProgramSettings constructor paramaters. * Fix for: Severity Code Description Project File Line Suppression State Error CA1829 Use the "Count" property instead of Enumerable.Count(). Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 182 Active Severity Code Description Project File Line Suppression State Error CA1827 Count() is used where Any() could be used instead to improve performance. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 287 Active * Fixes for CA1031: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * More fixes(Suppression) for CA1031 (Catching base exception) https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * Fix for nested types being externally visible. CA1034 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Fix for CA1028 If possible, make the underlying type of Hresult System.Int32 instead of uint. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Programs\UWP.cs 236 Active * Fix for CA2227 Collection properties should be readonly. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019 Fix rror CA2211 Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * CA2208: Instantiate argument exceptions correctly https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208?view=vs-2019 * Win32.cs Static analysis fixes: CA2200: Rethrow to preserve stack details https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200?view=vs-2019 CA1825: Avoid zero-length array allocations https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1825?view=vs-2019 CA2211: Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * More Win32.cs static analysis fixes: CA1307: Specify StringComparison https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019 Verifying query is not null before using it. CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 * UWPApplication. CA2007: Do not directly await a Task https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019 * Error CA2010 Consume the hresult returned by method 'Microsoft.Plugin.Program.Programs.AppxPackageHelper.IAppxManifestApplication.GetStringValue(string, out string)' and call Marshal.ThrowExceptionForHR. Note: CA2010 has been removed from future FXCop versions as it can provide incorrect advice for methods that don't return hresult (possibly when [PreserveSig] has been incorrectly labeled.) I've verified the methods in question do still return hresults. * More fixes for Error CA2010 Consume the hresult returned by method and call Marshal.ThrowExceptionForHR. * Fixes for: Error CA2000 Call System.IDisposable.Dispose on object created before all references to it are out of scope. * Suppress CA1031:Do not catch general exception types * Fixes for Error CA1034 Do not nest type ... * Fixing Unit tests that were broken as per a previous fix. * Fix for: CA1034: Nested types should not be visible: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Suppressing CA1707 for native/com types. CA1707:Identifiers should not contain underscores * Fix for Error CA1307 The behavior of 'string.Replace(string, string?)' could vary based on the current user's locale settings. * Fixes for Error CA1825 Avoid unnecessary zero-length array allocations. Use Array.Empty<string>() instead. * Fix for: Error CA1823 Unused field 'IndexLock'. * Fixes for CA1822: Mark members as static: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019 * Fix for Error CA1819 Properties should not return arrays * Fix for Error CA1806 ContextMenus calls Trim but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary. * Fix for Error CA1801 Parameter settings of method UpdateSettings is never used. Remove the parameter or use it in the method body. * Fix for Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. * Includes FxCop for static analysis * Fix for Error CA1717 Only FlagsAttribute enums should have plural names * Suppress Stgm flags: Error CA1714 Flags enums should have plural names Microsoft.Plugin.Program * Rename Win32 to Win32Program Fix for: Error CA1724 The type name Win32 conflicts in whole or in part with the namespace name 'Microsoft.Win32' defined in the .NET Framework. Rename the type to eliminate the conflict. * Fixes for Error CA1507 Use nameof in place of string literal * Using ordinal instead of invariant culture when not symbolic comparison * CA1308: Normalize strings to uppercase https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1308?view=vs-2019 * CA1304: Specify CultureInfo CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1304?view=vs-2019 * CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 * CA1056: URI properties should not be strings https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1056?view=vs-2019 Fix: Making LogoURi private, as it is only used internally and operated on as a string * Warning CA1052 Type 'AppxPackageHelper' is a static holder type but is neither static nor NotInheritable * CA1060: Move P/Invokes to NativeMethods class https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019 * Programs Plugin - Treat warnings as errors true * CA1031:Do not catch general exception types Missed release only compile issue. Making debug and release behave the same. * Renaming 'Create' to 'CreateWin32Program' based on PR feedback.
2020-08-12 00:08:44 +08:00
private struct WIN32_FIND_DATAW
{
public uint dwFileAttributes;
public long ftCreationTime;
public long ftLastAccessTime;
public long ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}
2020-08-14 06:31:14 +08:00
[Flags]
Enable Static Analysis on programs plugin (#5847) * Removing unused ProgramSettings constructor paramaters. * Fix for: Severity Code Description Project File Line Suppression State Error CA1829 Use the "Count" property instead of Enumerable.Count(). Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 182 Active Severity Code Description Project File Line Suppression State Error CA1827 Count() is used where Any() could be used instead to improve performance. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 287 Active * Fixes for CA1031: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * More fixes(Suppression) for CA1031 (Catching base exception) https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * Fix for nested types being externally visible. CA1034 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Fix for CA1028 If possible, make the underlying type of Hresult System.Int32 instead of uint. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Programs\UWP.cs 236 Active * Fix for CA2227 Collection properties should be readonly. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019 Fix rror CA2211 Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * CA2208: Instantiate argument exceptions correctly https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208?view=vs-2019 * Win32.cs Static analysis fixes: CA2200: Rethrow to preserve stack details https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200?view=vs-2019 CA1825: Avoid zero-length array allocations https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1825?view=vs-2019 CA2211: Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * More Win32.cs static analysis fixes: CA1307: Specify StringComparison https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019 Verifying query is not null before using it. CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 * UWPApplication. CA2007: Do not directly await a Task https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019 * Error CA2010 Consume the hresult returned by method 'Microsoft.Plugin.Program.Programs.AppxPackageHelper.IAppxManifestApplication.GetStringValue(string, out string)' and call Marshal.ThrowExceptionForHR. Note: CA2010 has been removed from future FXCop versions as it can provide incorrect advice for methods that don't return hresult (possibly when [PreserveSig] has been incorrectly labeled.) I've verified the methods in question do still return hresults. * More fixes for Error CA2010 Consume the hresult returned by method and call Marshal.ThrowExceptionForHR. * Fixes for: Error CA2000 Call System.IDisposable.Dispose on object created before all references to it are out of scope. * Suppress CA1031:Do not catch general exception types * Fixes for Error CA1034 Do not nest type ... * Fixing Unit tests that were broken as per a previous fix. * Fix for: CA1034: Nested types should not be visible: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Suppressing CA1707 for native/com types. CA1707:Identifiers should not contain underscores * Fix for Error CA1307 The behavior of 'string.Replace(string, string?)' could vary based on the current user's locale settings. * Fixes for Error CA1825 Avoid unnecessary zero-length array allocations. Use Array.Empty<string>() instead. * Fix for: Error CA1823 Unused field 'IndexLock'. * Fixes for CA1822: Mark members as static: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019 * Fix for Error CA1819 Properties should not return arrays * Fix for Error CA1806 ContextMenus calls Trim but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary. * Fix for Error CA1801 Parameter settings of method UpdateSettings is never used. Remove the parameter or use it in the method body. * Fix for Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. * Includes FxCop for static analysis * Fix for Error CA1717 Only FlagsAttribute enums should have plural names * Suppress Stgm flags: Error CA1714 Flags enums should have plural names Microsoft.Plugin.Program * Rename Win32 to Win32Program Fix for: Error CA1724 The type name Win32 conflicts in whole or in part with the namespace name 'Microsoft.Win32' defined in the .NET Framework. Rename the type to eliminate the conflict. * Fixes for Error CA1507 Use nameof in place of string literal * Using ordinal instead of invariant culture when not symbolic comparison * CA1308: Normalize strings to uppercase https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1308?view=vs-2019 * CA1304: Specify CultureInfo CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1304?view=vs-2019 * CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 * CA1056: URI properties should not be strings https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1056?view=vs-2019 Fix: Making LogoURi private, as it is only used internally and operated on as a string * Warning CA1052 Type 'AppxPackageHelper' is a static holder type but is neither static nor NotInheritable * CA1060: Move P/Invokes to NativeMethods class https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019 * Programs Plugin - Treat warnings as errors true * CA1031:Do not catch general exception types Missed release only compile issue. Making debug and release behave the same. * Renaming 'Create' to 'CreateWin32Program' based on PR feedback.
2020-08-12 00:08:44 +08:00
[System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Represents flags specified in IShellLink interface")]
public enum SLR_FLAGS
{
SLR_NO_UI = 0x1,
SLR_ANY_MATCH = 0x2,
SLR_UPDATE = 0x4,
SLR_NOUPDATE = 0x8,
SLR_NOSEARCH = 0x10,
SLR_NOTRACK = 0x20,
SLR_NOLINKINFO = 0x40,
SLR_INVOKE_MSI = 0x80,
}
// Reference : http://www.pinvoke.net/default.aspx/Interfaces.IShellLinkW
2020-08-14 06:31:14 +08:00
// The IShellLink interface allows Shell links to be created, modified, and resolved
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
interface IShellLinkW
{
/// <summary>Retrieves the path and file name of a Shell link object</summary>
2020-08-14 06:31:14 +08:00
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, ref WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags);
/// <summary>Retrieves the list of item identifiers for a Shell link object</summary>
void GetIDList(out IntPtr ppidl);
2020-08-14 06:31:14 +08:00
/// <summary>Sets the pointer to an item identifier list (PIDL) for a Shell link object.</summary>
void SetIDList(IntPtr pidl);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the description string for a Shell link object</summary>
2020-08-14 06:31:14 +08:00
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
/// <summary>Sets the description for a Shell link object. The description can be any application-defined string</summary>
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the name of the working directory for a Shell link object</summary>
2020-08-14 06:31:14 +08:00
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
/// <summary>Sets the name of the working directory for a Shell link object</summary>
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the command-line arguments associated with a Shell link object</summary>
2020-08-14 06:31:14 +08:00
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
/// <summary>Sets the command-line arguments for a Shell link object</summary>
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the hot key for a Shell link object</summary>
void GetHotkey(out short pwHotkey);
2020-08-14 06:31:14 +08:00
/// <summary>Sets a hot key for a Shell link object</summary>
void SetHotkey(short wHotkey);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the show command for a Shell link object</summary>
void GetShowCmd(out int piShowCmd);
2020-08-14 06:31:14 +08:00
/// <summary>Sets the show command for a Shell link object. The show command sets the initial show state of the window.</summary>
void SetShowCmd(int iShowCmd);
2020-08-14 06:31:14 +08:00
/// <summary>Retrieves the location (path and index) of the icon for a Shell link object</summary>
2020-08-14 06:31:14 +08:00
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
/// <summary>Sets the location (path and index) of the icon for a Shell link object</summary>
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
2020-08-14 06:31:14 +08:00
/// <summary>Sets the relative path to the Shell link object</summary>
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
2020-08-14 06:31:14 +08:00
/// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed</summary>
void Resolve(ref Accessibility._RemotableHandle hwnd, SLR_FLAGS fFlags);
2020-08-14 06:31:14 +08:00
/// <summary>Sets the path and file name of a Shell link object</summary>
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
2020-08-14 06:31:14 +08:00
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
Enable Static Analysis on programs plugin (#5847) * Removing unused ProgramSettings constructor paramaters. * Fix for: Severity Code Description Project File Line Suppression State Error CA1829 Use the "Count" property instead of Enumerable.Count(). Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 182 Active Severity Code Description Project File Line Suppression State Error CA1827 Count() is used where Any() could be used instead to improve performance. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Views\ProgramSetting.xaml.cs 287 Active * Fixes for CA1031: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * More fixes(Suppression) for CA1031 (Catching base exception) https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1031?view=vs-2019 * Fix for nested types being externally visible. CA1034 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Fix for CA1028 If possible, make the underlying type of Hresult System.Int32 instead of uint. Microsoft.Plugin.Program C:\Repos\PowerToys\src\modules\launcher\Plugins\Microsoft.Plugin.Program\Programs\UWP.cs 236 Active * Fix for CA2227 Collection properties should be readonly. https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2227?view=vs-2019 Fix rror CA2211 Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * CA2208: Instantiate argument exceptions correctly https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2208?view=vs-2019 * Win32.cs Static analysis fixes: CA2200: Rethrow to preserve stack details https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2200?view=vs-2019 CA1825: Avoid zero-length array allocations https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1825?view=vs-2019 CA2211: Non-constant fields should not be visible https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2211?view=vs-2019 * More Win32.cs static analysis fixes: CA1307: Specify StringComparison https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1307?view=vs-2019 Verifying query is not null before using it. CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 * UWPApplication. CA2007: Do not directly await a Task https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2007?view=vs-2019 * Error CA2010 Consume the hresult returned by method 'Microsoft.Plugin.Program.Programs.AppxPackageHelper.IAppxManifestApplication.GetStringValue(string, out string)' and call Marshal.ThrowExceptionForHR. Note: CA2010 has been removed from future FXCop versions as it can provide incorrect advice for methods that don't return hresult (possibly when [PreserveSig] has been incorrectly labeled.) I've verified the methods in question do still return hresults. * More fixes for Error CA2010 Consume the hresult returned by method and call Marshal.ThrowExceptionForHR. * Fixes for: Error CA2000 Call System.IDisposable.Dispose on object created before all references to it are out of scope. * Suppress CA1031:Do not catch general exception types * Fixes for Error CA1034 Do not nest type ... * Fixing Unit tests that were broken as per a previous fix. * Fix for: CA1034: Nested types should not be visible: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1034?view=vs-2019 * Suppressing CA1707 for native/com types. CA1707:Identifiers should not contain underscores * Fix for Error CA1307 The behavior of 'string.Replace(string, string?)' could vary based on the current user's locale settings. * Fixes for Error CA1825 Avoid unnecessary zero-length array allocations. Use Array.Empty<string>() instead. * Fix for: Error CA1823 Unused field 'IndexLock'. * Fixes for CA1822: Mark members as static: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1822?view=vs-2019 * Fix for Error CA1819 Properties should not return arrays * Fix for Error CA1806 ContextMenus calls Trim but does not use the new string instance that the method returns. Pass the instance as an argument to another method, assign the instance to a variable, or remove the call if it is unnecessary. * Fix for Error CA1801 Parameter settings of method UpdateSettings is never used. Remove the parameter or use it in the method body. * Fix for Error CA1724 The type name Settings conflicts in whole or in part with the namespace name 'Microsoft.PowerToys.Settings'. * Includes FxCop for static analysis * Fix for Error CA1717 Only FlagsAttribute enums should have plural names * Suppress Stgm flags: Error CA1714 Flags enums should have plural names Microsoft.Plugin.Program * Rename Win32 to Win32Program Fix for: Error CA1724 The type name Win32 conflicts in whole or in part with the namespace name 'Microsoft.Win32' defined in the .NET Framework. Rename the type to eliminate the conflict. * Fixes for Error CA1507 Use nameof in place of string literal * Using ordinal instead of invariant culture when not symbolic comparison * CA1308: Normalize strings to uppercase https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1308?view=vs-2019 * CA1304: Specify CultureInfo CA1305: Specify IFormatProvider https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1305?view=vs-2019 https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1304?view=vs-2019 * CA1062: Validate arguments of public methods https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019 * CA1056: URI properties should not be strings https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1056?view=vs-2019 Fix: Making LogoURi private, as it is only used internally and operated on as a string * Warning CA1052 Type 'AppxPackageHelper' is a static holder type but is neither static nor NotInheritable * CA1060: Move P/Invokes to NativeMethods class https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1060?view=vs-2019 * Programs Plugin - Treat warnings as errors true * CA1031:Do not catch general exception types Missed release only compile issue. Making debug and release behave the same. * Renaming 'Create' to 'CreateWin32Program' based on PR feedback.
2020-08-12 00:08:44 +08:00
private class ShellLink
{
}
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
// Contains the description of the app
2020-08-14 06:31:14 +08:00
public string description { get; set; } = string.Empty;
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
// Contains the arguments to the app
2020-08-14 06:31:14 +08:00
public string Arguments { get; set; } = string.Empty;
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
public bool hasArguments { get; set; } = false;
// Retrieve the target path using Shell Link
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
public string RetrieveTargetPath(string path)
{
var link = new ShellLink();
const int STGM_READ = 0;
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
try
{
((IPersistFile)link).Load(path, STGM_READ);
}
catch (System.IO.FileNotFoundException ex)
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
{
ProgramLogger.LogException($"|Win32| ShellLinkHelper.retrieveTargetPath | {path} | Path could not be retrieved", ex);
2020-08-14 06:31:14 +08:00
return string.Empty;
Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960) * Added file system wrapper and interface * added win32program repository which would load store app and also handle new apps being added/deleted * Added event handlers to win32 program repo * added paths to monitor and setting FSWs for each location * Events firing as expected * filter extensions added, events fire as expected * override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected * implemented setter for filters in FileSystemWatcher * Rename adds item but does not seem to delete the previous app * catching an exception when a duplicate item is inserted * Removed notify filter for directory because we only need to monitor files * Added exe programs to be indexed in the desktop and startmenu * created a new class to init FileSystemHelpers instead of main * Added fix for shortcut applications to work as expected while renaming and deleting them * Added wrappers for file system operations * Added some tests * Added all tests for appref-ms and added a condition to search in sub directories * Added tests for Exe applications * Added lnk app tests * Added tests for shortcut applications * removed unnecessary wrappers * override Equals for win32 * removed debug statements * Fixed exe issue * Fixed internet shortcut exception * fixed renaming shortcut apps * Added a retry block for ReadAllLines * capitalized method name - RetrieveTargetPath * made naming consistent, helper variables start with underscore * Added the exception condition back * renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers * made win32Program repository variable static * changed list to ilist * disposed file system watchers * make retrieveTargetPath upper case in tests
2020-07-18 13:32:21 +08:00
}
2020-08-14 06:31:14 +08:00
var hwnd = default(_RemotableHandle);
((IShellLinkW)link).Resolve(ref hwnd, 0);
const int MAX_PATH = 260;
StringBuilder buffer = new StringBuilder(MAX_PATH);
2020-08-14 06:31:14 +08:00
var data = default(WIN32_FIND_DATAW);
((IShellLinkW)link).GetPath(buffer, buffer.Capacity, ref data, SLGP_FLAGS.SLGP_SHORTPATH);
var target = buffer.ToString();
// To set the app description
2020-08-14 06:31:14 +08:00
if (!string.IsNullOrEmpty(target))
{
buffer = new StringBuilder(MAX_PATH);
((IShellLinkW)link).GetDescription(buffer, MAX_PATH);
description = buffer.ToString();
StringBuilder argumentBuffer = new StringBuilder(MAX_PATH);
((IShellLinkW)link).GetArguments(argumentBuffer, argumentBuffer.Capacity);
Arguments = argumentBuffer.ToString();
// Set variable to true if the program takes in any arguments
if (argumentBuffer.Length != 0)
{
hasArguments = true;
}
}
2020-08-14 06:31:14 +08:00
return target;
}
}
}