"CoInitialize has not been called" when using TXMLDocument

By: Lucas Bantner

Abstract: This FAQ addresses a common error that comes up when using Delphi's XML components inside of a thread


I have an application that uses the XML components. This application worked just fine until I decided to make it multi-threaded. Now I am getting a "CoInitialize has not been called" error when I try and use an XML component.


Microsoft's XML parser, which the XML components use by default, is a COM-based parser, which means that COM needs to be initialized before you can use them. Delphi calls the CoInitialize function for you when an application starts up, but that function only initializes COM on the thread it was called from. You should put a call to CoInitialize at the beginning of your TThread descendant's Execute method and a matching CoUnInitialize at the end.


I have a TXMLDocument component on my form, and I have no problems using it. However, I tried creating a TXMLDocument at runtime, and with the same exact code that worked earlier, I am getting access violations! What is causing this?

Server Response from: ETNASC03