c++ hr 0x800401f0 CoInitialize has not been called. error
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
// 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;
}
}
}
}