dotnet/wpf · error · Win32Exception

Win32Exception (native SystemParametersInfo failure…

Error message

Win32Exception (native SystemParametersInfo failure retrieving FocusBorderWidth)

What it means

The FocusBorderWidth property calls the native SystemParametersInfo(SPI_GETFOCUSBORDERWIDTH) API; when it returns false, the cached slot is invalidated and a Win32Exception is thrown. This surfaces as 'native SystemParametersInfo failure retrieving FocusBorderWidth' and means the OS refused to report the focus-rectangle border width. Win32Exception with no explicit code captures Marshal.GetLastWin32Error.

Solutions

  1. Verify the machine's Themes service and desktop theme are healthy; restart the session or reboot.
  2. Retry the property read once - the cache slot was invalidated so the next read re-issues the SPI call.
  3. Wrap access in try/catch(Win32Exception) and fall back to a default width (typically 1 pixel).
  4. Run under an interactive desktop session with a valid user profile; avoid reading system parameters from a session-0 service.

Example fix

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

Strategy: try-catch

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: Reading SystemParameters.FocusBorderWidth (possibly first access, since the value is cached) when the native SystemParametersInfo call fails, e.g. on a terminal/remoted session or corrupted user profile where the SPI query is unsupported.

Common situations: App startup reading theme metrics in remote-desktop or Citrix sessions; UI automation on service accounts without an interactive desktop; themes/theme service (Themes service stopped) broken on the machine.

Related errors


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

Appendix: source

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

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

                        int focusBorderWidth = 0;

                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFOCUSBORDERWIDTH, 0, ref focusBorderWidth, 0))
                        {
                            _focusBorderWidth = ConvertPixel(focusBorderWidth);
                        }
                        else
                        {
                            _cacheValid[(int)CacheSlot.FocusBorderWidth] = false;
                            throw new Win32Exception();
                        }
                    }
                }

                return _focusBorderWidth;
            }
        }

        /// <summary>
        ///     Maps to SPI_GETFOCUSBORDERHEIGHT
        /// </summary>
        public static double FocusBorderHeight
        {
            get
            {
                lock (_cacheValid)
                {
                    while (!_cacheValid[(int)CacheSlot.FocusBorderHeight])

View on GitHub (pinned to 81131a70a4)