CO_E_NOTINITIALIZED (0X800401F0) – CoInitialize Not Called Fix
This COM error means your code didn't call CoInitialize before using COM objects. Real fix: call CoInitializeEx once per thread.
Quick Answer
Call CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) at the start of each thread that uses COM, and CoUninitialize() before the thread exits. That's it.
Why This Happens
Every time you see error 0x800401F0 (CO_E_NOTINITIALIZED), it means a thread tried to use a COM object without initializing the COM library first. COM isn't automatic—you have to tell Windows, 'Hey, I'm going to use COM on this thread.' If you don't, any CoCreateInstance, CreateObject, or New on a COM class will blow up with this error.
I've seen this most often in old VB6 apps, PowerShell scripts, and custom .NET code that spawns worker threads. Had a client last month whose entire print queue management app crashed every time they kicked off a background job. Turned out the developer forgot to call CoInitialize on the worker thread. One line of code fixed it.
Step-by-Step Fix
- Identify the thread causing the error. Look at your stack trace—it'll show exactly where the COM call failed. If you're in a multi-threaded app, the offending thread is usually a background worker or a timer callback.
- Add CoInitializeEx at the start of that thread's function. Use this exact call before any COM work:
For most single-threaded apps,CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);COINIT_APARTMENTTHREADEDis the safe choice. If you're building a multi-threaded server that creates COM objects from many threads, useCOINIT_MULTITHREADED—but that's rare and requires careful handling. - Pair it with CoUninitialize. When the thread is done with COM (typically at the end of the function), call:
Skipping this can leak resources and cause weird crashes later. I've debugged apps that ran fine for hours then randomly died because of missingCoUninitialize();CoUninitializecalls. - Test the fix. Run the app and reproduce the scenario that triggered the error. If it still fails, double-check that you're calling
CoInitializeExon the same thread that makes the COM call. COM initialization is per-thread—not per-process.
Alternative Fixes
If the main fix doesn't work or you can't modify the code, try these:
- For PowerShell scripts: The
New-Objectcmdlet automatically initializes COM on the main thread. But if you're using[activator]::CreateInstanceor calling COM from a runspace, you need explicit initialization. Wrap the COM code in a script block and useStart-ThreadJobwith the-InitializationScriptparameter that calls[System.Threading.Thread]::BeginThreadAffinity()and[System.Windows.Forms.Application]::OleRequired(). - For .NET apps: If you're using
BackgroundWorkerorTask.Run, callCoInitializeinside the delegate. An easier path: useSTAThreadattribute on the main thread and ensure all background work runs on an STA thread. SetThread.CurrentThread.SetApartmentState(ApartmentState.STA)before starting the thread. - For VB6 or legacy COM clients: Wrap the COM object creation in a
On Error Gotoblock, callOleInitializefromole32.dllas a fallback. But really, the proper fix is to callCoInitializeat your app's startup or in each thread's entry point.
Prevention Tip
The easiest way to avoid this error: make it a habit to call CoInitializeEx at the entry point of every thread that might touch COM—even if you think it doesn't need COM. I do this in every new project's thread pool init code. Also, wrap your COM calls in a try/finally block that ensures CoUninitialize runs. A simple RAII wrapper in C++ or a using block in C# can save you hours of debugging later.
One more thing: if you're using .NET, the CLR calls
CoInitializeautomatically for your main thread if it's STA. But worker threads are MTA by default, so you still need to initialize COM manually. Don't assume the framework handles it.
Was this solution helpful?