/////////////////////////////////////////////////////////////////
// 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;
}
void WINAPI CAGCEventLogger::ReleaseWorkItem(UINT idMsg, int cParams, LPARAM* rgParams)
{
  switch (idMsg)
  {
    case e_LogEvent:
    {
      PRIVATE_ASSERTE(2 <= cParams);
      IAGCEvent* pEvent = reinterpret_cast<IAGCEvent*>(rgParams[0]);
      HANDLE     hevt   = reinterpret_cast<HANDLE    >(rgParams[1]);
      pEvent->Release();
      if (hevt)
        ::SetEvent(hevt);
      break;
    };
    case e_CloseNTEventLog:
    {
      PRIVATE_ASSERTE(2 <= cParams);
      HANDLE hevt  = reinterpret_cast<HANDLE  >(rgParams[0]);
      HRESULT* phr = reinterpret_cast<HRESULT*>(rgParams[1]);
      SetEvent(hevt);
      break;
    }
    case e_ChangeNTEventLog:
    {
      PRIVATE_ASSERTE(3 <= cParams);
      BSTR bstr    = reinterpret_cast<BSTR    >(rgParams[0]);
      HANDLE hevt  = reinterpret_cast<HANDLE  >(rgParams[1]);
      HRESULT* phr = reinterpret_cast<HRESULT*>(rgParams[2]);
      SetEvent(hevt);
      break;
    }
    case e_CloseDatabase:
    {
      PRIVATE_ASSERTE(2 <= cParams);
      HANDLE hevt  = reinterpret_cast<HANDLE  >(rgParams[0]);
      HRESULT* phr = reinterpret_cast<HRESULT*>(rgParams[1]);
      SetEvent(hevt);
      break;
    }
    case e_ChangeDatabase:
    {
      PRIVATE_ASSERTE(5 <= cParams);
      IDBInitialize* pInit = reinterpret_cast<IDBInitialize*>(rgParams[0]);
      DBID* pidTable       = reinterpret_cast<DBID*         >(rgParams[1]);
      CDBPropSet* pPropSet = reinterpret_cast<CDBPropSet*   >(rgParams[2]);
      HANDLE hevt          = reinterpret_cast<HANDLE        >(rgParams[3]);
      HRESULT* phr         = reinterpret_cast<HRESULT*      >(rgParams[4]);
      pInit->Release();
      SetEvent(hevt);
      break;
    }
  }
}
示例#3
0
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;    
}
HRESULT DoTests
	(
	)
{
	IDBInitialize*	    pIDBInitialize 	= NULL;
    IOpenRowset*        pIOpenRowset    = NULL;
    IRowset*		    pIRowset		= NULL;
	LPWSTR			    pwszTableName   = 	L"customer.csv";
	HRESULT			    hr;


	hr = GetSampprovDataSource( &pIDBInitialize );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "GetSampprovDataSource" );
		goto error;
	}

	hr = GetDBSessionFromDataSource( pIDBInitialize, &pIOpenRowset );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "GetDBSessionFromDataSource" );
		goto error;
	}

	pIDBInitialize->Release();
	pIDBInitialize = NULL;    

	hr = GetRowsetFromDBSession( pIOpenRowset, pwszTableName, &pIRowset );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "GetRowsetFromDBCreateSession" );
		goto error;
	}

    pIOpenRowset->Release();
    pIOpenRowset = NULL;    

	hr = GetDataFromRowset( pIRowset );
	if (FAILED(hr))
	{
		DUMP_ERROR_LINENUMBER();
		DumpErrorHResult( hr, "GetDataFromRowset" );
		goto error;
	}
	pIRowset->Release(); 
	pIRowset = NULL;
	CoFreeUnusedLibraries();

	DumpStatusMsg( "\nDone! ");
	printf("\n\nFor more data from this run, see the log file sampclnt.out\n" );	
	return ResultFromScode( S_OK );
    
error:    
	if (pIRowset) 
		pIRowset->Release();
    if (pIOpenRowset)
        pIOpenRowset->Release();    
    if (pIDBInitialize)
    	pIDBInitialize->Release();	    
	
	return ResultFromScode( hr );
}