dotnet/wpf · error · SystemException

SR.Format(SR.OleServicesContext_oleInitializeFailure, hr)

Error message

SR.Format(SR.OleServicesContext_oleInitializeFailure, hr)

What it means

After the STA check, SetDispatcherThread calls the Win32 OleInitialize API to start COM/OLE services on the thread. If OleInitialize returns a failure HRESULT (e.g. RPC_E_CHANGED_MODE or E_OUTOFMEMORY), a SystemException containing the hr is thrown.

Solutions

  1. Ensure the thread is not previously CoInitializeEx'd in MTA mode; keep apartment mode consistent (STA) for the whole thread lifetime.
  2. Inspect the HRESULT in the message (hr) and address the root COM issue (RPC_E_CHANGED_MODE means an apartment-mode conflict).
  3. Move WPF initialization to a fresh, dedicated STA thread that has no prior COM initialization.
  4. If embedding WPF in a native host, initialize COM as STA (CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)) before loading PresentationCore.

Example fix

// before (host code)
CoInitializeEx(NULL, COINIT_MULTITHREADED); // conflicts with WPF OLE init
CreateWpfUi();

// after
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
CreateWpfUi();
Defensive patterns

Strategy: try-catch

Try / catch

try { InitializeWpfOleServices(); }
catch (SystemException ex) when (ex.Message.Contains("hr"))
{
    // log HRESULT, fall back to non-OLE code path or restart on a clean STA thread
}

Prevention

When it happens

Trigger: OleInitialize returning S_FALSE-treated-as-failure hr such as RPC_E_CHANGED_MODE when the thread apartment was changed concurrently, or out-of-memory conditions during COM initialization, immediately after the STA check passes.

Common situations: Mixed-mode COM hosting where another component called CoInitializeEx with COINIT_MULTITHREADED on the same thread; COM already initialized differently by a host application (e.g. Office add-ins, COM-shimmed plugins); resource exhaustion on loaded systems.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/OleServicesContext.cs:152

    ///  Initialize OleServicesContext that will call Ole initialize for ole services(DragDrop and Clipboard)
    ///  and add the disposed event handler of Dispatcher to clean up resources and uninitalize Ole.
    /// </summary>
    private void SetDispatcherThread()
    {
        int hr;

        if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
        {
            throw new ThreadStateException(SR.OleServicesContext_ThreadMustBeSTA);
        }

        // Initialize Ole services.
        // Balanced with OleUninitialize call in OnDispatcherShutdown.
        hr = OleInitialize();

        if (!NativeMethods.Succeeded(hr))
        {
            throw new SystemException(SR.Format(SR.OleServicesContext_oleInitializeFailure, hr));
        }

        // Add Dispatcher.Shutdown event handler. 
        // We will call ole Uninitialize and clean up the resource when UIContext is terminated.
        Dispatcher.CurrentDispatcher.ShutdownFinished += new EventHandler(OnDispatcherShutdown);
    }

    /// <summary>
    ///  This is a callback when the <see cref="Dispatcher"/> is shut down.
    /// </summary>
    /// <remarks>
    ///  <para>
    ///   This method must be called before shutting down the application on the dispatcher thread. It must be called
    ///   by the same thread running the dispatcher and the thread must have its ApartmentState property set to
    ///   <see cref="ApartmentState.STA"/>.
    ///  </para>
    /// </remarks>
    private void OnDispatcherShutdown(object sender, EventArgs args)

View on GitHub (pinned to 81131a70a4)