Page 1 of 1

CoInitialize Error with SQLServerUniProvider?

Posted: Wed 02 Oct 2013 17:53
by jonanderson
I'm getting a strange error when attempting to use UNIDAC components in a service on Windows 7. I am using Delphi XE. Other services I have converted to UNIDAC work just fine, but this one in particular is giving me odd errors.

When I do not include SQLServerUniProvider in my uses, I am given an error stating that "You should add the SQLServerUniProvider unit to the uses clause of any unit..."

When I add SQLServerUniProvider in my uses, my service attempts to start, then immediately errors out and says "OLE DB Error occured. Code 800401F0h. CoInitialize has not been called." This error is triggered when I try to set Connected := True during a database connection attempt. This is my example, in which I see the log message "I made it this far!" only to have it error out on the next line.

Code: Select all

      Connected := False;
      Server := dbConfig.Server;
      Database := dbConfig.Database;
      Username := dbConfig.UserID;
      Password := dbConfig.Password;
      ProviderName := 'SQL Server';
      logger.Log('I made it this far!');
      Connected := True;
      logger.Log('I did not make it this far!');
Isn't CoInitialize handled in the background? What is going on here? If I manually add CoInitialize to my code, the service runs successfully, but I am blocked from starting my main application...

Re: CoInitialize Error with SQLServerUniProvider?

Posted: Thu 03 Oct 2013 11:01
by AndreyZ
It is needed to call CoInitialize and CoUnInitialize functions when developing services and console applications. You can use the OnStart or OnStop events. Note that to use the CoInitialize and CoUnInitialize functions, you should add the ActiveX unit to the USES clause of your unit.

Re: CoInitialize Error with SQLServerUniProvider?

Posted: Thu 03 Oct 2013 22:38
by jonanderson
VCL usually calls CoInitialize automatically, doesn't it? This is where my confusion is coming from. I've upgraded a few other services to use UNIDAC (formerly BDE), and I've not once had to manually call this function to get them to start up and run.

Re: CoInitialize Error with SQLServerUniProvider?

Posted: Fri 04 Oct 2013 06:50
by AndreyZ
UniDAC does not call the CoInitialize and CoUninitialize functions directly as it is considered as a bad practice. VCL applications take care of these calls for you, so VCL applications do not require calling CoInitialize and CoUninitialize. You can find more information about this here:
http://edn.embarcadero.com/article/22090
https://forums.embarcadero.com/thread.j ... 8&tstart=0
http://docwiki.embarcadero.com/RADStudi ... nformation