Click here to Skip to main content
15,892,059 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 847.2K   4.9K   442  
Learn the fundamental principles of the COM Single-Threaded Apartment Model by code examples.
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
#include "ExeServerInterfaces.h"
#include "ExeServerInterfaces_i.c"





// Simple function that displays the current thread ID.
void DisplayCurrentThreadId()
{
  TCHAR szMessage[256];

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

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





int main()
{
  IExeObj01* pIExeObj01A = NULL;
  IExeObj01* pIExeObj01B = NULL;
  IExeObj02* pIExeObj02A = NULL;
  IExeObj02* pIExeObj02B = NULL;
  IExeObj03* pIExeObj03A = NULL;
  IExeObj03* pIExeObj03B = NULL;

  //HRESULT hr = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);

  ::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*)&pIExeObj01A         //Address of output variable that receives 
                           // the interface pointer requested in riid
  );

  ::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*)&pIExeObj01B         //Address of output variable that receives 
                           // the interface pointer requested in riid
  );

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

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

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

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

  if (pIExeObj01A)
  {
    pIExeObj01A -> TestMethod1();
  }

  if (pIExeObj01B)
  {
    pIExeObj01B -> TestMethod1();
  }

  if (pIExeObj02A)
  {
    pIExeObj02A -> TestMethod1();
  }

  if (pIExeObj02B)
  {
    pIExeObj02B -> TestMethod1();
  }

  if (pIExeObj03A)
  {
    pIExeObj03A -> TestMethod1();
  }

  if (pIExeObj03B)
  {
    pIExeObj03B -> TestMethod1();
  }

  if (pIExeObj01A)
  {
    pIExeObj01A -> Release();
	pIExeObj01A = NULL;
  }

  if (pIExeObj01B)
  {
    pIExeObj01B -> Release();
	pIExeObj01B = NULL;
  }

  if (pIExeObj02A)
  {
    pIExeObj02A -> Release();
	pIExeObj02A = NULL;
  }

  if (pIExeObj02B)
  {
    pIExeObj02B -> Release();
	pIExeObj02B = NULL;
  }

  if (pIExeObj03A)
  {
    pIExeObj03A -> Release();
	pIExeObj03A = NULL;
  }

  if (pIExeObj03B)
  {
    pIExeObj03B -> Release();
	pIExeObj03B = NULL;
  }

  ::CoUninitialize();

  return 0;
}

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