troubleshooting Question

c++ hr 0x800401f0 CoInitialize has not been called. error

Avatar of sydneyguy
sydneyguyFlag for Australia asked on
C++Programming
4 Comments2 Solutions2142 ViewsLast Modified:
have a tree view program that i have been working with which takes data of an manual array and this i am changing so the it will read the data from a database so that i can manipulate the tree to display it as it should be
but it throws up  "0x800401f0 CoInitialize has not been called.  error"

            accessMSdb::ExtractDataDB();
*****  THE load THE TREE VIEW CODE WILL GET REPLACED WITH A CALL TO THE FUNCTION BELOW THIS BUT WHEN IT RUNS IT FALL OVER AT PLACE MARKED
********* // FAILS AT THIS POINT
WHICH THROWS UP THE ERROR "0x800401f0 CoInitialize has not been called.  error"
THIS IS ALL DONE IN VS2003 I HAVE REBUILT THE PROGRAM WITH MAIN AND ONE OTHER CLASS WHICH IS THE SAME CODE AS BELOW THAT FALLS OVER  INTO ANOTHER PROJECT SO IT IS SIMPLY MAIN CALLING THE BELOW CODE , I BUILT THIS IN BOUT MFC FORMS AND CONSOLE AND IT WORKS FINE, SO ITS SOMeTHING TO DO WITH THE WAY THE TREEVIEW IS CONFIGURED OR IS MISSING SOMETHING
WERE DO I GO FROM HERE PLEASE
// this



#include "stdafx.h"
#include ".\accessmsdb.h"
#include  <atldbcli.h>
#include <iostream>
#include <string>

using namespace std;

accessMSdb::accessMSdb(void)
{
}

accessMSdb::~accessMSdb(void)
{
}

int accessMSdb::ExtractDataDB()  //
{

      // TODO: Please replace the sample code below with your own.
    HRESULT hr = 0;
    std::string str;
    int i=0, ret=0;
  //  std::string query;
      
      
      string DAM;
      //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
      //   Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;Password=;
      LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;;Password=;";

                  // To initialize the connection to a database using an OLE DB provider,
            // two ATL classes are needed: CDataSource and CSession.
            CDataSource dbDataSource;
            CSession dbSession;

            // Uses ATL's string conversion macros to convert between character encodings.
            USES_CONVERSION;

            // Open the connection and initialize the data source specified by the passed
            // initialization string.
// FAILS AT THIS POINT
            hr = dbDataSource.OpenFromInitializationString(lpcOleConnect);
              if (FAILED(hr))
            {
                  cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<endl;
            }
            else
            {
                  hr = dbSession.Open(dbDataSource);
                  if (FAILED(hr))
                  {
                        cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<endl;
                  }
                  else
                  {
                        CComVariant var;
                        hr = dbDataSource.GetProperty(DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
                        if (FAILED(hr) || (var.vt == VT_EMPTY))
                        {
                              cout<<DAM<<": No Data Source Name Specified."<<endl;
                        }
                        else
                        {
                              cout<<DAM<<": Successfully connected to database. Data source name:\n  "
                                    <<COLE2T(var.bstrVal)<<endl;
                       
                              // Prepare SQL query.
                              
                  //            LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,ParentTable.a,ParentTable.b,ParentTable.c FROM ParentTable;";
                   //      LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName, ChildTable.ChildId, ChildTable.ChildName FROM ParentTable INNER JOIN ChildTable ON ParentTable.ParentId = ChildTable.ParentId;";
                              
                              LPCOLESTR query = L"SELECT * from bothtaables;";
                              
                              cout<<DAM<<": SQL query:\n  "<<OLE2T(query)<<endl;

                              // Excecute the query and create a record set.
                              CCommand<CDynamicStringAccessor> cmd;
                              hr = cmd.Open(dbSession, query);
                              DBORDINAL colCount = cmd.GetColumnCount();
                              if (SUCCEEDED(hr) && 0 < colCount)
                              {
                                    cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
                                    DBORDINAL cColumns;
                                    DBCOLUMNINFO* rgInfo = NULL;
                                    OLECHAR* pStringsBuffer = NULL;
                                    cmd.GetColumnInfo(&cColumns, &rgInfo, &pStringsBuffer);
                                    for (int col=0; col < (int)colCount; col++)
                                    {
                                          cout<<" | "<<OLE2T(rgInfo[col].pwszName);
                                    }
                                    cout<<endl;

                                    cout<<DAM<<": Fetch the actual data: "<<endl;
                                    int rowCount = 0;
                                    CRowset<CDynamicStringAccessor>* pRS = (CRowset<CDynamicStringAccessor>*)&cmd;
                                    // Loop through the rows in the result set.
                                    while (pRS->MoveNext() == S_OK)
                                    {
                                          //*******************************************************************************************************
                                          // THIS IS WERE THE DATA IS DRAWN OUT OF THE TABLE AND EXPORTED OUT

                                          for (int col=1; col <= (int)colCount; col++)
                                          {
                                                CHAR* szValue = cmd.GetString(col);
                                                cout<<" | "<<szValue;
                                          }
                                          cout<<endl;
                                          rowCount++;
                                    }
                                    cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
                              }                  
                              else
                              {
                              cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
                              }
                        }  
                  }
            }

            dbDataSource.Close();
            dbSession.Close();
            cout<<DAM<<": Cleanup. Done."<<endl;

      return 0;
}
error-cos1.png
ASKER CERTIFIED SOLUTION
Karrtik Iyer
Software Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros