{"record":{"id":"8008a6a3104d9859","repo":"dotnet/wpf","slug":"win32exception-no-message-error-code-from-getlasterror","errorCode":null,"errorMessage":"Win32Exception (no message; error code from GetLastError)","messagePattern":"Win32Exception \\(no message; error code from GetLastError\\)","errorType":"exception","errorClass":"System.ComponentModel.Win32Exception","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs","lineNumber":327,"sourceCode":"        /// </summary>\n        public static bool FlatMenu\n        {\n\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.FlatMenu])\n                    {\n                        _cacheValid[(int)CacheSlot.FlatMenu] = true;\n\n                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFLATMENU, 0, ref _flatMenu, 0))\n                        {\n                        }\n                        else\n                        {\n                            _cacheValid[(int)CacheSlot.FlatMenu] = false;\n                            throw new Win32Exception();\n                        }\n                    }\n                }\n\n                return _flatMenu;\n            }\n        }\n\n        /// <summary>\n        ///     Maps to SPI_GETWORKAREA\n        /// </summary>\n        internal static NativeMethods.RECT WorkAreaInternal\n        {\n            get\n            {\n                lock (_cacheValid)\n                {\n                    while (!_cacheValid[(int)CacheSlot.WorkAreaInternal])","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemParameters.cs#L309-L345","documentation":"The FlatMenu property calls SystemParametersInfo(SPI_GETFLATMENU); a false return invalidates the FlatMenu cache slot and throws a bare Win32Exception whose ErrorCode comes from GetLastError. It means the native query for flat-menu appearance failed. No message text is attached; inspect the exception's NativeErrorCode.","triggerScenarios":"First (uncached) read of SystemParameters.FlatMenu when the SPI_GETFLATQUERY fails - typically when visual styles/themes are unavailable, e.g. service context or remoted session.","commonSituations":"Startup theme-metric collection on Windows Server with Themes service stopped; headless CI UI tests; remote sessions without full desktop composition.","solutions":["Restart/enable the Themes service and rerun in an interactive session.","Catch Win32Exception, inspect NativeErrorCode, and default FlatMenu to false.","Retry the property access; the invalidated cache forces a fresh SPI call next time.","Log the NativeErrorCode to diagnose whether access is denied vs unsupported."],"exampleFix":"// before\nbool flat = SystemParameters.FlatMenu;\n// after\nbool flat;\ntry { flat = SystemParameters.FlatMenu; }\ncatch (Win32Exception ex) { Log(ex.NativeErrorCode); flat = false; }","handlingStrategy":"try-catch","validationCode":"bool sessionOk = !SystemInformation.TerminalServerSession && Environment.UserInteractive;\n// only read SystemParameters.FlatMenu when sessionOk, else default false","typeGuard":null,"tryCatchPattern":"bool flat;\ntry { flat = SystemParameters.FlatMenu; }\ncatch (Win32Exception ex)\n{\n    Trace.TraceWarning($\"FlatMenu SPI failed: {ex.NativeErrorCode}\");\n    flat = false;\n}","preventionTips":["Inspect ex.NativeErrorCode since the message is empty","Retry once - the cache slot was invalidated so the next read re-queries","Run in an interactive session with themes enabled","Centralize SPI-dependent reads in a guarded helper"],"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"}