dotnet/wpf · error · ArgumentException
SR.NullHwnd
Error message
SR.NullHwnd
What it means
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.
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.
Example fix
// before
var source = HwndSource.FromHwnd(hwnd); // throws if hwnd == IntPtr.Zero
// after
if (hwnd == IntPtr.Zero)
return null;
var source = HwndSource.FromHwnd(hwnd);
Defensive patterns
Strategy: type-guard
Validate before calling
if (hwnd == IntPtr.Zero) return null; // or EnsureHandle() first var source = HwndSource.FromHwnd(hwnd);
Type guard
static bool HasHandle(Window w) =>
w != null && new WindowInteropHelper(w).Handle != IntPtr.Zero; Try / catch
try
{
var source = HwndSource.FromHwnd(hwnd);
}
catch (ArgumentException)
{
// hwnd was IntPtr.Zero
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- File name is null or empty.
- SR.InvalidAssemblyVersion
- " }} " element found. Expected fixed page element ( }} ).
- ' ' ContentType is not valid.
- ' ' ID is not a valid XSD ID.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a3b0aac7dd8f6cc2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs:673
/// <summary>
/// Returns the HwndSource that corresponds to the specified window.
/// </summary>
/// <param name="hwnd">The window.</param>
/// <returns>The source that corresponds to the specified window.</returns>
///<remarks>
/// Callers must have UIPermission(UIPermissionWindow.AllWindows) to call this API.
///</remarks>
public static HwndSource FromHwnd(IntPtr hwnd)
{
return CriticalFromHwnd(hwnd);
}
internal static HwndSource CriticalFromHwnd(IntPtr hwnd)
{
if (hwnd == IntPtr.Zero)
{
throw new ArgumentException(SR.NullHwnd);
}
HwndSource hwndSource = null;
foreach (PresentationSource source in PresentationSource.CriticalCurrentSources)
{
HwndSource test = source as HwndSource;
if (test != null && test.Handle == hwnd)
{
// Don't hand out a disposed source.
if (!test.IsDisposed)
hwndSource = test;
break;
}
}
return hwndSource;
}
/// <summary>View on GitHub (pinned to 81131a70a4)