stride3d/stride · error · ArgumentException

The hwnd cannot be null

Error message

The hwnd cannot be null

What it means

WindowInfo wraps a native window handle. The constructor throws ArgumentException when hwnd is IntPtr.Zero, because a null handle cannot identify any window — subsequent Win32 lookups (FromHwnd) would be meaningless.

Solutions

  1. Check hwnd != IntPtr.Zero before constructing WindowInfo and skip null handles
  2. Re-query the handle if a transient IntPtr.Zero is returned (window may be closing)
  3. Verify the source of the handle (GetForegroundWindow, hook parameters) is valid at call time

Example fix

// before
var info = new WindowInfo(GetForegroundWindow()); // may be IntPtr.Zero
// after
var hwnd = GetForegroundWindow();
if (hwnd != IntPtr.Zero) { var info = new WindowInfo(hwnd); }
Defensive patterns

Strategy: validation

Validate before calling

if (hwnd == IntPtr.Zero) return null;

Type guard

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

Try / catch

try { var w = new WindowInfo(hwnd); } catch (ArgumentException ex) when (ex.ParamName == "hwnd") { /* skip null handle */ }

Prevention

When it happens

Trigger: Constructing WindowInfo with IntPtr.Zero, e.g. from a WinEvent hook or window enumeration callback that received a null handle, or from GetForegroundWindow returning IntPtr.Zero when no window is active.

Common situations: Hook callbacks receiving hwnd=0 during window teardown; race where the window was destroyed between event and handling; calling window APIs before the process has any window.

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/913a3684dc2eb935. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowInfo.cs:33

    {
        private IntPtr hwnd;

        /// <summary>
        /// Initializes a new instance of the <see cref="WindowInfo"/> class.
        /// </summary>
        /// <param name="window">The window represented by this object.</param>
        public WindowInfo([NotNull] Window window)
        {
            Window = window ?? throw new ArgumentNullException(nameof(window));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WindowInfo"/> class.
        /// </summary>
        /// <param name="hwnd">The hwnd of the window represented by this object.</param>
        internal WindowInfo(IntPtr hwnd)
        {
            if (hwnd == IntPtr.Zero) throw new ArgumentException(@"The hwnd cannot be null", nameof(hwnd));
            var window = FromHwnd(hwnd);
            Window = window;
            if (window == null)
                this.hwnd = hwnd;
        }

        /// <summary>
        /// Gets the <see cref="Window"/> represented by this object, if available.
        /// </summary>
        public Window Window { get; }

        /// <summary>
        /// Gets the hwnd of the window represented by this object, if available.
        /// </summary>
        public IntPtr Hwnd => hwnd == IntPtr.Zero && Window != null ? ToHwnd(Window) : hwnd;

        /// <summary>
        /// Gets whether the corresponding window is currently disabled.

View on GitHub (pinned to 96fad776d2)