dotnet/wpf · error · COMException

0x80004005

0x80004005

Error message

SR.TextStore_CompositionRejected

What it means

During StartComposition/text-insert handling, TextStore computes the insertion range; if walking to the next insertion position yields a null start pointer, the composition cannot be placed anywhere valid and is rejected with E_FAIL (0x80004005) using SR.TextStore_CompositionRejected.

Solutions

  1. Ensure the control has a valid, editable insertion position before starting composition (focus an editable caret position).
  2. Relax MaxLength/character filtering constraints that can leave no valid insertion point.
  3. In the text service, catch COMException with E_FAIL and cancel the composition gracefully.

Example fix

// caller-side guard
var start = range.GetNextInsertionPosition(LogicalDirection.Forward);
if (start != null) store.SetText(flags, offset1, offset2, text, text.Length, out change);
Defensive patterns

Strategy: try-catch

Validate before calling

var insertion = range.GetNextInsertionPosition(LogicalDirection.Forward); if (insertion == null) { /* no valid insertion point; skip composition */ }

Type guard

bool HasInsertionPosition(TextPointer p) => p?.GetNextInsertionPosition(LogicalDirection.Forward) != null;

Try / catch

try { /* composition path */ } catch (COMException ex) when (ex.ErrorCode == unchecked((int)0x80004005)) { /* cancel composition */ }

Prevention

When it happens

Trigger: An IME calls SetText/InsertTextAtSelection path in TextStore at a position where GetNextInsertionPosition(LogicalDirection.Forward) returns null (e.g. at document end with no valid insertion point), so start == null.

Common situations: Composition into a degenerate or empty document range; text container in a state where no insertion position exists (fully truncated content, MaxLength constraints producing an empty valid range).

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextStore.cs:495

            if (IsTracing)
            {
                IMECompositionTracer.Trace(this, IMECompositionTraceOp.BSetText,
                                            "text:", startIndex, endIndex, cch, new String(text, 0, cch));
            }

            ITextPointer start;
            ITextPointer end;

            GetNormalizedRange(startIndex, endIndex, out start, out end);

            while (start != null && TextPointerBase.IsBeforeFirstTable(start))
            {
                start = start.GetNextInsertionPosition(LogicalDirection.Forward);
            }

            if (start == null)
            {
                throw new COMException(SR.TextStore_CompositionRejected, NativeMethods.E_FAIL);
            }

            if (start.CompareTo(end) > 0)
            {
                end = start;
            }

            string filteredText = FilterCompositionString(new string(text), start.GetOffsetToPosition(end)); // does NOT filter MaxLength.
            if (filteredText == null)
            {
                throw new COMException(SR.TextStore_CompositionRejected, NativeMethods.E_FAIL);
            }

            // Openes a composition undo unit for the composition undo.
            CompositionParentUndoUnit unit = OpenCompositionUndoUnit();
            UndoCloseAction undoCloseAction = UndoCloseAction.Rollback;

            try

View on GitHub (pinned to 81131a70a4)