/////////////////////////////////////////////////////////////////
// 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;
}
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;    
}
示例#3
0
/********************************************************************
 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;
}
示例#4
0
//--------------------------------------------------------------------
// @mfunc InitializeDSO
//
// @parm [IN]  If the DSO is alreadyIf eReinitialize == REINITIALIZE_YES,
//
// Initializes the DSO.  
// the DSO is uninitialized and then reinitialized if it was already
// initialized when this method was called.  If eReinitialzie == 
// REINITIALIZE_NO, if the DSO has already been initialized, nothing is done,
// if the DSO has not been initialized, initialization is attempted.
//
//--------------------------------------------------------------------
HRESULT	CDataSourceObject::InitializeDSO(EREINITIALIZE eReinitialize, ULONG cExPropSets, DBPROPSET* rgExPropSets)
{
	HRESULT hr = E_FAIL;
	IDBProperties* pIDBProperties	= NULL;
	ULONG cPropSets = 0;
	DBPROPSET* rgPropSets = NULL;

	// We must have IDBInitialize already
	if (!m_pIDBInitialize)
		goto CLEANUP;
	
	// Decide what to do if we are already initialized
	if (m_fInitialized)
	{
		// We stay in same state if the user doesn't want to reinitialize
		if (eReinitialize == REINITIALIZE_NO)
		{
			hr = S_OK;
			goto CLEANUP;
		}
		else // We uninitialize and go on
		{
			CHECK(UninitializeDSO(), S_OK);
		}
	}

	// Build our init options from string passed to us from LTM for this provider
	if(!GetInitProps(&cPropSets, &rgPropSets))
	{
		hr = E_FAIL;
		goto CLEANUP;
	}

	// Get IDBProperties Pointer
	if(!VerifyInterface(m_pIDBInitialize, IID_IDBProperties, DATASOURCE_INTERFACE, (IUnknown**)&pIDBProperties))
	{
		hr = E_NOINTERFACE;
		goto CLEANUP;
	}

	//Set any Extra Properties the user wishes to set on the DataSource
	if(cExPropSets)
	{
		if(FAILED(hr = pIDBProperties->SetProperties(cExPropSets, rgExPropSets)))
			goto CLEANUP;
	}

	//Now, set the Init properties before we Initialize
	if(FAILED(hr = pIDBProperties->SetProperties(cPropSets, rgPropSets)))
		goto CLEANUP;

	// Try to Initialize and set flag accordingly
	if(FAILED(hr = m_pIDBInitialize->Initialize()))
		goto CLEANUP;

	m_fInitialized = TRUE;	

CLEANUP:
	// Only free if we got properties back	
	FreeProperties(&cPropSets, &rgPropSets);

	// Free pIDBProperties
	SAFE_RELEASE(pIDBProperties);
	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;
}
示例#6
0
/*****************************************************************************
 * データベースに接続します。
 *****************************************************************************/
int DbSqlCeClient::open()
{
	HRESULT hr;
	ULONG  count = 0;
	ULONG i, j;

	IDBProperties *pProp = NULL;
	// CompactのConnectionStringの指定可能なオプション数は、3が最大なので固定で確保します。
	DBPROPSET arInitPropSet[PROPSET_MAX] = {0};
	DBPROP    arProp1[4]= {0}, arProp2[10] = {0};

	//
	try{
		// インタフェースの生成
		hr = CoCreateInstance( m_sqlceVerGUID, NULL, CLSCTX_INPROC_SERVER,
				IID_IDBInitialize, (LPVOID*)&m_pDBinit);
		if( FAILED( hr ) )		throw(1);

		// プロパティの取得
		hr = m_pDBinit->QueryInterface(IID_IDBProperties, (void **)&pProp);
		if( FAILED( hr ) )		throw(2);

		// 最大数とDBPROPの配列を設定。
		arInitPropSet[0].cProperties = ARRAYSIZE(arProp1);
		arInitPropSet[0].rgProperties = arProp1;
		arInitPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
		for( i = 0; i < ARRAYSIZE(arProp1); i ++ ){
			VariantInit( &(arProp1[i].vValue) );
		}

		arInitPropSet[1].cProperties = ARRAYSIZE(arProp2);
		arInitPropSet[1].rgProperties = arProp2;
		arInitPropSet[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
		for( i = 0; i < ARRAYSIZE(arProp2); i ++ ){
			VariantInit( &(arProp2[i].vValue) );
		}

		// プロパティの作成
		count = create_open_prop( arInitPropSet, ARRAYSIZE(arInitPropSet) );
		if( 0 == count ){
			throw(3);
		}
		else{
			hr = pProp->SetProperties( count, arInitPropSet );
			// プロパティのメモリ開放
			for( i = 0; i < count; i ++ ){
				for( j = 0; j < arInitPropSet[i].cProperties; j ++ ){
					if( VT_BSTR == arInitPropSet[i].rgProperties[j].vValue.vt ){
						SysFreeString( arInitPropSet[i].rgProperties[j].vValue.bstrVal );
					}
				}
			}
			pProp->Release();

			if( FAILED( hr ) )		throw(4);
		}

		// データベースに接続します。
		hr = m_pDBinit->Initialize();
        if( FAILED( hr ) ){
            throw(5);
        }

		m_IsOpened = true;

		// 次に必要なインタフェースを取得します。
		hr = m_pDBinit->QueryInterface( IID_IDBCreateSession, (void**)&m_pSession);
		if( FAILED( hr ) )		throw(6);

		//
		hr = m_pSession->CreateSession( NULL, IID_IDBCreateCommand,(IUnknown**) &m_pCrtCmd);
		if( FAILED( hr ) )		throw(7);

		//
		hr = m_pCrtCmd->CreateCommand( NULL, IID_ICommandText,(IUnknown**) &m_pCmdtext );
		if( FAILED( hr ) )		throw(8);
	}
	catch(const int err){
		if( m_IsOpened ){
			m_pDBinit->Uninitialize();
            m_IsOpened = false;
		}
		if( m_pCmdtext ){
			m_pCmdtext->Release();
			m_pCmdtext =NULL;
		}
		if( m_pCrtCmd ){
			m_pCrtCmd->Release();
			m_pCrtCmd = NULL;
		}
		if( m_pSession ){
			m_pSession->Release();
			m_pSession = NULL;
		}
		if( pProp ){
			pProp->Release();
			pProp = NULL;
		}
		if( m_pDBinit ){
			m_pDBinit->Release();
			m_pDBinit = NULL;
		}

		return err;
	}

	return 0;
}