dotnet/wpf · error · COMException

E_INVALIDARG

E_INVALIDARG

Error message

Count and contents of attributes passed to IFilter::Init do not match.

What it means

XpsFilter.Init validates its attribute arguments: if cAttributes > 0 but aAttributes is null, the count and the array do not match, so it throws COMException with E_INVALIDARG before delegating to the inner filter's Init.

Solutions

  1. Pass aAttributes as null together with cAttributes == 0
  2. Populate the FULLPROPSPEC array so its length matches cAttributes
  3. Guard the call: if the attribute array is null, pass 0 for cAttributes

Example fix

// before
filter.Init(flags, requestedAttributes.Count, null);
// after
filter.Init(flags, requestedAttributes.Count, requestedAttributes.ToArray());
Defensive patterns

Strategy: validation

Validate before calling

if (attrs == null) cAttributes = 0;
else if (cAttributes > 0 && attrs.Length == 0) cAttributes = 0;

Try / catch

try { filter.Init(flags, cAttrs, attrs); }
catch (COMException ex) when (ex.ErrorCode == unchecked((int)0x80070057)) { /* retry with cAttrs=0, null */ }

Prevention

When it happens

Trigger: Calling IFilter.Init with cAttributes set to a positive value while passing null for the FULLPROPSPEC[] array.

Common situations: Callers building the attribute list conditionally but forgetting to zero cAttributes when the array ends up null; interop marshalling inconsistencies.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs:61

        /// <returns>
        /// IFILTER_FLAGS_NONE to indicate that the caller should not use the IPropertySetStorage
        /// and IPropertyStorage interfaces to locate additional properties.
        /// </returns>
        IFILTER_FLAGS IFilter.Init(
            [In] IFILTER_INIT grfFlags,
            [In] uint cAttributes,
            [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] FULLPROPSPEC[] aAttributes)
        {
            if (_filter == null)
            {
                throw new COMException(SR.FileToFilterNotLoaded,
                    (int)NativeMethods.E_FAIL);
            }

            if (cAttributes > 0 && aAttributes == null)
            {
                // Attributes count and array do not match.
                throw new COMException(SR.FilterInitInvalidAttributes,
                    (int)NativeMethods.E_INVALIDARG);
            }

            return _filter.Init(grfFlags, cAttributes, aAttributes);
        }

        /// <summary>
        /// Returns description of the next chunk.
        /// </summary>
        /// <returns>Chunk descriptor</returns>
        STAT_CHUNK IFilter.GetChunk()
        {
            if (_filter == null)
            {
                throw new COMException(SR.FileToFilterNotLoaded,
                    (int)FilterErrorCode.FILTER_E_ACCESS);
            }

View on GitHub (pinned to 81131a70a4)