#include "CMDXToolCF.h"
#include "CMDXTool.h"

HRESULT _stdcall CMDXToolCFQueryInterface( IClassFactory *pCF, REFIID iid, void **ppvObject)
{
CMDXToolCF *pThis = (CMDXToolCF *)pCF;

if( IsEqualIID( iid, &IID_IUnknown) ||IsEqualIID( iid, &IID_IClassFactory) )
	{
	*ppvObject = pThis;
	pThis->icf.lpVtbl->AddRef(pCF );
	return S_OK;
	}
else
	{
	*ppvObject = NULL;
	return E_NOINTERFACE;
	}
}

ULONG _stdcall CMDXToolCFAddRef( IClassFactory *pCF )
{
CMDXToolCF *pThis = (CMDXToolCF *)pCF;

return ++pThis->cRef;
}

ULONG _stdcall CMDXToolCFRelease( IClassFactory *pCF )
{
CMDXToolCF *pThis = (CMDXToolCF *)pCF;

if( --pThis->cRef == 0 )
{
free( pThis );
return 0;
}
else
return pThis->cRef;
}

HRESULT _stdcall CMDXToolCFCreateInstance( IClassFactory *pICF, IUnknown * pUnkOuter, //Pointer to whether object is or isn't part of an aggregate
REFIID riid, /*Reference to the identifier of the interface*/
void ** ppvObject /*indirect pointer to the interface*/
)
{
CMDXToolCF *pThis = (CMDXToolCF *)pICF;
CMDXTool *pCM;


if( pUnkOuter != NULL )
	{
	return CLASS_E_NOAGGREGATION;
	}
pCM = (CMDXTool *)malloc( sizeof( CMDXTool ) );
if( NULL == pCM )
	{
	return E_OUTOFMEMORY;
	}
pCM->Im.lpVtbl = &IMVT;
pCM->cRef = 1;
/*
if( S_OK != pCM->Im.lpBack to the "COM Inproc Server in C" tutorial.tbl->pfnQueryInterface( &(pCM->Im), riid, ppvObject ) )
{
free( pCM );
return E_NOINTERFACE;
}
*/
pCM->Im.lpVtbl->pfnRelease(&(pCM->Im));
return TRUE;
}

HRESULT _stdcall CMDXToolCFLockServer( IClassFactory *pICF, BOOL fLock)
{
return S_OK;
}

struct IClassFactoryVtbl ICFVT = 
	{ 
	CMDXToolCFQueryInterface, 
	CMDXToolCFAddRef, 
	CMDXToolCFRelease, 
	CMDXToolCFCreateInstance, 
	CMDXToolCFLockServer 
	};

/*//////////////////////// End of ClassFactory implementation //////////////////////*/