dotnet/wpf · error · System.ComponentModel.Win32Exception

Win32Exception (no message; error code from GetLastError)

Error message

Win32Exception (no message; error code from GetLastError)

What it means

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.

Solutions

  1. Restart/enable the Themes service and rerun in an interactive session.
  2. Catch Win32Exception, inspect NativeErrorCode, and default FlatMenu to false.
  3. Retry the property access; the invalidated cache forces a fresh SPI call next time.
  4. Log the NativeErrorCode to diagnose whether access is denied vs unsupported.

Example fix

// before
bool flat = SystemParameters.FlatMenu;
// after
bool flat;
try { flat = SystemParameters.FlatMenu; }
catch (Win32Exception ex) { Log(ex.NativeErrorCode); flat = false; }
Defensive patterns

Strategy: try-catch

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: 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.

Common situations: Startup theme-metric collection on Windows Server with Themes service stopped; headless CI UI tests; remote sessions without full desktop composition.

Related errors


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

Appendix: source

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

        /// </summary>
        public static bool FlatMenu
        {

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

                        if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETFLATMENU, 0, ref _flatMenu, 0))
                        {
                        }
                        else
                        {
                            _cacheValid[(int)CacheSlot.FlatMenu] = false;
                            throw new Win32Exception();
                        }
                    }
                }

                return _flatMenu;
            }
        }

        /// <summary>
        ///     Maps to SPI_GETWORKAREA
        /// </summary>
        internal static NativeMethods.RECT WorkAreaInternal
        {
            get
            {
                lock (_cacheValid)
                {
                    while (!_cacheValid[(int)CacheSlot.WorkAreaInternal])

View on GitHub (pinned to 81131a70a4)