{"record":{"id":"2303290238f9405d","repo":"dotnet/wpf","slug":"win32exception-native-systemparametersinfo-failure-230329","errorCode":null,"errorMessage":"Win32Exception (native SystemParametersInfo failure retrieving HighContrast)","messagePattern":"Win32Exception \\(native SystemParametersInfo failure retrieving HighContrast\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs","lineNumber":179,"sourceCode":"            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.HighContrast])\n                    {\n                        _cacheValid[(int)CacheSlot.HighContrast] = true;\n\n                        NativeMethods.HIGHCONTRAST_I highContrast = new NativeMethods.HIGHCONTRAST_I\n                        {\n                            cbSize = Marshal.SizeOf(typeof(NativeMethods.HIGHCONTRAST_I))\n                        };\n                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETHIGHCONTRAST, highContrast.cbSize, ref highContrast, 0))\n                        {\n                            _highContrast = (highContrast.dwFlags & NativeMethods.HCF_HIGHCONTRASTON) == NativeMethods.HCF_HIGHCONTRASTON;\n                        }\n                        else\n                        {\n                            _cacheValid[(int)CacheSlot.HighContrast] = false;\n                            throw new Win32Exception();\n                        }\n                    }\n                }\n\n                return _highContrast;\n            }\n        }\n\n        /// <summary>\n        /// Maps to SPI_GETMOUSEVANISH.\n        /// </summary>\n        // If this becomes public, it will need a matching ResourceKey.\n        internal static bool MouseVanish\n        {\n            get\n            {\n                lock (_cacheValid)\n                {","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs#L161-L197","documentation":"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.","triggerScenarios":"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.","commonSituations":"Accessibility-aware apps checking high-contrast at startup from a service; terminal-server sessions with restricted UI access; security-hardened environments blocking SPI queries.","solutions":["Retry the read - the invalidated cache slot makes the next access re-query the OS.","Catch Win32Exception and assume high contrast is off (or ask the user) as a fallback.","Ensure the code runs in an interactive user session with access to the default desktop.","Check environment policy (remote session restrictions, AppLocker/service account) if failures are persistent."],"exampleFix":"// before\nbool hc = SystemParameters.HighContrast;\n// after\nbool hc;\ntry { hc = SystemParameters.HighContrast; }\ncatch (Win32Exception) { hc = false; }","handlingStrategy":"try-catch","validationCode":"bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;\n// only read SystemParameters.HighContrast when sessionOk, else assume false","typeGuard":null,"tryCatchPattern":"bool hc;\ntry { hc = SystemParameters.HighContrast; }\ncatch (Win32Exception ex)\n{\n    Trace.TraceWarning($\"HighContrast SPI failed: {ex.NativeErrorCode}\");\n    hc = false;\n}","preventionTips":["Retry once - the invalidated cache slot re-queries the OS","Default to 'high contrast off' when the query fails","Query on the UI thread of an interactive session","Handle SystemParameters.StaticPropertyChanged instead of polling where possible"],"tags":["wpf","win32","systemparametersinfo","accessibility"],"backgroundTag":"native-systemparametersinfo-failure","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}