Page 1 of 1

CoInitialize Error on Database Connection

Posted: Tue 12 Sep 2017 07:53
by adeel.aziz
Hi,

I have been trying to integrate DevArt dbExpress component (trial version) in my Delphi 2009 application. When I do so in a separate thread it gives me the following error:
OLE DB error occurred. Code 800401F0h.
CoInitialize has not been called.
So, I added the following code in the Execute method of the thread:

Code: Select all

CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
So, it started working. Previously I was using dbExpress version 10 that comes with Delphi (which uses OLE DB driver for SQL Server). It did not gave me this error. But when I have tried to use DevArt component (which uses SQL Server Native Client 11), the error appeared. My question is that why do I need to call CoInitialize in devArt when I do not need this in the dbExpress component.

Regards,

Adeel.

Re: CoInitialize Error on Database Connection

Posted: Tue 12 Sep 2017 13:12
by azyk
If dbExpress driver uses standard OLEDB provider or SQL Server Native Client to connect to SQL Server, the CoInitialize method should be called before opening a connection. More details about this in Embarcadero documentation: http://docwiki.embarcadero.com/RADStudi ... nformation

Maybe, in a custom project with standard dbExpress driver you passed the already opened TSQLConnection instance to the thread, therefore CoInitialize/CoInitializeEx should not be called in the Execute method.

Note that Devart dbExpress driver for SQL Server has a possibility to connect in the Direct mode without using additional client libraries, i.e. there is no need to call CoInitialize/CoInitializeEx in the Direct mode. To select the Direct mode in Devart dbExpress driver for SQL Server, set the 'DevartSQLServerDirect' value to the TSQLConnection.DriverName property.

You can read more detailed description of the Direct mode in SDAC documentation: https://www.devart.com/sdac/docs/?direct_mode.htm