Click here to Skip to main content
15,886,258 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 842.7K   4.9K   442  
Learn the fundamental principles of the COM Single-Threaded Apartment Model by code examples.
#include "ExeObj02.h"
#include <stdio.h>






CExeObj02::CExeObj02()
{
  ::CoCreateInstance
  (
    CLSID_ExeObj01,     //Class identifier (CLSID) of the object
    NULL, //Pointer to controlling IUnknown
    CLSCTX_LOCAL_SERVER,  //Context for running executable code
    IID_IExeObj01,         //Reference to the identifier of the interface
    (LPVOID*)&m_pIExeObj01         //Address of output variable that receives 
                           // the interface pointer requested in riid
  );
}





CExeObj02::~CExeObj02()
{
  if (m_pIExeObj01)
  {
    m_pIExeObj01 -> Release();
	m_pIExeObj01 = NULL;
  }
}





STDMETHODIMP CExeObj02::QueryInterface(REFIID riid, void** ppvObject)
{
  if (riid == IID_IUnknown || riid == IID_IExeObj02)
  {
	*ppvObject = (IExeObj02*)this;
	((CReferenceCountedObject*)this) -> AddRef();
	return S_OK;
  }
  
  return E_NOINTERFACE;
}





// IUnknown method.
STDMETHODIMP_(ULONG) CExeObj02::AddRef()
{
  return CReferenceCountedObject::AddRef();
}





// IUnknown method.
STDMETHODIMP_(ULONG) CExeObj02::Release()
{
  return CReferenceCountedObject::Release();
}





STDMETHODIMP CExeObj02::TestMethod1()
{
  TCHAR szMessage[256];

  sprintf (szMessage, "0x%X", GetCurrentThreadId());

  ::MessageBox(NULL, szMessage, "CExeObj02::TestMethod1()", MB_OK);

  return m_pIExeObj01 -> TestMethod1();
}





CExeObj02_Factory::CExeObj02_Factory()
{
}





CExeObj02_Factory::~CExeObj02_Factory()
{
}





// IUnknown method. Overridde QueryInterface() method.
STDMETHODIMP CExeObj02_Factory::QueryInterface(REFIID riid, void** ppvObject)
{
  if (riid == IID_IUnknown)
  {
    CReferenceCountedObject* pCReferenceCountedObject = (CReferenceCountedObject*)this;

	*ppvObject = (IUnknown*)pCReferenceCountedObject;
	((CReferenceCountedObject*)this) -> AddRef();
	return S_OK;
  }

  if (riid == IID_IClassFactory)
  {
    *ppvObject = (IClassFactory*)this;
	((CReferenceCountedObject*)this) -> AddRef();
	return S_OK;
  }

  *ppvObject = NULL;
  
  return E_NOINTERFACE;
}





// IUnknown method.
STDMETHODIMP_(ULONG) CExeObj02_Factory::AddRef()
{
  return CClassFactory::AddRef();
}





// IUnknown method.
STDMETHODIMP_(ULONG) CExeObj02_Factory::Release()
{
  return CClassFactory::Release();
}





// IClassFactory method.
STDMETHODIMP CExeObj02_Factory::CreateInstance
(
  IUnknown __RPC_FAR *pUnkOuter,
  REFIID riid,
  void __RPC_FAR *__RPC_FAR *ppvObject
)
{
  CExeObj02*	pCExeObj02 = NULL;

  // Initialise the receiver.
  *ppvObject = NULL;

  if (pUnkOuter != NULL)
  {
	return CLASS_E_NOAGGREGATION;
  }

  // Create an instance of the component.
  pCExeObj02 = new CExeObj02;

  if (pCExeObj02 == NULL)
  {
	return E_OUTOFMEMORY;
  }

  pCExeObj02 -> QueryInterface (riid, ppvObject);

  pCExeObj02 -> Release();

  return S_OK;
}





// IClassFactory method.
STDMETHODIMP CExeObj02_Factory::LockServer
(
  BOOL fLock
)
{
  return CClassFactory::LockServer(fLock);
}








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