Click here to Skip to main content
Click here to Skip to main content

Understanding The COM Single-Threaded Apartment Part 1

By , 6 Jan 2005
 
Prize winner in Competition "MFC/C++ Dec 2004"
ccomthread_src.zip
LegacyCOMObject1
LegacyCOMObject1.def
LegacyCOMObject1.dsp
LegacyCOMObject1.dsw
LegacyCOMObject1.rgs
LegacyCOMObject1.tlb
LegacyCOMObject1ps.def
LegacyCOMObject1ps.mk
Shared
SimpleCOMObject1
SimpleCOMObject1.def
SimpleCOMObject1.dsp
SimpleCOMObject1.dsw
SimpleCOMObject1.rgs
SimpleCOMObject1.tlb
SimpleCOMObject1ps.def
SimpleCOMObject1ps.mk
SimpleCOMObject2
SimpleCOMObject2.def
SimpleCOMObject2.dsp
SimpleCOMObject2.dsw
SimpleCOMObject2.rgs
SimpleCOMObject2.tlb
SimpleCOMObject2ps.def
SimpleCOMObject2ps.mk
Test Programs
VBTest
FormMain.frm
MSSCCPRJ.SCC
VBTest.exe
VBTest.vbp
VBTest.vbw
VCTests
DemonstrateDefaultSTA
VCTest01
SimpleCOMObject2.tlh
SimpleCOMObject2.tli
VCTest01.dsp
VCTest01.dsw
VCTest02
SimpleCOMObject2.tlh
SimpleCOMObject2.tli
VCTest02.dsp
VCTest02.dsw
DemonstrateExeServerSTA
Client
VCTest01
VCTest01.dsp
VCTest01.dsw
Implementation
ExeServerImpl
ExeServerImpl.dsp
ExeServerImpl.dsw
Interface
ExeServerInterfaces
ExeObj01.rgs
ExeObj02.rgs
ExeObj03.rgs
ExeServerInterfaces.def
ExeServerInterfaces.dsp
ExeServerInterfaces.dsw
ExeServerInterfaces.tlb
ExeServerInterfacesps.def
ExeServerInterfacesps.mk
DemonstrateLegacySTA
VCTest01
LegacyCOMObject1.tlh
LegacyCOMObject1.tli
VCTest01.dsp
VCTest01.dsw
VCTest02
VCTest02.dsp
VCTest02.dsw
DemonstrateNoMessageLoop
VCTest01
SimpleCOMObject2.tlh
SimpleCOMObject2.tli
VCTest01.dsp
VCTest01.dsw
DemonstrateSTA
VCTest01
SimpleCOMObject2.tlh
SimpleCOMObject2.tli
VCTest01.dsp
VCTest01.dsw
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>

#import "..\..\..\..\SimpleCOMObject2\Debug\SimpleCOMObject2.dll"
using namespace SIMPLECOMOBJECT2Lib;





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





DWORD WINAPI ThreadFunc(LPVOID lpvParameter)
{
  ::CoInitializeEx(NULL, COINIT_MULTITHREADED);

  DisplayCurrentThreadId();

  if (1)
  {
    ISimpleCOMObject2Ptr spISimpleCOMObject2A;
	ISimpleCOMObject2Ptr spISimpleCOMObject2B;

    spISimpleCOMObject2A.CreateInstance(__uuidof(SimpleCOMObject2));
	spISimpleCOMObject2B.CreateInstance(__uuidof(SimpleCOMObject2));

	spISimpleCOMObject2A -> TestMethod1();
	spISimpleCOMObject2B -> TestMethod1();
  }

  ::CoUninitialize();

  return 0;
}





int main()
{
  HANDLE hThread = NULL;
  DWORD  dwThreadId = 0;

  ::CoInitializeEx(NULL, COINIT_MULTITHREADED);

  DisplayCurrentThreadId();

  if (1)
  {
    ISimpleCOMObject2Ptr spISimpleCOMObject2;

    spISimpleCOMObject2.CreateInstance(__uuidof(SimpleCOMObject2));

    spISimpleCOMObject2 -> TestMethod1();

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

    WaitForSingleObject(hThread, INFINITE);

	spISimpleCOMObject2 -> TestMethod1();
  }

  ::CoUninitialize();

  return 0;
}





By viewing downloads associated with this article you agree to the Terms of use 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)

About the Author

Lim Bio Liong
Web Developer
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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 6 Jan 2005
Article Copyright 2005 by Lim Bio Liong
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid