{"record":{"id":"c48709a7d7356c15","repo":"dotnet/wpf","slug":"win32exception-native-systemparametersinfo-failure-c48709","errorCode":null,"errorMessage":"Win32Exception (native SystemParametersInfo failure retrieving FocusBorderHeight)","messagePattern":"Win32Exception \\(native SystemParametersInfo failure retrieving FocusBorderHeight\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs","lineNumber":146,"sourceCode":"        {\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.FocusBorderHeight])\n                    {\n                        _cacheValid[(int)CacheSlot.FocusBorderHeight] = true;\n\n                        int focusBorderHeight = 0;\n\n                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFOCUSBORDERHEIGHT, 0, ref focusBorderHeight, 0))\n                        {\n                            _focusBorderHeight = ConvertPixel(focusBorderHeight);\n                        }\n                        else\n                        {\n                            _cacheValid[(int)CacheSlot.FocusBorderHeight] = false;\n                            throw new Win32Exception();\n                        }\n                    }\n                }\n\n                return _focusBorderHeight;\n            }\n        }\n\n        /// <summary>\n        ///     Maps to SPI_GETHIGHCONTRAST -> HCF_HIGHCONTRASTON\n        /// </summary>\n        public static bool HighContrast\n        {\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.HighContrast])","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs#L128-L164","documentation":"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.","triggerScenarios":"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.","commonSituations":"Theme metric collection during app startup in RDP/ICA sessions; running under service or non-interactive accounts; corrupted visual styles.","solutions":["Restore a healthy desktop/theme environment (Themes service running, interactive session).","Retry the read; the cache invalidation ensures a fresh SPI attempt next time.","Catch Win32Exception and substitute a sensible default (typically 1 pixel).","Avoid calling from non-interactive contexts; query in the UI thread of a real user session."],"exampleFix":"// before\ndouble h = SystemParameters.FocusBorderHeight;\n// after\ndouble h;\ntry { h = SystemParameters.FocusBorderHeight; }\ncatch (Win32Exception) { h = 1.0; }","handlingStrategy":"try-catch","validationCode":"bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;\n// only read SystemParameters.FocusBorderHeight when sessionOk, else use default","typeGuard":null,"tryCatchPattern":"double h;\ntry { h = SystemParameters.FocusBorderHeight; }\ncatch (Win32Exception ex)\n{\n    Trace.TraceWarning($\"FocusBorderHeight SPI failed: {ex.NativeErrorCode}\");\n    h = 1.0;\n}","preventionTips":["Retry once on failure - the cache slot is re-queried next read","Avoid reading theme metrics from session-0 services","Log NativeErrorCode for diagnostics","Keep a documented default for each system metric"],"tags":["wpf","win32","systemparametersinfo","interop"],"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-22T01:17:13.364Z"}