[PTRun][Program]Fix crashes getting images for .lnk files (#29237)

* [PTRun] Refactor GetHBitmap to handle external DLL failures in a separate task.

* Revert "[PTRun] Refactor GetHBitmap to handle external DLL failures in a separate task."

This reverts commit 159c5744b8.

* [PTRun] GetBitmapSource function has been added for file extensions that cause crashes.

* [PTRun] Spell Check update.

* [PTRun] Renamed function and variable name.

* [PTRun] ShellLinkHelper moved to Wox.Infrastructure for common use.
This commit is contained in:
gokcekantarci
2023-11-09 17:18:00 +03:00
committed by GitHub
parent 534b9673cd
commit 912d7ec060
4 changed files with 32 additions and 8 deletions

View File

@@ -7,10 +7,10 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Abstractions; using System.IO.Abstractions;
using System.Linq; using System.Linq;
using Microsoft.Plugin.Program.Programs;
using Microsoft.Plugin.Program.Storage; using Microsoft.Plugin.Program.Storage;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq; using Moq;
using Wox.Infrastructure;
using Wox.Infrastructure.FileSystemHelper; using Wox.Infrastructure.FileSystemHelper;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Win32Program = Microsoft.Plugin.Program.Programs.Win32Program; using Win32Program = Microsoft.Plugin.Program.Programs.Win32Program;

View File

@@ -2,7 +2,7 @@
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Program.Programs namespace Wox.Infrastructure
{ {
public interface IShellLinkHelper public interface IShellLinkHelper
{ {

View File

@@ -82,7 +82,19 @@ namespace Wox.Infrastructure.Image
public static BitmapSource GetThumbnail(string fileName, int width, int height, ThumbnailOptions options) public static BitmapSource GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
{ {
IntPtr hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options); IntPtr hBitmap = IntPtr.Zero;
if (Path.GetExtension(fileName).Equals(".lnk", StringComparison.OrdinalIgnoreCase))
{
// If the file has a '.lnk' extension, it is a shortcut file. Use the shellLinkHelper to retrieve the actual target file path from the shortcut.
IShellLinkHelper shellLinkHelper = new ShellLinkHelper();
string targetFilePath = shellLinkHelper.RetrieveTargetPath(fileName);
hBitmap = ExtractIconToHBitmap(targetFilePath);
}
else
{
hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options);
}
try try
{ {
@@ -119,7 +131,7 @@ namespace Wox.Infrastructure.Image
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap); HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out hBitmap);
// if extracting image thumbnail and failed, extract shell icon // if extracting image thumbnail and failed, extract shell icon
if (options == ThumbnailOptions.ThumbnailOnly && hr == HResult.ExtractionFailed) if (options == ThumbnailOptions.ThumbnailOnly && hr == HResult.ExtractionFailed)
{ {
hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, ThumbnailOptions.IconOnly, out hBitmap); hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, ThumbnailOptions.IconOnly, out hBitmap);
@@ -146,6 +158,19 @@ namespace Wox.Infrastructure.Image
} }
} }
public static IntPtr ExtractIconToHBitmap(string fileName)
{
// Extracts the icon associated with the file
using (System.Drawing.Icon thumbnailIcon = System.Drawing.Icon.ExtractAssociatedIcon(fileName))
{
// Convert to Bitmap
using (System.Drawing.Bitmap bitmap = thumbnailIcon.ToBitmap())
{
return bitmap.GetHbitmap();
}
}
}
private static bool logReportedAdobeReaderDetected; // Keep track if Adobe Reader detection has been logged yet. private static bool logReportedAdobeReaderDetected; // Keep track if Adobe Reader detection has been logged yet.
private static bool logReportedErrorInDetectingAdobeReader; // Keep track if we reported an exception while trying to detect Adobe Reader yet. private static bool logReportedErrorInDetectingAdobeReader; // Keep track if we reported an exception while trying to detect Adobe Reader yet.
private static bool adobeReaderDetectionLastResult; // The last result when Adobe Reader detection has read the registry. private static bool adobeReaderDetectionLastResult; // The last result when Adobe Reader detection has read the registry.

View File

@@ -7,10 +7,9 @@ using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes; using System.Runtime.InteropServices.ComTypes;
using System.Text; using System.Text;
using Accessibility; using Accessibility;
using Microsoft.Plugin.Program.Logger;
using Wox.Plugin.Logger; using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Program.Programs namespace Wox.Infrastructure
{ {
public class ShellLinkHelper : IShellLinkHelper public class ShellLinkHelper : IShellLinkHelper
{ {
@@ -142,7 +141,7 @@ namespace Microsoft.Plugin.Program.Programs
} }
catch (System.IO.FileNotFoundException ex) catch (System.IO.FileNotFoundException ex)
{ {
ProgramLogger.Exception("Path could not be retrieved", ex, GetType(), path); Log.Exception("Path could not be retrieved", ex, GetType(), path);
return string.Empty; return string.Empty;
} }
@@ -165,7 +164,7 @@ namespace Microsoft.Plugin.Program.Programs
((IShellLinkW)link).GetDescription(buffer, MAX_PATH); ((IShellLinkW)link).GetDescription(buffer, MAX_PATH);
Description = buffer.ToString(); Description = buffer.ToString();
} }
catch (Exception e) catch (System.Exception e)
{ {
Log.Exception($"Failed to fetch description for {target}, {e.Message}", e, GetType()); Log.Exception($"Failed to fetch description for {target}, {e.Message}", e, GetType());
Description = string.Empty; Description = string.Empty;