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
- Read the exception's resource reason to see which argument failed
- Ensure the hwnd passed to HostProviderFromHandle is non-zero and the window exists
- Pass non-null provider/element/hwnd values to ReturnRawElementProvider
- 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
- Verify window handles are created before hosting providers
- Pass non-null provider/element instances to ReturnRawElementProvider
- Validate arguments at your own API boundary before delegating to AutomationInteropProvider
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
- throw new…
- ' ' is not a valid value for this control.
- File name is null or empty.
- SR.AtLeastOnePropertyMustBeSpecified
- SR.Format(SR.NotAValidValue, str)
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)