DECLARE_CLASSFACTORY_NOPING()

 

Here's yet another ATL classfactory variation you may find useful. If they want to disable COM's default Garbage collection/pinging (and all the associated goodies - ref counting & timely shutdown etc.) than they can use this classfactory.

MS KB Q171414 describes one way to do this. But it has several problems.  I like this one. The object using this classfactory is responsible for its own shutdown by calling CoDisconnectObject() at the place appropriate to his code logic.

Don Box gave the idea to write this one. Thanks Don...

 


class CComClassFactoryNoPing: public CComClassFactory        

{

public:

STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj)

{

    HRESULT hr = CComClassFactory::CreateInstance(pUnkOuter, riid, ppvObj);

    if(SUCCEEDED(hr)) //NoPing CF

    {

        CComPtr<IStream> spStream;

        hr = ::CreateStreamOnHGlobal(0,FALSE,&spStream);

        if(SUCCEEDED(hr))

        {

            hr = ::CoMarshalInterface(spStream,

                        riid,(IUnknown*)*ppvObj,

                        MSHCTX_DIFFERENTMACHINE,

                        NULL,

                        MSHLFLAGS_NOPING|MSHLFLAGS_NORMAL);

        }

    }

    return hr;

}

};

#define DECLARE_CLASSFACTORY_NOPING() DECLARE_CLASSFACTORY_EX(CComClassFactoryNoPing)


Usage :

 

class ATL_NO_VTABLE CFoo:   public CComObjectRootEx<CComSingleThreadModel>,

                            public CComCoClass<CFoo, &CLSID_Foo>,

                            public IFoo ...

{

public:

    CFoo(){}

    DECLARE_REGISTRY_RESOURCEID(IDR_ADTS)

    DECLARE_CLASSFACTORY_NOPING() // Disable Pinging

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    ...

    ...

    ...

};


This page and the source contained in(or referred to) by this page are Copyright (c) 1998, Dharma Shukla.
All rights reserved. No warrenties extended. Use at your own risk.
Do send Comments/Bugs to me :-)