Basic COM   «Prev 

How IClassFactory creates objects

Implementing IClassFactory

ULONG g_lock = 0;

//Assume COM object MyComObject is implemented
//by class CMyComObject using the multiple inheritance technique. 

class CMyComObject : public IMyComInterface,
  public IYourComInterface { ... }

struct CFMyComObject : public IClassFactory {
  ULONG m_refcnt;

  HRESULT QueryInterface
    (const IID& riid, VOID **ppv) {
    //class  factories normally only respond to
    //IID_IUnknown and IClassFactory
    if (IID_IUnknown == riid ||
   IID_IClassFactory == riid) {
    *ppv = this;
    ((IUnknown *) *ppv)->AddRef();
      return S_OK;
    return E_NOINTERFACE;

   ULONG AddRef() { return ++m_refcnt; }

   ULONG Release() {
    //When the reference count goes to zero we
    //normally delete the class factory
     if (--m_refcnt == 0) {
       delete this;
       return 0;
      return m_refcnt;

   HRESULT CreateInstance(IUnknown *pOuter,
     const IID& riid, void **ppv) {
     CMyComObject *pc;

    //if pOuter is not NULL, return an ERROR
   if (pOuter != NULL)
     return E_NOAGGREGATION;

    //Create a new instance of the C++ class that
    //implements our COM object and call
  pc = new CMyComObject();

    //QueryInterface in it to see
    //if the object supports the interface
    //requested by the client.
      HRESULT hr = pc->QueryInterface(riid, ppv);

    //If the client has asked for an interface not   
    //supported by the newly created COM object
    //delete the COM object and
    //return the error code
    //from the QueryInterface call. This
    //will usually be E_NOINTERFACE.
     if (FAILED(hr)) {
       delete pc;
       return hr;

     return S_OK;

   HRESULT LockServer(BOOL fLock) {
     if (fLock) ++g_lock;
     else --g_lock;

   CFMyComObject() : m_refcnt(0) { }