
ATL Singleton and Singleton Class also from C++
Why don't you use the standard singleton pattern ???
class CEnvironement : ...
{
static CComObject<CEnvironemnt>& Self()
{ if ( !m_instance )
{
m_instance = new CComObject<CEnvironement >;
m_instance->AddRef(); // keep it alive
}
return *m_instance;
}
static CComObject<CEnvironemnt>* m_instance;
Quote:
};
And then create a factory that use the singleton instance (derive
from CComClassFactory, easy to do). That way, a COM client
uses the singleton, and the C++ client can access the singleton
whithout the >>ugly<< cast you are making !!!
Btw, like every singleton object, there is a problem with the
lifetime of the object and the dll. Maybe a singleton object
should not increase the lock count of the server ???
FReD
Quote:
> hallo everybody!
> I posted a view days ago this message, and the answer was make it with
> CoCreateInstance!
> It works, but this way i cannot make! I need a Singleton Class in my
ATL
> Component which is singleton from outside of the component and also
insite
> the componenet! (i instanciate the same class in my Component as
well!)
> i have used the macro DECLARE_CLASSFACTORY_SINGLETON(CEnvironment)
> it does not work!
> my class:
> class ATL_NO_VTABLE CEnvironment :
> public CComObjectRootEx<CComSingleThreadModel>,
> public CComCoClass<CEnvironment, &CLSID_Environment>,
> public IDispatchImpl<IEnvironment, &IID_IEnvironment,
&LIBID_MWMELDUNGLib>
Quote:
> {
> DECLARE_CLASSFACTORY_SINGLETON(CEnvironment)
> public:
> CEnvironment()
> {
> }
> DECLARE_REGISTRY_RESOURCEID(IDR_ENVIRONMENT)
> DECLARE_PROTECT_FINAL_CONSTRUCT()
> BEGIN_COM_MAP(CEnvironment)
> COM_INTERFACE_ENTRY(IEnvironment)
> COM_INTERFACE_ENTRY(IDispatch)
> END_COM_MAP()
> BEGIN_CONNECTION_POINT_MAP(CEnvironment)
> END_CONNECTION_POINT_MAP()
> // ISupportsErrorInfo
> STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
> // IEnvironment
> public:
> STDMETHOD(get_DatabasePath)(/*[out, retval]*/ BSTR *pVal);
> STDMETHOD(put_DatabasePath)(/*[in]*/ BSTR newVal);
> private:
> _bstr_t vbDatabasePath;
> _bstr_t vbApplicationPath;
> char cTest;
> };
> if i wanna use this class and wanna get the singleton class and i use
it
> like this:
> IEnvironment* pIEnvironment=NULL;
> CEnvironment* pEnvironment = NULL;
> CEnvironment::CreateInstance(&pIEnvironment);
> pEnvironment = (CEnvironment *)pIEnvironment;
> i get not a singleton instance but always another instance!
> If i make it with CoCreateInstance i get always an singleton instance
BUT in
> my Class there is an Interface Pointer to my Class and the Class
itself has
> other attributes and Methods (not exporter with an Interface) And i
need to
> work with these methods which are not in the Interface!
> what is wrong in my code
> can anyone help me??
> thank you
> :) Heinz
> Heinz Tuppinger
Sent via Deja.com http://www.deja.com/
Before you buy.