dotnet/wpf · error · Win32Exception

Win32Exception (native SystemParametersInfo failure…

Error message

Win32Exception (native SystemParametersInfo failure retrieving HighContrast)

What it means

The HighContrast property queries SPI_GETHIGHCONTRAST through SystemParametersInfo; on failure the cache slot is reset and a Win32Exception is thrown. It means the OS declined to report whether high-contrast mode is on. This is thrown from the public property getter on first (uncached) access.

Solutions

  1. Retry the read - the invalidated cache slot makes the next access re-query the OS.
  2. Catch Win32Exception and assume high contrast is off (or ask the user) as a fallback.
  3. Ensure the code runs in an interactive user session with access to the default desktop.
  4. Check environment policy (remote session restrictions, AppLocker/service account) if failures are persistent.

Example fix

// before
bool hc = SystemParameters.HighContrast;
// after
bool hc;
try { hc = SystemParameters.HighContrast; }
catch (Win32Exception) { hc = false; }
Defensive patterns

Strategy: try-catch

Validate before calling

bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;
// only read SystemParameters.HighContrast when sessionOk, else assume false

Try / catch

bool hc;
try { hc = SystemParameters.HighContrast; }
catch (Win32Exception ex)
{
    Trace.TraceWarning($"HighContrast SPI failed: {ex.NativeErrorCode}");
    hc = false;
}

Prevention

When it happens

Trigger: Reading SystemParameters.HighContrast when the native SystemParametersInfo(SPI_GETHIGHCONTRAST) call returns false, e.g. no interactive window station, restricted tokens, or an accessibility-API failure in remoted sessions.

Common situations: Accessibility-aware apps checking high-contrast at startup from a service; terminal-server sessions with restricted UI access; security-hardened environments blocking SPI queries.

Related errors


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

Appendix: source

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

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

                        NativeMethods.HIGHCONTRAST_I highContrast = new NativeMethods.HIGHCONTRAST_I
                        {
                            cbSize = Marshal.SizeOf(typeof(NativeMethods.HIGHCONTRAST_I))
                        };
                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETHIGHCONTRAST, highContrast.cbSize, ref highContrast, 0))
                        {
                            _highContrast = (highContrast.dwFlags & NativeMethods.HCF_HIGHCONTRASTON) == NativeMethods.HCF_HIGHCONTRASTON;
                        }
                        else
                        {
                            _cacheValid[(int)CacheSlot.HighContrast] = false;
                            throw new Win32Exception();
                        }
                    }
                }

                return _highContrast;
            }
        }

        /// <summary>
        /// Maps to SPI_GETMOUSEVANISH.
        /// </summary>
        // If this becomes public, it will need a matching ResourceKey.
        internal static bool MouseVanish
        {
            get
            {
                lock (_cacheValid)
                {

View on GitHub (pinned to 81131a70a4)