/*
*********************************************************************
*
*	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
*
*	File Name: ICLASSF.CPP
*
*	Purpose: Class factory for OLE Auomation server app
*
*********************************************************************
*/

#include "iclassf.h"
#include "hellodisp.h"

CHelloCF::CHelloCF()
{ 
    m_RefCount = 1; 
}


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


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

STDMETHODIMP_(ULONG) CHelloCF::Release()
{
    if(--m_RefCount == 0) 
    {
       delete this;
       return 0;
    }
    return m_RefCount;
}





STDMETHODIMP CHelloCF::CreateInstance(IUnknown FAR* pUnkOuter,
    REFIID riid,  void FAR* FAR* ppv)
{

	if (pUnkOuter != NULL && riid != IID_IUnknown)
	{
		return CLASS_E_NOAGGREGATION ;
	}
/*
Create a new CHello object, passing the outer IUnknown we are given 
as an aggregator. It might be NULL if this object is being used standalone.
*/

	CAggrHelloDispatch * pHello = new CAggrHelloDispatch (pUnkOuter) ;

/*
Query interface to see if new object supports requested interface.  
If found, go happily on our way.
*/

   	if (pHello->QueryInterface (riid, ppv) == NOERROR)
	{
		pHello->Release ( ) ;
		return NOERROR ;
	}
/*
Object does not support requested interface. Delete it and return an
error code.
*/
	else
	{
		delete pHello ;
		return E_NOINTERFACE ;
	}
}

STDMETHODIMP CHelloCF::LockServer(BOOL fLock)
{
	MessageBox (NULL, "LockServer", "", 0) ;
    return NOERROR;
}
