mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user