ExceptionHelper.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Runtime.InteropServices; 7 8 namespace PowerLauncher.Helper 9 { 10 [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Win32 naming conventions")] 11 internal static class ExceptionHelper 12 { 13 private const string PresentationFrameworkExceptionSource = "PresentationFramework"; 14 15 private const int DWM_E_COMPOSITIONDISABLED = unchecked((int)0x80263001); 16 17 // HRESULT for NT STATUS STATUS_MESSAGE_LOST (0xC0000701 | 0x10000000 == 0xD0000701) 18 private const int STATUS_MESSAGE_LOST_HR = unchecked((int)0xD0000701); 19 20 /// <summary> 21 /// Returns true if the exception is a recoverable DWM composition exception. 22 /// </summary> 23 internal static bool IsRecoverableDwmCompositionException(Exception exception) 24 { 25 if (exception is not COMException comException) 26 { 27 return false; 28 } 29 30 if (comException.HResult is DWM_E_COMPOSITIONDISABLED) 31 { 32 return true; 33 } 34 35 if (comException.HResult is STATUS_MESSAGE_LOST_HR && comException.Source == PresentationFrameworkExceptionSource) 36 { 37 return true; 38 } 39 40 // Check for common DWM composition changed patterns in the stack trace 41 var stackTrace = comException.StackTrace; 42 return !string.IsNullOrEmpty(stackTrace) && 43 stackTrace.Contains("DwmCompositionChanged"); 44 } 45 } 46 }