MDAC 2.5 SDK - OLE DB Programmer's Reference
Chapter 27: OLE DB for OLAP Code Samples


 

Creating an Instance of a Data Source Object Directly by Using the Provider's CLSID

This example connects directly to a specific MDP by using the provider's unique class identifier, in this case represented by CLSID_GENOLAP. After obtaining an IID_IDBSchemaRowset pointer, this function proceeds to call the function MDPSchemaSample (the code for which appears in the section "Creating a Schema Rowset") to traverse the schema rowset for the MDP data source object.

//
HRESULT MDPConnectUsingClsId(void)
{
   HRESULT hr;

   // Initialize property values.
   for (ULONG i=0; i < NUMELEM(rgProperties); i++)
      VariantInit(&rgProperties[i].vValue);

   // Create the instance of the MSOLAP provider.
   IDBInitialize *pIDBInitialize = NULL;
   hr = CoCreateInstance(CLSID_MSOLAP, NULL, CLSCTX_INPROC_SERVER, 
      IID_IDBInitialize, (LPVOID*)&pIDBInitialize);

   IDBProperties *pIDBProperties = NULL;
   hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)
                                       &pIDBProperties);

   // Identify property set.
   DBPROPSET rgPropertySet[1];
   DBPROP rgProperties[1];
   rgPropertySet[0].rgProperties = rgProperties;
   rgPropertySet[0].cProperties = 1;
   rgPropertySet[0].guidPropertySet = DBPROPSET_DBINIT;

   // Fill in data source.
   rgProperties[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
   rgProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
   rgProperties[0].colid = DB_NULLID;
   rgProperties[0].vValue.vt = VT_BSTR;
   rgProperties[0].vValue.bstrVal = SysAllocString(L"LocalHost");
   assert(rgProperties[0].vValue.bstrVal);

   hr = pIDBProperties->SetProperties(NUMELEM(rgPropertySet),
  rgPropertySet);

   // Initialize and create the session.
   hr = pIDBInitialize->Initialize();

   IDBCreateSession *pIDBCreateSession = NULL;
   hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession, 
      (void **) &pIDBCreateSession);

   IDBSchemaRowset *pIDBSchemaRowset = NULL;
   hr = pIDBCreateSession->CreateSession(NULL, IID_IDBSchemaRowset, 
      (IUnknown **) &pIDBSchemaRowset);

   IRowset* pIRowset = NULL;
   hr = MDPSchemaSample(pIDBSchemaRowset, (IUnknown**)pIRowset);

   if (pIDBInitialize) pIDBInitialize->Release();
   if (pIDBProperties) pIDBProperties->Release();
   if (pIDBCreateSession) pIDBCreateSession->Release();
   if (pIDBSchemaRowset) pIDBSchemaRowset->Release();
   if (pIRowset) pIRowset->Release();
   return hr;
}