ATL Singleton and Singleton Class also from C++ 
Author Message
 ATL Singleton and Singleton Class also from C++

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>
{
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;

Quote:
};

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



Fri, 14 Mar 2003 03:00:00 GMT  
 ATL Singleton and Singleton Class also from C++
I hope i have checked it!
but it works!

i have to get the interface pointer with CoCreateInstance and than cast my
interface pointer to my class and  than i have all my methods i need!

IEnvironment pIEnvironment;
CEnvironment * pEnvironment;
CComPtr<IEnvironment> pIEnvironmentZw = NULL;

pIEnvironmentZw.CoCreateInstance (CLSID_Environment);
pIEnvironment = pIEnvironmentZw;
pEnvironment = (CEnvironment *)pIEnvironment;



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>

- Show quoted text -

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




Fri, 14 Mar 2003 03:00:00 GMT  
 ATL Singleton and Singleton Class also from C++

Why don't you carefully step-trough your and ATLs code in de{*filter*} to see
what exactly happens?
-vlad


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>

- Show quoted text -

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




Fri, 14 Mar 2003 03:00:00 GMT  
 ATL Singleton and Singleton Class also from C++
Don't use a public singleton. Use an internal singleton - a global instance
of
your class. Then construct a public object with no data of its own which
simply delegates to the internal singleton. In your other code you can
access
the internal singleton too - exactly what you need. Be careful with the
threading issues (if any) of course...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


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>

- Show quoted text -

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




Sat, 15 Mar 2003 03:00:00 GMT  
 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>

- Show quoted text -

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.


Sun, 16 Mar 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Singleton not a singleton?

2. ATL Singleton Class

3. ATL MFC EXE singleton server, single instance application

4. ATL Singleton Server

5. ATL MFC EXE singleton server, single instance application

6. Is a multiuser ATL singleton possible?

7. singleton exe server event, atl client.

8. ATL Server - Singletons on WinXP

9. ATL Singleton EXE Server

10. ATL Singleton Klasse erstellen

11. Singleton in ATL - HOWTO...

12. singleton class please help

 

 
Powered by phpBB® Forum Software