Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / ATL

Understanding The COM Single-Threaded Apartment Part 1

Rate me:
Please Sign up or sign in to vote.
4.95/5 (206 votes)
6 Jan 2005CPOL49 min read 839.3K   4.9K   441  
Learn the fundamental principles of the COM Single-Threaded Apartment Model by code examples.
#ifndef REFERENCE_COUNTED_OBJ_H
  #define REFERENCE_COUNTED_OBJ_H

#include <windows.h>

// For reference counting on all created objects.
extern long g_lObjsInUse;
extern long g_lServerLocks;
extern DWORD g_dwMainThreadID;





// This is the root class of all classes the objects of which are referenced counted.
class CReferenceCountedObject : public IUnknown
{
  public :
    CReferenceCountedObject();
    virtual ~CReferenceCountedObject();

	// IUnknown methods
    STDMETHODIMP QueryInterface(REFIID riid, void** ppvObject);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

  protected :
	LONG		m_cRef;
};





class CClassFactory : public IClassFactory
{
  public :
    CClassFactory();
	virtual ~CClassFactory();

	// IUnknown methods
    STDMETHODIMP QueryInterface(REFIID riid, void** ppvObject);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

	// IClassFactory methods.
    STDMETHODIMP CreateInstance
	( 
      IUnknown __RPC_FAR *pUnkOuter,
      REFIID riid,
      void __RPC_FAR *__RPC_FAR *ppvObject
	);
        
    STDMETHODIMP LockServer
	(
      BOOL fLock
	);

  protected :
	LONG		m_cRef;

  public :
	// Shut down the application.
	static void AttemptToTerminateServer();
};





#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer NEC
Singapore Singapore
Lim Bio Liong is a Specialist at a leading Software House in Singapore.

Bio has been in software development for over 10 years. He specialises in C/C++ programming and Windows software development.

Bio has also done device-driver development and enjoys low-level programming. Bio has recently picked up C# programming and has been researching in this area.

Comments and Discussions