[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 коммит произвёл GitHub
Родитель 534b9673cd
Коммит 912d7ec060
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 32 добавлений и 8 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -82,7 +82,19 @@ namespace Wox.Infrastructure.Image
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
{
@ -119,7 +131,7 @@ namespace Wox.Infrastructure.Image
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)
{
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 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.

Просмотреть файл

@ -7,10 +7,9 @@ using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using Accessibility;
using Microsoft.Plugin.Program.Logger;
using Wox.Plugin.Logger;
namespace Microsoft.Plugin.Program.Programs
namespace Wox.Infrastructure
{
public class ShellLinkHelper : IShellLinkHelper
{
@ -142,7 +141,7 @@ namespace Microsoft.Plugin.Program.Programs
}
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;
}
@ -165,7 +164,7 @@ namespace Microsoft.Plugin.Program.Programs
((IShellLinkW)link).GetDescription(buffer, MAX_PATH);
Description = buffer.ToString();
}
catch (Exception e)
catch (System.Exception e)
{
Log.Exception($"Failed to fetch description for {target}, {e.Message}", e, GetType());
Description = string.Empty;