mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[PTRun][Program]Exception handling and checks at startup thumbnail generation (#28534)
* [PTRun] Exception handling and other checks are added. * [PTRun] Revert lines
This commit is contained in:
@@ -97,36 +97,53 @@ namespace Wox.Infrastructure.Image
|
|||||||
|
|
||||||
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
|
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
|
||||||
{
|
{
|
||||||
Guid shellItem2Guid = new Guid(IShellItem2Guid);
|
IntPtr hBitmap = IntPtr.Zero;
|
||||||
int retCode = NativeMethods.SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out IShellItem nativeShellItem);
|
IShellItem nativeShellItem = null;
|
||||||
|
|
||||||
if (retCode != 0)
|
try
|
||||||
{
|
{
|
||||||
throw Marshal.GetExceptionForHR(retCode);
|
Guid shellItem2Guid = new Guid(IShellItem2Guid);
|
||||||
}
|
int retCode = NativeMethods.SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out nativeShellItem);
|
||||||
|
|
||||||
NativeSize nativeSize = new NativeSize
|
if (retCode != 0)
|
||||||
{
|
{
|
||||||
Width = width,
|
Log.Error($"Error while creating item. retCode:{retCode} ", MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
Height = height,
|
throw Marshal.GetExceptionForHR(retCode);
|
||||||
};
|
}
|
||||||
|
|
||||||
HResult hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out IntPtr hBitmap);
|
NativeSize nativeSize = new NativeSize
|
||||||
|
{
|
||||||
|
Width = width,
|
||||||
|
Height = height,
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
Marshal.ReleaseComObject(nativeShellItem);
|
if (hr != HResult.Ok)
|
||||||
|
{
|
||||||
|
throw Marshal.GetExceptionForHR((int)hr);
|
||||||
|
}
|
||||||
|
|
||||||
if (hr == HResult.Ok)
|
|
||||||
{
|
|
||||||
return hBitmap;
|
return hBitmap;
|
||||||
}
|
}
|
||||||
|
catch (System.Exception ex)
|
||||||
throw new InvalidComObjectException($"Error while extracting thumbnail for {fileName}", Marshal.GetExceptionForHR((int)hr));
|
{
|
||||||
|
Log.Exception($"Error while extracting thumbnail for {fileName}", ex, MethodBase.GetCurrentMethod().DeclaringType);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (nativeShellItem != null)
|
||||||
|
{
|
||||||
|
Marshal.ReleaseComObject(nativeShellItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user