class CO1 : public IF1 { ... } struct CFO1 : public IClassFactory { ULONG m_refcnt; HRESULT QueryInterface (const IID& riid, VOID **ppv) { if (IID_IUnknown == riid || IID_IClassFactory == riid) { *ppv = this; return S_OK; } return E_NOINTERFACE; } ULONG AddRef() { return ++m_refcnt; } ULONG Release() { if (--m_refcnt == 0) { delete this; return 0; } return m_refcnt; }
IClassFactory
HRESULT CreateInstance (IUnknown *pOuter, const IID &riid,void **ppv) { CO1*pc; if (pOuter != NULL) return E_NOAGGREGATION; pc = new CO1(); HRESULT hr = pc->QueryInterface(riid, ppv); return S_OK; } HRESULT LockServer(BOOL fLock) { if (fLock) ++g_lock; else --g_lock; } CFO1() { } };