//--------------------------------------------------------------------
// Microsoft OLE DB Sample Provider
// (C) Copyright 1994 - 1996 Microsoft Corporation. All Rights Reserved.
//
// @doc
//
// @module DATASRC.CPP | CDataSource object implementation
//
//
// Includes ------------------------------------------------------------------
#include "headers.h"
// Code ----------------------------------------------------------------------
// CDataSource::CDataSource --------------------------------------------------
//
// @mfunc Constructor for this class
//
// @rdesc NONE
//
CDataSource::CDataSource
(
LPUNKNOWN pUnkOuter //@parm IN | Outer Unkown Pointer
)
{
// Initialize simple member vars
m_cRef = 0L;
m_pUnkOuter = pUnkOuter;
*m_szPath = '\0';
m_fDSOInitialized = FALSE;
m_fDBSessionCreated = FALSE;
m_pUtilProp = NULL;
// Initially, NULL all contained interfaces
m_pIDBInitialize = NULL;
m_pIDBProperties= NULL;
m_pIDBInfo= NULL;
m_pIDBCreateSession = NULL;
// Increment global object count.
OBJECT_CONSTRUCTED();
return;
}
// CDataSource::~CDataSource -------------------------------------------------
//
// @mfunc Destructor for this class
//
// @rdesc NONE
//
CDataSource:: ~CDataSource
(
void
)
{
ULONGulRefCount;
// Decrement the ref count on the data conversion object
if( g_pIDataConvert )
{
ulRefCount = g_pIDataConvert->Release();
// Is it gone for good?
if( !ulRefCount )
g_pIDataConvert = NULL;
}
// Free properties management object
delete m_pUtilProp;
// Free contained interfaces
delete m_pIDBInitialize;
delete m_pIDBProperties;
delete m_pIDBInfo;
delete m_pIDBCreateSession;
// Decrement global object count.
OBJECT_DESTRUCTED();
return;
}
// CDataSource::FInit --------------------------------------------------------
//
// @mfunc Initialize the command Object
//
// @rdesc Did the Initialization Succeed
// @flag TRUE | Initialization succeeded
// @flag FALSE | Initialization failed
//
BOOL CDataSource::FInit
(
void
)
{
HRESULThr;
LPUNKNOWN pIUnknown = (LPUNKNOWN) this;
if (m_pUnkOuter)
pIUnknown = m_pUnkOuter;
// Instantiate the data conversion service object
if( !g_pIDataConvert )
{
hr = CoCreateInstance(CLSID_OLEDB_CONVERSIONLIBRARY,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDataConvert,
(void **)&g_pIDataConvert);
if( FAILED(hr) )
return FALSE;
}
else
// Already instantiated, increment reference count
g_pIDataConvert->AddRef();
// Allocate properties management object
m_pUtilProp = new CUtilProp();
// Allocate contained interface objects
m_pIDBInitialize = new CImpIDBInitialize( this, pIUnknown );
m_pIDBProperties= new CImpIDBProperties( this, pIUnknown );
m_pIDBInfo= new CImpIDBInfo( this, pIUnknown );
m_pIDBCreateSession = new CImpIDBCreateSession( this, pIUnknown );
return (BOOL) (m_pIDBInitialize &&
m_pIDBInfo &&
m_pIDBProperties&&
m_pIDBCreateSession);
}
// CDataSource::QueryInterface -----------------------------------------------
//
// @mfunc Returns a pointer to a specified interface. Callers use
// QueryInterface to determine which interfaces the called object
// supports.
//
// @rdesc HRESULT indicating the status of the method
// @flag S_OK | Interface is supported and ppvObject is set.
// @flag E_NOINTERFACE | Interface is not supported by the object
// @flag E_INVALIDARG | One or more arguments are invalid.
//
STDMETHODIMP CDataSource::QueryInterface
(
REFIID riid, //@parm IN | Interface ID of the interface being queried for.
LPVOID * ppv //@parm OUT | Pointer to interface that was instantiated
)
{
// Is the pointer bad?
if (ppv == NULL)
return ResultFromScode( E_INVALIDARG );
// Place NULL in *ppv in case of failure
*ppv = NULL;
// This is the non-delegating IUnknown implementation
if (riid == IID_IUnknown)
*ppv = (LPVOID) this;
else if (riid == IID_IDBInitialize)
*ppv = (LPVOID) m_pIDBInitialize;
else if (riid == IID_IDBInfo)
*ppv = (LPVOID) m_pIDBInfo;
else if (riid == IID_IDBProperties)
*ppv = (LPVOID) m_pIDBProperties;
else if (riid == IID_IDBCreateSession)
*ppv = (LPVOID) m_pIDBCreateSession;
// If we're going to return an interface, AddRef it first
if (*ppv)
{
((LPUNKNOWN) *ppv)->AddRef();
return ResultFromScode( S_OK );
}
else
return ResultFromScode( E_NOINTERFACE );
}
// CDataSource::AddRef -------------------------------------------------------
//
// @mfunc Increments a persistence count for the object
//
// @rdesc Current reference count
//
STDMETHODIMP_( ULONG ) CDataSource::AddRef
(
void
)
{
return ++m_cRef;
}
// CDataSource::Release ------------------------------------------------------
//
// @mfunc Decrements a persistence count for the object and if
// persistence count is 0, the object destroys itself.
//
// @rdesc Current reference count
//
STDMETHODIMP_( ULONG ) CDataSource::Release
(
void
)
{
if (!--m_cRef)
{
delete this;
return 0;
}
return m_cRef;
}