dotnet/wpf · error · Win32Exception

Win32Exception (native SystemParametersInfo failure…

Error message

Win32Exception (native SystemParametersInfo failure retrieving FocusBorderHeight)

What it means

FocusBorderHeight retrieves SPI_GETFOCUSBORDERHEIGHT via SystemParametersInfo; if the native call fails, the cache slot is invalidated and a Win32Exception is thrown. The exception means the OS could not supply the focus-rectangle border height. Like its siblings, the failure is reported through the standard last-Win32-error mechanism.

Solutions

  1. Restore a healthy desktop/theme environment (Themes service running, interactive session).
  2. Retry the read; the cache invalidation ensures a fresh SPI attempt next time.
  3. Catch Win32Exception and substitute a sensible default (typically 1 pixel).
  4. Avoid calling from non-interactive contexts; query in the UI thread of a real user session.

Example fix

// before
double h = SystemParameters.FocusBorderHeight;
// after
double h;
try { h = SystemParameters.FocusBorderHeight; }
catch (Win32Exception) { h = 1.0; }
Defensive patterns

Strategy: try-catch

Validate before calling

bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;
// only read SystemParameters.FocusBorderHeight when sessionOk, else use default

Try / catch

double h;
try { h = SystemParameters.FocusBorderHeight; }
catch (Win32Exception ex)
{
    Trace.TraceWarning($"FocusBorderHeight SPI failed: {ex.NativeErrorCode}");
    h = 1.0;
}

Prevention

When it happens

Trigger: First read of SystemParameters.FocusBorderHeight (cache miss forces the SPI query) on a system where SystemParametersInfo returns false, e.g. remoted sessions or a broken theme configuration.

Common situations: Theme metric collection during app startup in RDP/ICA sessions; running under service or non-interactive accounts; corrupted visual styles.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/c48709a7d7356c15. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs:146

        {
            get
            {
                lock (_cacheValid)
                {
                    while (!_cacheValid[(int)CacheSlot.FocusBorderHeight])
                    {
                        _cacheValid[(int)CacheSlot.FocusBorderHeight] = true;

                        int focusBorderHeight = 0;

                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFOCUSBORDERHEIGHT, 0, ref focusBorderHeight, 0))
                        {
                            _focusBorderHeight = ConvertPixel(focusBorderHeight);
                        }
                        else
                        {
                            _cacheValid[(int)CacheSlot.FocusBorderHeight] = false;
                            throw new Win32Exception();
                        }
                    }
                }

                return _focusBorderHeight;
            }
        }

        /// <summary>
        ///     Maps to SPI_GETHIGHCONTRAST -> HCF_HIGHCONTRASTON
        /// </summary>
        public static bool HighContrast
        {
            get
            {
                lock (_cacheValid)
                {
                    while (!_cacheValid[(int)CacheSlot.HighContrast])

View on GitHub (pinned to 81131a70a4)