Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error: CoInitialize has not been called

Status
Not open for further replies.

AP81

Programmer
Apr 11, 2003
740
AU
I have an multithreaded application which runs fine, until I remove a procedure which is no longer needed. I am removing all the references to ADO (as I am no longer using ADO).

When I remove this procedure, any thread I have will not run. When the thread is executed, it will give the followinf error:
CoInitialize has not been called.

Here is the code, that creates the error when removed:

Code:
procedure TfrmMain.SetADOObjects;
begin
  ADOTableCustomers.ConnectionString := connString;
  ADOTableCustomers.TableName := 'customer_msg';
  ADOTableCustomers.Open;
end;

Here is the basic layout of one of my threads(they are all similar).

Declaration:
Code:
//type for updating items --> runs in it's own thread
type TUpdateItems = class(TThread)
  protected
    procedure Execute; override;
end;



Start the thread
Code:
procedure TfrmMain.cmdPerformOperationClick(Sender: TObject);
var
  ItemThread : TUpdateItems;
begin
if optUpdateItems.Checked then
    begin
    intResult := MessageDlg('You are about to update the database with all item codes ' +
                'from the Quicken database' + #13#10 + #13#10  +
                'If you wish to update the Items codes and descriptions, click OK',
                mtInformation, [mbOK, mbCancel], 0);
    //user presses OK
    if intResult = mrOK then
      begin
        //begin importing items and descriptions
        ItemThread := TupdateItems.Create(True);
        ItemThread.FreeOnTerminate := True;
        ItemThread.Resume;
      end
    else
      exit; //exit procedure
    end;
end;


The actual thread execute:
Code:
//procedure to retrieve all items from the Quicken database
procedure TUpdateItems.Execute;
var
  strTicket : WideString;
  strXMLRequest, strXMLResponse : String;
  //Create the RequestProcessor object
  qbXMLCOM : TRequestProcessor2;
  blnSessionBegun : Boolean;   //utility variable
begin
  frmMain.lblAdminErrorMsg.Caption := '';
  Try //begin error trapping
    //create new instance of TRrequestProcessor
    qbXMLCOM := TRequestProcessor2.Create(nil);
    qbXMLCOM.OpenConnection('','Update Database Inventory');
    strTicket := qbXMLCOM.BeginSession('', QBXMLRP2Lib_TLB.qbFileOpenDoNotCare);
    //session has begun
    blnSessionBegun := True;

    //show status label
    frmMain.taStatus.Visible := True;

    //assign strXMLRequestString to XML to update all items
    strXMLRequest :=  '<?xml version="1.0" encoding="ISO-8859-1"?>' +
                      '<?qbxml version="AU3.0"?>' +
                      '<QBXML>' +
                      '<QBXMLMsgsRq onError="continueOnError">' +
                      '<ItemQueryRq requestID="1">' +
                      '</ItemQueryRq>' +
                      '</QBXMLMsgsRq>' +
                      '</QBXML>';

    //parse XML to Quickbooks qbXML parser
    strXMLResponse := qbXMLCOM.ProcessRequest(strTicket, strXMLRequest);
    qbXMLCOM.EndSession(strTicket);  //end session
    qbXMLCOM.CloseConnection;        //close connection

  Except
    on E : Exception do
      begin
        //close session and connection
        if blnSessionBegun then
          begin
            qbXMLCOM.EndSession(strTicket);
            qbXMLCOM.CloseConnection;
          end;
        frmMain.taStatus.Visible := False;    //hide status label
        frmMain.lblAdminErrorMsg.Caption := 'Error: ' + E.Message;
         exit; //stop further processing
      end;
    end;  {try}
    frmMain.taStatus.Visible := False;    //hide import label
    //process XML datastream from Quickbooks
    frmMain.SaveToXML(strXMLResponse, 'ItemUpdate.xml');
    frmMain.FilterItems;
end;





------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
This one annoyed me a lot too when I first came across it. I'm still not 100% sure what causes it - I thought it was by referencing an ADO object prior to Application.Run was called, but I don't think that's always the case. At any rate, to fix:

Code:
uses
  ActiveX;
begin
  CoInitialize(nil);
end;

Documentation i've come across seems to insist that if you do this, you should also call CoUninitialize; as your application is shutting down and after you've destroyed all of your ADO objects.
 
Griffynm, I understand what you mean but do not know where to add the code:

Code:
uses
  ActiveX;
begin
  CoInitialize(nil);
end;




------------------------------------
There's no place like 127.0.0.1
------------------------------------
 
If you have any [/b]initialization[/b] sections in your units that create any ADO objects, then call it prior to that. If not, then create one in your main form. That should fix it.
 
Hi AP81,

you must call Coinitialize in the thread were you're creating your ADO objects...

don't forget to call CoUninitialize; in the thread's destructor!

--------------------------------------
What You See Is What You Get
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top