{"record":{"id":"a3b0aac7dd8f6cc2","repo":"dotnet/wpf","slug":"sr-nullhwnd","errorCode":null,"errorMessage":"SR.NullHwnd","messagePattern":"SR\\.NullHwnd","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs","lineNumber":673,"sourceCode":"\n        /// <summary>\n        ///     Returns the HwndSource that corresponds to the specified window.\n        /// </summary>\n        /// <param name=\"hwnd\">The window.</param>\n        /// <returns>The source that corresponds to the specified window.</returns>\n        ///<remarks>\n        ///     Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.\n        ///</remarks>\n        public static HwndSource FromHwnd(IntPtr hwnd)\n        {\n            return CriticalFromHwnd(hwnd);\n        }\n\n        internal static HwndSource CriticalFromHwnd(IntPtr hwnd)\n        {\n            if (hwnd == IntPtr.Zero)\n            {\n                throw new ArgumentException(SR.NullHwnd);\n            }\n            HwndSource hwndSource = null;\n            foreach (PresentationSource source in PresentationSource.CriticalCurrentSources)\n            {\n                HwndSource test = source as HwndSource;\n                if (test != null && test.Handle == hwnd)\n                {\n                    // Don't hand out a disposed source.\n                    if (!test.IsDisposed)\n                        hwndSource = test;\n                    break;\n                }\n            }\n            return hwndSource;\n        }\n\n\n        /// <summary>","sourceCodeStart":655,"sourceCodeEnd":691,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs#L655-L691","documentation":"HwndSource.FromHwnd (via CriticalFromHwnd) resolves an HWND to the registered HwndSource presentation source. Passing IntPtr.Zero (a null window handle) is rejected immediately with ArgumentException(SR.NullHwnd) because a zero handle can never match a registered source. The library enforces a valid native window handle as a precondition.","triggerScenarios":"Calling HwndSource.FromHwnd(IntPtr.Zero) — e.g. when a control's Handle has not been created yet, or a cached hwnd variable was never initialized.","commonSituations":"Accessing WindowInteropHelper.Handle or Control.Handle before the window is shown/created; capturing the handle in a field during construction before the HWND exists; passing a default-constructed IntPtr.","solutions":["Ensure the WPF window/control has been shown (handle created) before calling FromHwnd; force creation with new WindowInteropHelper(window).EnsureHandle().","Check hwnd != IntPtr.Zero before calling FromHwnd and handle the null case explicitly.","If the source may not exist for the handle, remember FromHwnd can also return null; only pass handles obtained from a live HwndSource."],"exampleFix":"// before\nvar source = HwndSource.FromHwnd(hwnd); // throws if hwnd == IntPtr.Zero\n// after\nif (hwnd == IntPtr.Zero)\n    return null;\nvar source = HwndSource.FromHwnd(hwnd);\n","handlingStrategy":"type-guard","validationCode":"if (hwnd == IntPtr.Zero) return null; // or EnsureHandle() first\nvar source = HwndSource.FromHwnd(hwnd);","typeGuard":"static bool HasHandle(Window w) =>\n    w != null && new WindowInteropHelper(w).Handle != IntPtr.Zero;","tryCatchPattern":"try\n{\n    var source = HwndSource.FromHwnd(hwnd);\n}\ncatch (ArgumentException)\n{\n    // hwnd was IntPtr.Zero\n}","preventionTips":["Call WindowInteropHelper.EnsureHandle() or show the window before reading its handle.","Never pass default(IntPtr) handles; obtain handles from a live window.","Remember FromHwnd can also return null for unknown handles — check that too."],"tags":["argument","null","hwnd","wpf"],"backgroundTag":"null-argument","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"}