///////////////////////////////////////////////////////////////// // myDoInitialization // // This function sets initialization properties that tell the // provider to prompt the user for any information required to // initialize the provider, then calls the provider's // initialization function. // ///////////////////////////////////////////////////////////////// HRESULT myDoInitialization ( IUnknown * pIUnknown ) { HRESULT hr; IDBInitialize * pIDBInitialize = NULL; IDBProperties * pIDBProperties = NULL; HWND hWnd = GetDesktopWindow(); const ULONG cProperties = 2; DBPROP rgProperties[cProperties]; DBPROPSET rgPropSets[1]; // In order to initialize the DataSource object most providers require // some initialization properties to be set by the consumer. For instance, // these might include the data source to connect to and the user ID and // password to use to establish identity. We will ask the provider to // prompt the user for this required information by setting the following // properties: myAddProperty(&rgProperties[0],DBPROP_INIT_PROMPT,VT_I2,DBPROMPT_COMPLETE); #ifdef _WIN64 myAddProperty(&rgProperties[1],DBPROP_INIT_HWND, VT_I8, (LONG_PTR)hWnd); #else myAddProperty(&rgProperties[1],DBPROP_INIT_HWND, VT_I4, (LONG_PTR)hWnd); #endif rgPropSets[0].rgProperties = rgProperties; rgPropSets[0].cProperties = cProperties; rgPropSets[0].guidPropertySet = DBPROPSET_DBINIT; // Obtain the needed interfaces XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IDBProperties, (void**)&pIDBProperties)); XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IDBInitialize, (void**)&pIDBInitialize)); // If a provider requires initialization properties, it must support the // properties that we are setting (_PROMPT and _HWND). However, some // providers do not need initialization properties and may therefore // not support the _PROMPT and _HWND properties. Because of this, we will // not check the return value from SetProperties hr = pIDBProperties->SetProperties(1, rgPropSets); // Now that we've set our properties, initialize the provider XCHECK_HR(hr = pIDBInitialize->Initialize()); CLEANUP: if( pIDBProperties ) pIDBProperties->Release(); if( pIDBInitialize ) pIDBInitialize->Release(); return hr; }
DWORD CTextildb::Open() { SetCursor(LoadCursor(NULL,IDC_WAIT)); DWORD dr; DWORD rc; HRESULT hr; IDataInitialize * pIDataInitialize = NULL; IDBInitialize * pIDBInitialize = NULL; IDBCreateSession* pIDBCreateSession = NULL; IUnknown * pUnkSession = NULL; ICommandText* pICommandText = NULL; IDBCreateCommand* pICreateCommand = NULL; char cConnectionStr[500]; strcpy(cConnectionStr,"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=bd1.mdb;Persist Security Info=False"); db_CharToWChar(cConnectionStr); for(;;) { dr = ERR_OLEDBFAIL; hr = CoCreateInstance(CLSID_MSDAINITIALIZE,NULL,CLSCTX_INPROC_SERVER,IID_IDataInitialize,(VOID **)&pIDataInitialize); if (hr != S_OK) break; hr = pIDataInitialize->GetDataSource(NULL,CLSCTX_INPROC_SERVER,(LPCOLESTR)cConnectionStr,IID_IDBInitialize,(IUnknown **)&pIDBInitialize); if (hr != S_OK) break; hr = pIDBInitialize->Initialize(); if (hr != S_OK) break; hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession, (void**)&pIDBCreateSession); if (hr != S_OK) break; hr = pIDBCreateSession->CreateSession(NULL,IID_IOpenRowset,&pUnkSession ); if (hr != S_OK) break; hr = pUnkSession->QueryInterface(IID_IDBCreateCommand,(void**)&pICreateCommand); if (hr != S_OK) break; hr = pICreateCommand->CreateCommand(NULL,IID_ICommand,(IUnknown**)&m_lpICommand); if (hr != S_OK) break; dr = ERR_NONE; break; }; if (pIDataInitialize) rc = pIDataInitialize->Release(); if (pIDBInitialize) rc = pIDBInitialize->Release(); if (pIDBCreateSession) rc = pIDBCreateSession->Release(); if (pUnkSession) rc = pUnkSession->Release(); if (pICreateCommand) rc = pICreateCommand->Release(); if (hr != S_OK) { if (m_lpICommand) rc = m_lpICommand->Release(); m_lpICommand = NULL; } SetCursor(LoadCursor(NULL,IDC_ARROW)); return ERR_OLEDBFAIL; }
HRESULT GetSampprovDataSource ( IDBInitialize** ppIDBInitialize_out ) { IDBInitialize* pIDBInit = NULL; IDBProperties* pIDBProperties = NULL; DBPROPSET dbPropSet[1]; DBPROP dbProp[1]; HRESULT hr; DumpStatusMsg( "Connecting to the SampProv sample data provider...\n" ); assert(ppIDBInitialize_out != NULL); VariantInit(&(dbProp[0].vValue)); // Create an instance of the SampProv sample data provider hr = CoCreateInstance( CLSID_SampProv, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void **)&pIDBInit ); if (FAILED(hr)) { DUMP_ERROR_LINENUMBER(); DumpErrorHResult( hr, "CoCreateInstance" ); goto error; } // Initialize this provider with the path to the customer.csv file dbPropSet[0].rgProperties = &dbProp[0]; dbPropSet[0].cProperties = 1; dbPropSet[0].guidPropertySet = DBPROPSET_DBINIT; dbProp[0].dwPropertyID = DBPROP_INIT_DATASOURCE; dbProp[0].dwOptions = DBPROPOPTIONS_REQUIRED; dbProp[0].colid = DB_NULLID; V_VT(&(dbProp[0].vValue)) = VT_BSTR; V_BSTR(&(dbProp[0].vValue)) = SysAllocString( L"." ); if ( NULL == V_BSTR(&(dbProp[0].vValue)) ) { DUMP_ERROR_LINENUMBER(); DumpErrorMsg( "SysAllocString failed\n" ); goto error; } hr = pIDBInit->QueryInterface( IID_IDBProperties, (void**)&pIDBProperties); if (FAILED(hr)) { DUMP_ERROR_LINENUMBER(); DumpErrorHResult( hr, "IDBInitialize::QI for IDBProperties"); goto error; } hr = pIDBProperties->SetProperties( 1, &dbPropSet[0]); if (FAILED(hr)) { DUMP_ERROR_LINENUMBER(); DumpErrorHResult( hr, "IDBProperties::SetProperties" ); goto error; } hr = pIDBInit->Initialize(); if (FAILED(hr)) { DUMP_ERROR_LINENUMBER(); DumpErrorHResult( hr, "IDBInitialize::Initialize" ); goto error; } *ppIDBInitialize_out = pIDBInit; hr = ResultFromScode( S_OK ); error: VariantClear( &(dbProp[0].vValue) ); if( pIDBProperties ) pIDBProperties->Release(); if( FAILED(hr) ) { if (pIDBInit) pIDBInit->Release(); *ppIDBInitialize_out = NULL; } return hr; }
///////////////////////////////////////////////////////////////// // myCreateDataSource // // This function creates an OLE DB DataSource object for a // provider selected by the user, sets initialization properties // for the DataSource, and initializes the DataSource. The // function returns a pointer to the DataSource object's // IUnknown in *ppUnkDataSource. // ///////////////////////////////////////////////////////////////// HRESULT myCreateDataSource ( IUnknown ** ppUnkDataSource ) { HRESULT hr; IDataInitialize * pIDataInitialize = NULL; IDBPromptInitialize * pIDBPromptInitialize = NULL; IDBInitialize * pIDBInitialize = NULL; CLSID clsid = CLSID_MSDASQL; // Use the Microsoft Data Links UI to create the DataSource // object; this will allow the user to select the provider // to connect to and to set the initialization properties // for the DataSource object, which will be created by the // Data Links UI. if( g_dwFlags & USE_PROMPTDATASOURCE ) { // Create the Data Links UI object and obtain the // IDBPromptInitialize interface from it XCHECK_HR(hr = CoCreateInstance( CLSID_DataLinks, //clsid -- Data Links UI NULL, //pUnkOuter CLSCTX_INPROC_SERVER, //dwClsContext IID_IDBPromptInitialize, //riid (void**)&pIDBPromptInitialize //ppvObj )); // Invoke the Data Links UI to allow the user to select // the provider and set initialization properties for // the DataSource object that this will create XCHECK_HR(hr = pIDBPromptInitialize->PromptDataSource( NULL, //pUnkOuter GetDesktopWindow(), //hWndParent DBPROMPTOPTIONS_PROPERTYSHEET, //dwPromptOptions 0, //cSourceTypeFilter NULL, //rgSourceTypeFilter NULL, //pwszszzProviderFilter IID_IDBInitialize, //riid (IUnknown**)&pIDBInitialize //ppDataSource )); // We've obtained a DataSource object from the Data Links UI. This // object has had its initialization properties set, so all we // need to do is Initialize it XCHECK_HR(hr = pIDBInitialize->Initialize()); } // We are not using the Data Links UI to create the DataSource object. // Instead, we will enumerate the providers installed on this system // through the OLE DB Enumerator and will allow the user to select // the ProgID of the provider for which we will create a DataSource // object. else { // Use the OLE DB Enumerator to obtain a rowset of installed providers, // then allow the user to select a provider from this rowset CHECK_HR(hr = myCreateEnumerator(CLSID_OLEDB_ENUMERATOR, &clsid)); // We will create the DataSource object through the OLE DB service // component IDataInitialize interface, so we need to create an // instance of the data initialization object XCHECK_HR(hr = CoCreateInstance( CLSID_MSDAINITIALIZE, //clsid -- data initialize NULL, //pUnkOuter CLSCTX_INPROC_SERVER, //dwClsContext IID_IDataInitialize, //riid (void**)&pIDataInitialize //ppvObj )); // Use IDataInitialize::CreateDBInstance to create an uninitialized // DataSource object for the chosen provider. By using this service // component method, the service component manager can provide // additional functionality beyond what is natively supported by the // provider if the consumer requests that functionality XCHECK_HR(hr = pIDataInitialize->CreateDBInstance( clsid, //clsid -- provider NULL, //pUnkOuter CLSCTX_INPROC_SERVER, //dwClsContext NULL, //pwszReserved IID_IDBInitialize, //riid (IUnknown**)&pIDBInitialize //ppDataSource )); // Initialize the DataSource object by setting any required // initialization properties and calling IDBInitialize::Initialize CHECK_HR(hr = myDoInitialization(pIDBInitialize)); } CLEANUP: *ppUnkDataSource = pIDBInitialize; if( pIDataInitialize ) pIDataInitialize->Release(); if( pIDBPromptInitialize ) pIDBPromptInitialize->Release(); return hr; }
/******************************************************************** SqlConnectDatabase - establishes a connection to a database NOTE: wzInstance is optional if fIntegratedAuth is set then wzUser and wzPassword are ignored ********************************************************************/ extern "C" HRESULT DAPI SqlConnectDatabase( __in_z LPCWSTR wzServer, __in_z LPCWSTR wzInstance, __in_z LPCWSTR wzDatabase, __in BOOL fIntegratedAuth, __in_z LPCWSTR wzUser, __in_z LPCWSTR wzPassword, __out IDBCreateSession** ppidbSession ) { Assert(wzServer && wzDatabase && *wzDatabase && ppidbSession); HRESULT hr = S_OK; IDBInitialize* pidbInitialize = NULL; IDBProperties* pidbProperties = NULL; LPWSTR pwzServerInstance = NULL; DBPROP rgdbpInit[4]; DBPROPSET rgdbpsetInit[1]; ULONG cProperties = 0; memset(rgdbpInit, 0, sizeof(rgdbpInit)); memset(rgdbpsetInit, 0, sizeof(rgdbpsetInit)); //obtain access to the SQLOLEDB provider hr = ::CoCreateInstance(CLSID_SQLOLEDB, NULL, CLSCTX_INPROC_SERVER, IID_IDBInitialize, (LPVOID*)&pidbInitialize); ExitOnFailure(hr, "failed to create IID_IDBInitialize object"); // if there is an instance if (wzInstance && *wzInstance) { hr = StrAllocFormatted(&pwzServerInstance, L"%s\\%s", wzServer, wzInstance); } else { hr = StrAllocString(&pwzServerInstance, wzServer, 0); } ExitOnFailure(hr, "failed to allocate memory for the server instance"); // server[\instance] rgdbpInit[cProperties].dwPropertyID = DBPROP_INIT_DATASOURCE; rgdbpInit[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED; rgdbpInit[cProperties].colid = DB_NULLID; ::VariantInit(&rgdbpInit[cProperties].vValue); rgdbpInit[cProperties].vValue.vt = VT_BSTR; rgdbpInit[cProperties].vValue.bstrVal = ::SysAllocString(pwzServerInstance); ++cProperties; // database rgdbpInit[cProperties].dwPropertyID = DBPROP_INIT_CATALOG; rgdbpInit[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED; rgdbpInit[cProperties].colid = DB_NULLID; ::VariantInit(&rgdbpInit[cProperties].vValue); rgdbpInit[cProperties].vValue.vt = VT_BSTR; rgdbpInit[cProperties].vValue.bstrVal= ::SysAllocString(wzDatabase); ++cProperties; if (fIntegratedAuth) { // username rgdbpInit[cProperties].dwPropertyID = DBPROP_AUTH_INTEGRATED; rgdbpInit[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED; rgdbpInit[cProperties].colid = DB_NULLID; ::VariantInit(&rgdbpInit[cProperties].vValue); rgdbpInit[cProperties].vValue.vt = VT_BSTR; rgdbpInit[cProperties].vValue.bstrVal = ::SysAllocString(L"SSPI"); // default windows authentication ++cProperties; } else { // username rgdbpInit[cProperties].dwPropertyID = DBPROP_AUTH_USERID; rgdbpInit[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED; rgdbpInit[cProperties].colid = DB_NULLID; ::VariantInit(&rgdbpInit[cProperties].vValue); rgdbpInit[cProperties].vValue.vt = VT_BSTR; rgdbpInit[cProperties].vValue.bstrVal = ::SysAllocString(wzUser); ++cProperties; // password rgdbpInit[cProperties].dwPropertyID = DBPROP_AUTH_PASSWORD; rgdbpInit[cProperties].dwOptions = DBPROPOPTIONS_REQUIRED; rgdbpInit[cProperties].colid = DB_NULLID; ::VariantInit(&rgdbpInit[cProperties].vValue); rgdbpInit[cProperties].vValue.vt = VT_BSTR; rgdbpInit[cProperties].vValue.bstrVal = ::SysAllocString(wzPassword); ++cProperties; } // put the properties into a set rgdbpsetInit[0].guidPropertySet = DBPROPSET_DBINIT; rgdbpsetInit[0].rgProperties = rgdbpInit; rgdbpsetInit[0].cProperties = cProperties; // create and set the property set hr = pidbInitialize->QueryInterface(IID_IDBProperties, (LPVOID*)&pidbProperties); ExitOnFailure(hr, "failed to get IID_IDBProperties object"); hr = pidbProperties->SetProperties(1, rgdbpsetInit); ExitOnFailure(hr, "failed to set properties"); //initialize connection to datasource hr = pidbInitialize->Initialize(); ExitOnFailure1(hr, "failed to initialize connection to database: %ls", wzDatabase); hr = pidbInitialize->QueryInterface(IID_IDBCreateSession, (LPVOID*)ppidbSession); LExit: for (; 0 < cProperties; cProperties--) { ::VariantClear(&rgdbpInit[cProperties - 1].vValue); } ReleaseObject(pidbProperties); ReleaseObject(pidbInitialize); ReleaseStr(pwzServerInstance); return hr; }
//--------------------------------------------------------------------------- // The routine creates a Data Source object with ITransactionDispenser interface, // initialize it and create a table. // // @mfunc Init // // @rdesc Success or Failure // //--------------------------------------------------------------------------- BOOL CTransaction::Init(CTestCases * pTestCase, CTable * pCTable) { BOOL fInitSuccess = FALSE; IDBInitialize * pIDBInitialize = NULL; IDBProperties * pIDBProperties = NULL; ULONG cPropertySets = 0; DBPROPSET * rgPropertySets = NULL; if (!pTestCase) { if (!CTestCases::Init()) return FALSE; //Skip Transactions if using CONF_STRICT if(!IsUsableInterface(SESSION_INTERFACE, IID_ITransactionLocal)) return FALSE; // Get IDBInitialize Pointer if(!CHECK(GetModInfo()->CreateProvider(NULL, IID_IDBInitialize, (IUnknown**)&pIDBInitialize), S_OK)) return FALSE; // Get the Initialize parameters from LTM for this provider if(!GetInitProps(&cPropertySets, &rgPropertySets)) goto END; // Get IDBProperties Pointer if(!VerifyInterface(pIDBInitialize, IID_IDBProperties, DATASOURCE_INTERFACE, (IUnknown**)&pIDBProperties)) goto END; // Set the properties before we Initialize if (!CHECK(pIDBProperties->SetProperties(cPropertySets,rgPropertySets), S_OK)) goto END; // Initialize and Check to see if the Initialize FAILED if(!CHECK(m_hr = pIDBInitialize->Initialize(),S_OK)) goto END; // Get IDBCreateSession Pointer if(!VerifyInterface(pIDBInitialize, IID_IDBCreateSession, DATASOURCE_INTERFACE, (IUnknown**)&m_pIDBCreateSession)) goto END; // Create a DB Session object, asking for ITransactionLocal pointer if(FAILED(m_hr=m_pIDBCreateSession->CreateSession(NULL, IID_ITransactionLocal, (IUnknown**)&m_pITransactionLocal))) { // m_pITransactionDispenser should be NULL assert(!m_pITransactionLocal); CHECK(m_hr, E_NOINTERFACE); odtLog<<wszTransactionNotSupported; goto END; } //IOpenRowset if(!VerifyInterface(m_pITransactionLocal, IID_IOpenRowset, SESSION_INTERFACE, (IUnknown**)&m_pIOpenRowset)) goto END; //IDBCreateCommand (optional) if(VerifyInterface(m_pITransactionLocal, IID_IDBCreateCommand, SESSION_INTERFACE, (IUnknown**)&m_pIDBCreateCommand)) { //Create a ICommandProperties Object if(!CHECK(m_pIDBCreateCommand->CreateCommand(NULL, IID_ICommand, (IUnknown **)&m_pICommand), S_OK)) goto END; } // Create a Table if (!(m_pCTable = new CTable(m_pIOpenRowset, m_pwszTestCaseName, USENULLS))) goto END; // Insert 30 rows into the table if (!CHECK(m_pCTable->CreateTable(TRANSACTION_ROW_COUNT,1,NULL,PRIMARY,TRUE),S_OK)) goto END; } else { m_fInTestCase = TRUE; //Skip Transactions if using CONF_STRICT if(!IsUsableInterface(SESSION_INTERFACE, IID_ITransactionLocal)) return FALSE; // Get IDBCreateSession Pointer if (!m_pIDBCreateSession) m_pIDBCreateSession = (IDBCreateSession *)pTestCase->m_pThisTestModule->m_pIUnknown; if (!COMPARE(m_pIDBCreateSession != NULL, TRUE)) goto END; //ITransactionLocal if (!m_pITransactionLocal && !VerifyInterface(pTestCase->m_pThisTestModule->m_pIUnknown2, IID_ITransactionLocal, SESSION_INTERFACE, (IUnknown**)&m_pITransactionLocal)) { // m_pITransaction should be NULL assert(!m_pITransactionLocal); CHECK(m_hr, E_NOINTERFACE); odtLog<<wszTransactionNotSupported; goto END; } //IOpenRowset if(!m_pIOpenRowset && !VerifyInterface(m_pITransactionLocal, IID_IOpenRowset, SESSION_INTERFACE, (IUnknown**)&m_pIOpenRowset)) goto END; //IDBCreateCommand (optional) if(!m_pIDBCreateCommand && VerifyInterface(m_pITransactionLocal, IID_IDBCreateCommand, SESSION_INTERFACE, (IUnknown**)&m_pIDBCreateCommand)) { //Create a ICommandProperties Object if(!m_pICommand && !CHECK(m_pIDBCreateCommand->CreateCommand(NULL, IID_ICommand, (IUnknown **)&m_pICommand), S_OK)) goto END; } //Get the table if (!COMPARE(pCTable != NULL, TRUE)) goto END; m_pCTable = pCTable; } fInitSuccess = TRUE; END: SAFE_RELEASE(pIDBInitialize); SAFE_RELEASE(pIDBProperties); FreeProperties(&cPropertySets,&rgPropertySets); return fInitSuccess; }