/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*********************************************************************
*/

#include <windows.h>
#include "cclassf.h"
#include "scriptengine.h"

extern int ObjectCount ;

STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, 
	REFIID riid, LPVOID* ppvObj)
{
	    
/*
Check for aggregation request.  We don't do it.
*/
	if (pUnkOuter != NULL)
	{
		*ppvObj = NULL ;
		return CLASS_E_NOAGGREGATION ;
	}

/*
Query interface as requested.  If found,go happily on our way.
*/

	CScriptEngine *pSE = new CScriptEngine ( ) ;

   	if (pSE->QueryInterface (riid, ppvObj) == NOERROR)
	{
		pSE->Release ( ) ;
		ObjectCount ++ ;
		return NOERROR ;
	}
	else
	{
		delete pSE ;
		return E_NOINTERFACE ;
	}
}



STDMETHODIMP CClassFactory::LockServer(BOOL bLock)
{
	return NOERROR ;
}

CClassFactory::CClassFactory (void)
{
    m_RefCount = 1 ;
	AddRef( ) ;   
    return;
}


CClassFactory::~CClassFactory(void)
{
    return;
}



STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (IsEqualIID(riid, IID_IUnknown)
        || IsEqualIID(riid, IID_IClassFactory))
	{
        *ppv = (LPVOID)this;
		AddRef( ) ;
		return NOERROR;
	}
	else
	{
    	*ppv = NULL ;
    	return  E_NOINTERFACE ;
	}
        
}


STDMETHODIMP_(ULONG) CClassFactory::AddRef(void)
{
    return ++m_RefCount;
}


STDMETHODIMP_(ULONG) CClassFactory::Release(void)
{
	m_RefCount -- ;

    if (m_RefCount == 0)
	{
        delete this;
		return 0 ;
	}

    return m_RefCount ;
}



				  	
