Click here to Skip to main content
15,885,546 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.3K   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"
#include "registry.h"
#include "ExeObj01.h"
#include "ExeObj02.h"
#include "ExeObj03.h"





long g_lObjsInUse = 0;
long g_lServerLocks = 0;
DWORD g_dwMainThreadID = 0;

typedef struct StructRegisterViaThread_Tag
{
  DWORD		dwCookie;
  DWORD		dwThreadId;
  HANDLE	hEventRegistered;
} StructRegisterViaThread, *PStructRegisterViaThread;





template <class T> 
void RegisterClassObject(REFCLSID rclsid, LPDWORD lpdwCookieReceiver, T* pT_in = NULL)
{
  T*		pT = NULL;
  HRESULT	hr = S_OK;

  if (pT_in)
  {
    pT = pT_in;
	pT -> AddRef();
  }
  else
  {
    pT = new T();
  }

  if (pT)
  {
    IUnknown* pIUnknown = NULL;

    pT -> QueryInterface(IID_IUnknown, (void**)&pIUnknown);

	if (pIUnknown)
	{
      hr = ::CoRegisterClassObject
      (
        rclsid,
        pIUnknown,
        CLSCTX_LOCAL_SERVER,
        REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED,
        lpdwCookieReceiver
      );

      pIUnknown -> Release();
      pIUnknown = NULL;
	}

    pT -> Release();
    pT = NULL;
  }
}





DWORD RegisterClassObject_ViaThread(LPTHREAD_START_ROUTINE ThreadFunc, LPDWORD lpdwCookieReceiver)
{
  HANDLE					hThread = NULL;
  StructRegisterViaThread	struct_register_via_thread;

  memset (&struct_register_via_thread, 0, sizeof(struct_register_via_thread));

  struct_register_via_thread.hEventRegistered = CreateEvent(NULL, TRUE, FALSE, NULL);

  hThread = CreateThread
  (
    (LPSECURITY_ATTRIBUTES)NULL, // SD
    (SIZE_T)0,                       // initial stack size
    (LPTHREAD_START_ROUTINE)ThreadFunc,    // thread function
    (LPVOID)&struct_register_via_thread,                       // thread argument
    (DWORD)0,                    // creation option
    (LPDWORD)&(struct_register_via_thread.dwThreadId)                        // thread identifier
  );

  CloseHandle(hThread);

  WaitForSingleObject(struct_register_via_thread.hEventRegistered, INFINITE);

  CloseHandle(struct_register_via_thread.hEventRegistered);

  *lpdwCookieReceiver = struct_register_via_thread.dwCookie;

  return struct_register_via_thread.dwThreadId;
}





// 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);
}





void StopThread(DWORD dwThreadId)
{
  ::PostThreadMessage(dwThreadId, WM_QUIT, 0, 0);
}





DWORD WINAPI ThreadFunc_RegisterExeObj02Factory(LPVOID lpvParameter)
{
  MSG						msg;
  PStructRegisterViaThread	pStructRegisterViaThread = (PStructRegisterViaThread)lpvParameter;

  //::CoInitializeEx(NULL, COINIT_MULTITHREADED);
  ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

  DisplayCurrentThreadId();

  pStructRegisterViaThread -> dwThreadId = GetCurrentThreadId();

  RegisterClassObject<CExeObj02_Factory>(CLSID_ExeObj02, &(pStructRegisterViaThread -> dwCookie));

  SetEvent(pStructRegisterViaThread -> hEventRegistered);

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
	TranslateMessage(&msg);
	DispatchMessage(&msg);
  }

  ::CoUninitialize();

  return 0;
}





DWORD WINAPI ThreadFunc_RegisterExeObj03Factory(LPVOID lpvParameter)
{
  MSG						msg;
  PStructRegisterViaThread	pStructRegisterViaThread = (PStructRegisterViaThread)lpvParameter;

  //::CoInitializeEx(NULL, COINIT_MULTITHREADED);
  ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

  DisplayCurrentThreadId();

  pStructRegisterViaThread -> dwThreadId = GetCurrentThreadId();

  RegisterClassObject<CExeObj03_Factory>(CLSID_ExeObj03, &(pStructRegisterViaThread -> dwCookie));

  SetEvent(pStructRegisterViaThread -> hEventRegistered);

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0)) 
  {
	TranslateMessage(&msg);
	DispatchMessage(&msg);
  }

  ::CoUninitialize();

  return 0;
}





int APIENTRY WinMain
(
  HINSTANCE hInstance,
  HINSTANCE hPrevInstance,
  LPSTR     lpCmdLine,
  int       nCmdShow
)
{
  MSG		msg;
  HRESULT	hr = S_OK;
  bool		bRun = true;

  DisplayCurrentThreadId();

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

  if (stricmp((const char*)(&(lpCmdLine[0])), "RegServer") == 0)
  {
    hr = RegisterServer
    (
      (HMODULE)hInstance, 
      (const CLSID&)CLSID_ExeObj01, 
      (const char*)"ExeServerImpl.ExeObj01",
      (const char*)"ExeServerImpl.ExeObj01",
      (const char*)"ExeServerImpl.ExeObj01"
	);

	hr = RegisterServer
	(
	  (HMODULE)hInstance, 
      (const CLSID&)CLSID_ExeObj02, 
      (const char*)"ExeServerImpl.ExeObj02",
      (const char*)"ExeServerImpl.ExeObj02",
      (const char*)"ExeServerImpl.ExeObj02"
	);

	hr = RegisterServer
	(
	  (HMODULE)hInstance, 
      (const CLSID&)CLSID_ExeObj03, 
      (const char*)"ExeServerImpl.ExeObj03",
      (const char*)"ExeServerImpl.ExeObj03",
      (const char*)"ExeServerImpl.ExeObj03"
	);

	bRun = false;
  }

  if (stricmp((const char*)(&(lpCmdLine[0])), "UnregServer") == 0)
  {
    hr = UnregisterServer
    (
      (const CLSID&)CLSID_ExeObj01,
      (const char*)"ExeServerImpl.ExeObj01",
      (const char*)"ExeServerImpl.ExeObj01"
    );

    hr = UnregisterServer
	(
	  (const CLSID&)CLSID_ExeObj02,
      (const char*)"ExeServerImpl.ExeObj02",
      (const char*)"ExeServerImpl.ExeObj02"
    );

    hr = UnregisterServer
	(
	  (const CLSID&)CLSID_ExeObj03,
      (const char*)"ExeServerImpl.ExeObj03",
      (const char*)"ExeServerImpl.ExeObj03"
    );

	bRun = false;
  }

  if (bRun)
  {
    DWORD dwCookie_ExeObj01 = 0;
	DWORD dwCookie_ExeObj02 = 0;
	DWORD dwCookie_ExeObj03 = 0;
	DWORD dwThreadId_RegisterExeObj02Factory = 0;
	DWORD dwThreadId_RegisterExeObj03Factory = 0;

    g_dwMainThreadID = GetCurrentThreadId();

	RegisterClassObject<CExeObj01_Factory>(CLSID_ExeObj01, &dwCookie_ExeObj01);

	dwThreadId_RegisterExeObj02Factory = RegisterClassObject_ViaThread(ThreadFunc_RegisterExeObj02Factory, &dwCookie_ExeObj02);

	dwThreadId_RegisterExeObj03Factory = RegisterClassObject_ViaThread(ThreadFunc_RegisterExeObj03Factory, &dwCookie_ExeObj03);

	::CoResumeClassObjects();

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
	  TranslateMessage(&msg);
	  DispatchMessage(&msg);
    }

	StopThread(dwThreadId_RegisterExeObj02Factory);
	StopThread(dwThreadId_RegisterExeObj03Factory);

	::CoRevokeClassObject(dwCookie_ExeObj01);
	::CoRevokeClassObject(dwCookie_ExeObj02);
	::CoRevokeClassObject(dwCookie_ExeObj03);
  }

  ::CoUninitialize();

  return msg.wParam;
}

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