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

  1. Check hwnd != HWND.NULL before constructing and skip/skip-proxy when null.
  2. Verify the target window still exists (IsWindow) before obtaining a provider.
  3. 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

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


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
        {
            get

View on GitHub (pinned to 81131a70a4)