COM Error
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
COM errors in Delphi surface as EOleSysError or ECOMException, both containing an HResult error code from Windows. They occur when a COM or OLE operation fails — for example, when automating Excel, using shell objects, or calling any Windows COM interface. The HResult code in the exception identifies the exact cause.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- A COM object (e.g., Excel, Word, Internet Explorer) is not installed on the machine
- The COM server is not registered — its DLL or EXE has not been registered with regsvr32
- CoInitialize or CoInitializeEx was not called before using COM in a thread
- An interface pointer is nil or has been released before use
- The COM operation returned a failure HResult (any value where bit 31 is set)
How to Fix It
-
Catch the COM exception and log the ErrorCode: try ... except on E: EOleSysError do ShowMessage('COM error: ' + IntToHex(E.ErrorCode, 8) + ' ' + E.Message); end;
The hexadecimal HResult code and message together tell you exactly what Windows COM reported as the failure reason.
-
For error 0x80040154 (Class not registered), register the COM server: run regsvr32 yourcomponent.dll in an elevated command prompt, or run the application's installer which should register COM components.
COM components must be registered in the Windows registry before any application can use them.
-
Before using any COM interface, call CoInitialize(nil) on the main thread (or CoInitializeEx(nil, COINIT_APARTMENTTHREADED) for apartments). Call CoUninitialize in a finally block when done.
Delphi's Application.Initialize calls CoInitialize automatically for VCL applications, but background threads must call it themselves before using COM.
-
Check all interface variables for nil before calling methods on them. COM interface variables are nil until successfully assigned. Use if Assigned(MyInterface) then before calling methods.
Calling a method on a nil interface pointer causes an access violation, not a COM error — but the cause is still the COM initialization failure.
-
Use the OleCheck procedure around COM calls instead of checking HResult manually: OleCheck(SomeComFunction()). OleCheck raises EOleSysError automatically if the call fails.
OleCheck is defined in ComObj and is the standard Delphi idiom for checking COM HResult return values.
When to Call a Professional
COM errors require reading the HResult code to understand the exact failure. The EOleSysError.ErrorCode property contains the HResult. Common values: 0x80040154 (class not registered), 0x800401F3 (invalid class string), 0x8007007E (DLL not found). Search Microsoft documentation for the specific HResult to understand what failed.
Frequently Asked Questions
What is an HResult and how do I look one up?
An HResult is a 32-bit Windows error code used by COM. If bit 31 is set, it is a failure. To look up an HResult: search the value (e.g., 0x80040154) on docs.microsoft.com or use the free tool 'Error Lookup' in Visual Studio. Many HResults also appear in the Windows SDK header files (winerror.h).
Why do COM errors look different from regular Delphi exceptions?
Regular Delphi exceptions carry a string message. COM errors carry an HResult code, which is a Windows standard — COM is a cross-language technology and integer codes work across all programming languages. Delphi converts the HResult into an EOleSysError with both a numeric code and a translated text message.
Do I need CoInitialize if I use Delphi's built-in COM support like CreateOleObject?
For the main thread of a VCL application, Delphi calls CoInitialize automatically in Application.Initialize. For secondary threads and console applications, you must call CoInitialize or CoInitializeEx yourself before using any COM. Forgetting CoInitialize in a thread is a very common cause of COM errors.