{"record":{"id":"913a3684dc2eb935","repo":"stride3d/stride","slug":"the-hwnd-cannot-be-null","errorCode":null,"errorMessage":"The hwnd cannot be null","messagePattern":"The hwnd cannot be null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowInfo.cs","lineNumber":33,"sourceCode":"    {\n        private IntPtr hwnd;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"WindowInfo\"/> class.\n        /// </summary>\n        /// <param name=\"window\">The window represented by this object.</param>\n        public WindowInfo([NotNull] Window window)\n        {\n            Window = window ?? throw new ArgumentNullException(nameof(window));\n        }\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"WindowInfo\"/> class.\n        /// </summary>\n        /// <param name=\"hwnd\">The hwnd of the window represented by this object.</param>\n        internal WindowInfo(IntPtr hwnd)\n        {\n            if (hwnd == IntPtr.Zero) throw new ArgumentException(@\"The hwnd cannot be null\", nameof(hwnd));\n            var window = FromHwnd(hwnd);\n            Window = window;\n            if (window == null)\n                this.hwnd = hwnd;\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"Window\"/> represented by this object, if available.\n        /// </summary>\n        public Window Window { get; }\n\n        /// <summary>\n        /// Gets the hwnd of the window represented by this object, if available.\n        /// </summary>\n        public IntPtr Hwnd => hwnd == IntPtr.Zero && Window != null ? ToHwnd(Window) : hwnd;\n\n        /// <summary>\n        /// Gets whether the corresponding window is currently disabled.","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowInfo.cs#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check hwnd != IntPtr.Zero before constructing WindowInfo and skip null handles","Re-query the handle if a transient IntPtr.Zero is returned (window may be closing)","Verify the source of the handle (GetForegroundWindow, hook parameters) is valid at call time"],"exampleFix":"// before\nvar info = new WindowInfo(GetForegroundWindow()); // may be IntPtr.Zero\n// after\nvar hwnd = GetForegroundWindow();\nif (hwnd != IntPtr.Zero) { var info = new WindowInfo(hwnd); }","handlingStrategy":"validation","validationCode":"if (hwnd == IntPtr.Zero) return null;","typeGuard":"bool HasWindow(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { var w = new WindowInfo(hwnd); } catch (ArgumentException ex) when (ex.ParamName == \"hwnd\") { /* skip null handle */ }","preventionTips":["Check IntPtr.Zero before wrapping handles","Handle transient zero handles from window APIs","Validate handle sources (GetForegroundWindow, hooks)"],"tags":["wpf","native-interop","window-handle"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}