COM IClassFactory接口封装

CAbstractClassFactory.h
#include <objbase.h>
#include <unknwn.h>
#define DECLARE_IUNKNOWN_INTERFACE														\
	public:																				\
	virtual HRESULT _stdcall QueryInterface(const IID& riid,void** ppvObject);			\
	virtual ULONG _stdcall AddRef ();													\
	virtual ULONG _stdcall Release();
class CAbstractClassFactory:public IClassFactory
{
public:
	CAbstractClassFactory();
	DECLARE_IUNKNOWN_INTERFACE
	virtual HRESULT _stdcall CreateInstance(IUnknown *pUnkOuter,const IID& riid,void** ppvObject)=0;
    virtual HRESULT _stdcall LockServer(BOOL fLock);
	long m_caref,g_caref;
};
const CLSID CLSID_CAbstractClassFactory = {0x59F6C62A,0x50BE,0x4687,{0x95,0x80,0x59,0xC4,0x8F,0x94,0xE5,0xD8}};

 

CAbstractClassFactory.cpp
#include "CAbstractClassFactory.h"
CAbstractClassFactory::CAbstractClassFactory(){m_caref=1;g_caref=1;}
HRESULT _stdcall CAbstractClassFactory::QueryInterface(const IID& riid,void** ppvObject)
{
	if(riid==IID_IUnknown||riid==IID_IClassFactory)
	{
		*ppvObject=static_cast<IClassFactory*>(this);
	}
	else
	{
		*ppvObject=NULL;
		return E_NOINTERFACE;
	}
	return S_OK;
	
}
ULONG _stdcall CAbstractClassFactory::AddRef()
{
	return InterlockedIncrement(&m_caref);
}
ULONG _stdcall CAbstractClassFactory::Release()
{
	if(InterlockedDecrement(&m_caref)==0)
	{
		delete this;
		return 0;
	}
	return m_caref;
}
HRESULT _stdcall CAbstractClassFactory::LockServer(BOOL fLock)
{
	if(fLock)
	{
		InterlockedIncrement(&g_caref);
	}
	else
	{
		InterlockedDecrement(&g_caref);
	}
	return S_OK;
}

 

你可能感兴趣的:(factory)