CRTSESS.CPP

//-------------------------------------------------------------------- 
// Microsoft OLE DB Sample Provider
// (C) Copyright 1994 - 1996 Microsoft Corporation. All Rights Reserved.
//
// @doc
//
// @module CRTSESS.CPP | IDBCreateSession interface implementation
//

// Includes ------------------------------------------------------------------

#include "headers.h"


// Code ----------------------------------------------------------------------

// CImpIDBCreateSession::CreateSession ------------------------------------------------
//
// @mfunc Creates a new DB Session object from the DSO, and returns the
// requested interface on the newly created object.
//
// @rdesc HRESULT
// @flag S_OK | The method succeeded.
// @flag E_INVALIDARG | ppDBSession was NULL
// @flag DB_E_NOAGGREGATION | pUnkOuter was not NULL (this object does not support
// being aggregated)
// @flag E_FAIL | Provider-specific error. This provider can only create
// one DBSession
// @flag E_OUTOFMEMORY | Out of memory
// @flag E_NOINTERFACE | Could not obtain requested interface on DBSession object
//
STDMETHODIMP CImpIDBCreateSession::CreateSession
(
IUnknown* pUnkOuter, //@parm IN | Controlling IUnknown if being aggregated
REFIID riid, //@parm IN | The ID of the interface
IUnknown** ppDBSession //@parm OUT | A pointer to memory in which to return the interface pointer
)
{
CDBSession* pDBSession = NULL;
HRESULT hr;

// check in-params and NULL out-params in case of error
if (ppDBSession)
*ppDBSession = NULL;
else
return ResultFromScode( E_INVALIDARG );

if (pUnkOuter)
return ResultFromScode( DB_E_NOAGGREGATION );

assert( m_pObj );

// this Data Source object can only create 1 DBSession...
if (m_pObj->m_fDBSessionCreated)
return ResultFromScode( E_FAIL );

// open a DBSession object
pDBSession = new CDBSession( NULL );
if (!pDBSession)
return ResultFromScode( E_OUTOFMEMORY );

// initialize the object
if (!pDBSession->FInit( m_pObj->m_szPath, m_pObj ))
{
delete pDBSession;
return ResultFromScode( E_FAIL );
}

// get requested interface pointer on DBSession
hr = pDBSession->QueryInterface( riid, (void **) ppDBSession );
if (FAILED( hr ))
{
delete pDBSession;
return ResultFromScode( hr );
}

// all went well
m_pObj->m_fDBSessionCreated = TRUE;
return ResultFromScode( S_OK );
}