{"record":{"id":"fa0f5649dc992f3a","repo":"dotnet/wpf","slug":"win32exception-native-systemparametersinfo-failure","errorCode":null,"errorMessage":"Win32Exception (native SystemParametersInfo failure retrieving FocusBorderWidth)","messagePattern":"Win32Exception \\(native SystemParametersInfo failure retrieving FocusBorderWidth\\)","errorType":"exception","errorClass":"Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs","lineNumber":115,"sourceCode":"        {\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.FocusBorderWidth])\n                    {\n                        _cacheValid[(int)CacheSlot.FocusBorderWidth] = true;\n\n                        int focusBorderWidth = 0;\n\n                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFOCUSBORDERWIDTH, 0, ref focusBorderWidth, 0))\n                        {\n                            _focusBorderWidth = ConvertPixel(focusBorderWidth);\n                        }\n                        else\n                        {\n                            _cacheValid[(int)CacheSlot.FocusBorderWidth] = false;\n                            throw new Win32Exception();\n                        }\n                    }\n                }\n\n                return _focusBorderWidth;\n            }\n        }\n\n        /// <summary>\n        ///     Maps to SPI_GETFOCUSBORDERHEIGHT\n        /// </summary>\n        public static double FocusBorderHeight\n        {\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.FocusBorderHeight])","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs#L97-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the machine's Themes service and desktop theme are healthy; restart the session or reboot.","Retry the property read once - the cache slot was invalidated so the next read re-issues the SPI call.","Wrap access in try/catch(Win32Exception) and fall back to a default width (typically 1 pixel).","Run under an interactive desktop session with a valid user profile; avoid reading system parameters from a session-0 service."],"exampleFix":"// before\ndouble w = SystemParameters.FocusBorderWidth;\n// after\ndouble w;\ntry { w = SystemParameters.FocusBorderWidth; }\ncatch (Win32Exception) { w = 1.0; }","handlingStrategy":"try-catch","validationCode":"bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;\n// only read SystemParameters.FocusBorderWidth when sessionOk, else use default","typeGuard":null,"tryCatchPattern":"double w;\ntry { w = SystemParameters.FocusBorderWidth; }\ncatch (Win32Exception ex)\n{\n    Trace.TraceWarning($\"FocusBorderWidth SPI failed: {ex.NativeErrorCode}\");\n    w = 1.0;\n}","preventionTips":["Retry once on failure - WPF invalidated the cache slot so the next read re-queries","Prefer interactive user sessions over services for system-parameter reads","Log NativeErrorCode for diagnostics","Centralize SystemParameters reads in one helper with fallback defaults"],"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-21T21:30:21.729Z"}