stride3d/stride · error · InvalidOperationException

Invalid window handle.

Error message

Invalid window handle.

What it means

NativeHelper.EnableMinimizeButton throws this InvalidOperationException when the native window handle is IntPtr.Zero, because SetWindowLong/GetWindowLong would otherwise operate on a null handle with no meaningful result. The library validates the handle before touching Win32 style flags.

Solutions

  1. Wait until the window handle exists (override OnSourceInitialized or use the Loaded event) before calling
  2. Check handle != IntPtr.Zero before calling
  3. Resolve the handle with WindowInteropHelper.EnsureHandle() or Handle property first

Example fix

// before
NativeHelper.EnableMinimizeButton(window.Handle);
// after
if (window.Handle != IntPtr.Zero)
    NativeHelper.EnableMinimizeButton(window.Handle);
Defensive patterns

Strategy: validation

Validate before calling

if (handle == IntPtr.Zero)
    return; // or defer until OnSourceInitialized

Type guard

bool HasHandle(IntPtr h) => h != IntPtr.Zero;

Try / catch

try { NativeHelper.EnableMinimizeButton(handle); }
catch (InvalidOperationException ex) { Log.Warn("No valid window handle", ex); }

Prevention

When it happens

Trigger: Calling NativeHelper.EnableMinimizeButton(IntPtr.Zero); passing a Handle property of a Window or control that has not yet been created (source initialized).

Common situations: Calling the helper in a window constructor before the HWND exists; passing IntPtr.Zero as a default/uninitialized value; interop with a window that failed to load.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/d8fc070e6f9ffda1. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Interop/NativeHelper.cs:623

        public const uint WINEVENT_INCONTEXT = 4;
        public const uint WINEVENT_OUTOFCONTEXT = 0;
        // ReSharper restore InconsistentNaming

        #endregion // Constants

        public static bool SetCursorPos(Point pt)
        {
            return SetCursorPos((int)pt.X, (int)pt.Y);
        }

        /// <summary>
        /// Enables window minimize button
        /// </summary>
        /// <param name="handle">Native window handle</param>
        public static void EnableMinimizeButton(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
                throw new InvalidOperationException("Invalid window handle.");

            SetWindowLong(handle, GWL_STYLE, GetWindowLong(handle, GWL_STYLE) | WS_MINIMIZEBOX);
        }

        /// <summary>
        /// Disables window minimize button
        /// </summary>
        /// <param name="handle">Native window handle</param>
        public static void DisableMinimizeButton(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
                throw new InvalidOperationException("Invalid window handle.");

            SetWindowLong(handle, GWL_STYLE, GetWindowLong(handle, GWL_STYLE) & ~WS_MINIMIZEBOX);
        }

        /// <summary>
        /// Enables window maximize button

View on GitHub (pinned to 96fad776d2)