dotnet/wpf · error · ArgumentNullException
ArgumentNullException (hwnd)
Error message
ArgumentNullException (hwnd)
What it means
The UIAutomation HwndProxyElementProvider constructor asserts and then throws ArgumentNullException when the hwnd argument is HWND.NULL. A proxy element provider must wrap a real window handle; a null/NULL handle cannot identify any automation element.
Solutions
- Check hwnd != HWND.NULL before constructing and skip/skip-proxy when null.
- Verify the target window still exists (IsWindow) before obtaining a provider.
- Re-query the element after the window is recreated or use UIA's element-from-point APIs that handle the no-window case.
Example fix
// before var provider = new HwndProxyElementProvider(hwnd); // hwnd may be NULL // after if (hwnd == NativeMethods.HWND.NULL) return null; var provider = new HwndProxyElementProvider(hwnd);
Defensive patterns
Strategy: type-guard
Validate before calling
if (hwnd == NativeMethods.HWND.NULL)
return null; // no window to proxy Type guard
static bool IsValidHwnd(NativeMethods.HWND hwnd) => hwnd != NativeMethods.HWND.NULL && SafeNativeMethods.IsWindow(hwnd);
Try / catch
try { var p = new HwndProxyElementProvider(hwnd); }
catch (ArgumentNullException) { /* skip; window handle was null */ } Prevention
- Always check Win32 window-lookup results for NULL before wrapping them.
- Re-validate with IsWindow after any async/remote step.
- Handle window teardown races in automation loops.
When it happens
Trigger: Constructing HwndProxyElementProvider (directly or via provider factory code) with NativeMethods.HWND.NULL, e.g. when a preceding window lookup (WindowFromPoint, RealGetWindow, etc.) returned NULL and the result was passed through unchecked.
Common situations: UIA proxying for a window that has already been destroyed; querying UIA at a screen point where no window exists; race with window teardown during automation tests.
Related errors
- ElementNotAvailableException
- Invalid type specified for AutomationIdentifier
- new ArgumentNullException(name)
- throw new ArgumentNullException(nameof(section));
- ' ' is not a valid value for this control.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/09a277b54cf34906.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs:43
IRawElementProviderFragment,
IWindowProvider,
ITransformProvider
{
//------------------------------------------------------
//
// Constructors
//
//------------------------------------------------------
#region Constructors
internal HwndProxyElementProvider( NativeMethods.HWND hwnd )
{
Debug.Assert( hwnd != NativeMethods.HWND.NULL );
if( hwnd == NativeMethods.HWND.NULL )
{
throw new ArgumentNullException( nameof(hwnd));
}
_hwnd = hwnd;
}
#endregion Constructors
//------------------------------------------------------
//
// Interface IRawElementProviderSimple/Fragment/Root
//
//------------------------------------------------------
#region IRawElementProviderSimple
ProviderOptions IRawElementProviderSimple.ProviderOptions
{
getView on GitHub (pinned to 81131a70a4)