Compare commits

...

1 Commits

Author SHA1 Message Date
Clint Rutkas
43bcab429b Fix COMException 0xD0000701 in PowerToys Run when resuming from sleep 2025-05-14 08:32:45 -07:00
3 changed files with 55 additions and 7 deletions

View File

@@ -112,5 +112,31 @@ namespace ManagedCommon
public int cyTopHeight;
public int cyBottomHeight;
}
/// <summary>
/// Safely calls DwmExtendFrameIntoClientArea to handle potential COMExceptions
/// that can occur after resuming from sleep state.
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="pMarInset">Margins</param>
/// <returns>IntPtr.Zero on success, error code otherwise</returns>
internal static IntPtr SafeDwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset)
{
try
{
return DwmExtendFrameIntoClientArea(hWnd, ref pMarInset);
}
catch (COMException ex) when (ex.HResult == unchecked((int)0xD0000701))
{
// Return a default value when this specific DWM error occurs (typically after sleep/resume)
// This is a non-critical styling error and can be safely ignored
return new IntPtr(-1); // Indicator for this specific error
}
catch (Exception)
{
// For other exceptions, rethrow to be handled by caller
throw;
}
}
}
}

View File

@@ -47,7 +47,9 @@ namespace ManagedCommon
if (OSVersionHelper.IsWindows10())
{
var margins = new NativeMethods.MARGINS { cxLeftWidth = 0, cxRightWidth = 0, cyBottomHeight = 0, cyTopHeight = 2 };
NativeMethods.DwmExtendFrameIntoClientArea(handle, ref margins);
// Use the safe version of DwmExtendFrameIntoClientArea that handles resume-from-sleep issues
NativeMethods.SafeDwmExtendFrameIntoClientArea(handle, ref margins);
}
}
}

View File

@@ -195,17 +195,37 @@ namespace PowerLauncher
_viewModel.RegisterHotkey(_hwndSource.Handle);
if (OSVersionHelper.IsGreaterThanWindows11_21H2())
{
// ResizeMode="NoResize" removes rounded corners. So force them to rounded.
IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint));
try
{
// ResizeMode="NoResize" removes rounded corners. So force them to rounded.
IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint));
}
catch (COMException ex) when (ex.HResult == unchecked((int)0xD0000701))
{
// Ignore this specific DWM error which can occur after resuming from sleep
// This error is related to window styling and doesn't affect functionality
Log.Exception("DWM styling error occurred (typically after sleep resume). This is non-critical.", ex, GetType());
// Fallback to non-rounded corners
MainBorder.BorderThickness = new Thickness(0.5);
}
catch (Exception ex)
{
// Log other errors but continue execution
Log.Exception("Error setting DWM window attributes", ex, GetType());
// Fallback to non-rounded corners
MainBorder.BorderThickness = new Thickness(0.5);
}
}
else
{
// On Windows10 ResizeMode="NoResize" removes the border so we add a new one.
// Also on 22000 it crashes due to DWMWA_WINDOW_CORNER_PREFERENCE https://github.com/microsoft/PowerToys/issues/36558
MainBorder.BorderThickness = new System.Windows.Thickness(0.5);
MainBorder.BorderThickness = new Thickness(0.5);
}
}