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


extern long ObjectCount ;

CClassFactory::CClassFactory (void)
{
    m_RefCount = 1 ;
	InterlockedIncrement (&ObjectCount) ;
    return;
}


CClassFactory::~CClassFactory(void)
{
	InterlockedDecrement (&ObjectCount) ;
    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 ;
}


				  	
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 ;
	}

/*
Create a new data object.  QUery interface as requested.  If found,
go happily on our way.
*/

	CShellExt * pSE = new  CShellExt( ) ;

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


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