dotnet/wpf · error · ArgumentException

throw new ArgumentException(SR.GetResourceString(reason…

Error message

throw new ArgumentException(SR.GetResourceString(reason, null));

What it means

AutomationInteropProvider.ValidateArgument throws ArgumentException with the resource string identified by 'reason' when a boolean validation condition fails. It guards the public entry points HostProviderFromHandle and ReturnRawElementProvider.

Solutions

  1. Read the exception's resource reason to see which argument failed
  2. Ensure the hwnd passed to HostProviderFromHandle is non-zero and the window exists
  3. Pass non-null provider/element/hwnd values to ReturnRawElementProvider
  4. Add explicit null/IntPtr.Zero checks before calling these provider APIs

Example fix

// before
return AutomationInteropProvider.HostProviderFromHandle(hwnd, provider);
// after
if (hwnd == IntPtr.Zero) throw new ArgumentNullException(nameof(hwnd));
if (provider == null) throw new ArgumentNullException(nameof(provider));
return AutomationInteropProvider.HostProviderFromHandle(hwnd, provider);
Defensive patterns

Strategy: validation

Validate before calling

if (hwnd == IntPtr.Zero) throw new ArgumentNullException(nameof(hwnd));
if (provider == null) throw new ArgumentNullException(nameof(provider));

Type guard

bool IsValidHandle(IntPtr h) => h != IntPtr.Zero;

Try / catch

try { var prov = AutomationInteropProvider.HostProviderFromHandle(hwnd, provider); }
catch (ArgumentException ex) { log.Error($"Invalid provider argument: {ex.Message}"); }

Prevention

When it happens

Trigger: Calling AutomationInteropProvider.HostProviderFromHandle or ReturnRawElementProvider with an invalid argument — e.g. a null or IntPtr.Zero handle, or a null client/provider object — failing the internal ValidateArgument condition.

Common situations: Hosting a custom UIA provider with an uncreated window handle; returning a raw element provider with a null element or null hwnd during window initialization.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7d99c53b4084e801. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs:190

        //
        //  Private Methods
        //
        //------------------------------------------------------

        #region Private Methods

        // Throw an argument Exception with a generic error
        private static void ThrowInvalidArgument(string argName)
        {
            throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));
        }

        // Check that specified condition is true; if not, throw exception
        private static void ValidateArgument(bool cond, string reason)
        {
            if (!cond)
            {
                throw new ArgumentException(SR.GetResourceString(reason, null));
            }
        }

        #endregion Private Methods
    }
}

View on GitHub (pinned to 81131a70a4)