[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:
gokcekantarci
2023-09-15 08:49:23 +03:00
committed by GitHub
parent 6af6f4f43f
commit 368ed68271

View File

@@ -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.