/////////////////////////////////////////////////////////////////
// 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;
}
Exemplo n.º 2
0
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;    
}
/////////////////////////////////////////////////////////////////
// myGetProperty
//
//	This function gets the BOOL value for the specified property
//	and returns the result in *pbValue.
//
/////////////////////////////////////////////////////////////////
HRESULT myGetProperty
	(
	IUnknown *				pIUnknown, 
	REFIID					riid, 
	DBPROPID				dwPropertyID, 
	REFGUID					guidPropertySet, 
	BOOL *					pbValue
	)
{
	HRESULT					hr;
	DBPROPID				rgPropertyIDs[1];
	DBPROPIDSET				rgPropertyIDSets[1];
	
	ULONG					cPropSets					= 0;
	DBPROPSET *				rgPropSets					= NULL;

	IDBProperties *			pIDBProperties				= NULL;
	ISessionProperties *	pISesProps					= NULL;
	ICommandProperties *	pICmdProps					= NULL;
	IRowsetInfo *			pIRowsetInfo				= NULL;

	// Initialize the output value
	*pbValue = FALSE;

	// Set up the property ID array
	rgPropertyIDs[0] = dwPropertyID;
	
	// Set up the Property ID Set
	rgPropertyIDSets[0].rgPropertyIDs	= rgPropertyIDs;
	rgPropertyIDSets[0].cPropertyIDs	= 1;
	rgPropertyIDSets[0].guidPropertySet	= guidPropertySet;

	// Get the property value for this property from the provider, but
	// don't try to display extended error information, since this may
	// not be a supported property: a failure is, in fact, expected if
	// the property is not supported
	if( riid == IID_IDBProperties )
	{
		XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IDBProperties, 
					(void**)&pIDBProperties));
		CHECK_HR(hr = pIDBProperties->GetProperties(
					1,					//cPropertyIDSets
					rgPropertyIDSets,	//rgPropertyIDSets
					&cPropSets,			//pcPropSets
					&rgPropSets			//prgPropSets
					));
	}
	else if( riid == IID_ISessionProperties )
	{
		XCHECK_HR(hr = pIUnknown->QueryInterface(IID_ISessionProperties, 
					(void**)&pISesProps));
		CHECK_HR(hr = pISesProps->GetProperties(
					1,					//cPropertyIDSets
					rgPropertyIDSets,	//rgPropertyIDSets
					&cPropSets,			//pcPropSets
					&rgPropSets			//prgPropSets
					));
	}
	else if( riid == IID_ICommandProperties )
	{
		XCHECK_HR(hr = pIUnknown->QueryInterface(IID_ICommandProperties, 
					(void**)&pICmdProps));
		CHECK_HR(hr = pICmdProps->GetProperties(
					1,					//cPropertyIDSets
					rgPropertyIDSets,	//rgPropertyIDSets
					&cPropSets,			//pcPropSets
					&rgPropSets			//prgPropSets
					));
	}
	else
	{
		XCHECK_HR(hr = pIUnknown->QueryInterface(IID_IRowsetInfo, 
					(void**)&pIRowsetInfo));
		CHECK_HR(hr = pIRowsetInfo->GetProperties(
					1,					//cPropertyIDSets
					rgPropertyIDSets,	//rgPropertyIDSets
					&cPropSets,			//pcPropSets
					&rgPropSets			//prgPropSets
					));
	}

	// Return the value for this property to the caller if
	// it's a VT_BOOL type value, as expected
	if( V_VT(&rgPropSets[0].rgProperties[0].vValue) == VT_BOOL )
		*pbValue = V_BOOL(&rgPropSets[0].rgProperties[0].vValue);

CLEANUP:
	if( rgPropSets )
	{
		CoTaskMemFree(rgPropSets[0].rgProperties);
		CoTaskMemFree(rgPropSets);
	}
	if( pIDBProperties )
		pIDBProperties->Release();
	if( pISesProps )
		pISesProps->Release();
	if( pICmdProps )
		pICmdProps->Release();
	if( pIRowsetInfo )
		pIRowsetInfo->Release();
	return hr;
}
Exemplo n.º 4
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;
}