Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C++

A very simple COM server without ATL or MFC

Rate me:
Please Sign up or sign in to vote.
4.30/5 (18 votes)
3 Aug 2000CPOL 262.2K   3.4K   62  
A step by step guide to write a COM server using C++ without MFC or ATL.

// ClassFactory.h
// a very simple COM server without MFC or ATL by :  Shoaib Ali
// you can send your comments at alleey@usa.net
//
#if !defined(AFX_CLASSFACTORY_H__369F4E09_64A7_11D4_B0B4_0050BABFC904__INCLUDED_)
#define AFX_CLASSFACTORY_H__369F4E09_64A7_11D4_B0B4_0050BABFC904__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdio.h"
#include "ComBase.h"

// Creator class for Singleton class factories this class returns the pointer to the same 
// object for multiple CreateObject requests ..
//
template<class comObj>
class CSingleCreator
{
protected:
	CSingleCreator():m_pObj(0) {};

	comObj *CreateObject()
	{
		if(!m_pObj)
		{
			m_pObj = new comObj;
		}
		return m_pObj;
	}
	comObj * m_pObj;
};

// Creator class for normal class factories this class returns the pointer to a new 
// object for each CreateObject request ..
//
template<class comObj>
class CMultiCreator
{
protected:
	CMultiCreator():m_pObj(0) {};
	comObj *CreateObject()
	{
		return new comObj;
	}
	comObj * m_pObj;
};


// ClassFactory implementation using the classes all around us now the default creator 
// class for classfactory is MultiCreator this class implements IClasFactory interface ....
// 
template <class comObj, class creatorClass  = CMultiCreator < comObj > >
class CClassFactory :  public CComBase<>,public InterfaceImpl<IClassFactory>,public creatorClass 
{
public:
	CClassFactory() {};
	virtual ~CClassFactory() {};

	STDMETHOD(QueryInterface)(REFIID riid,LPVOID *ppv)
	{
		*ppv = NULL;
		if(IsEqualIID(riid,IID_IUnknown) || IsEqualIID(riid,IID_IClassFactory))
		{
			*ppv = (IClassFactory *) this;
			_AddRef();
			return S_OK;
		}
		return E_NOINTERFACE;
	}

	STDMETHODIMP CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, LPVOID *ppvObj)
	{
		*ppvObj = NULL;
		if (pUnkOuter)
    		return CLASS_E_NOAGGREGATION;
		m_pObj = CreateObject();  // m_pObj is defined in creatorClass 
		if (!m_pObj)
    		return E_OUTOFMEMORY;
		HRESULT hr = m_pObj->QueryInterface(riid, ppvObj);
		if(hr != S_OK)
		{
			delete m_pObj;
		}
		return hr;
	}

	STDMETHODIMP LockServer(BOOL) {	return S_OK; }  // not implemented
};



#endif // !defined(AFX_CLASSFACTORY_H__369F4E09_64A7_11D4_B0B4_0050BABFC904__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions