Click here to Skip to main content
13,291,510 members (62,290 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 29 Mar 2000

A smart critical section wrapper for COM objects

, 29 Mar 2000
Rate this:
Please Sign up or sign in to vote.
An extremely simple class that wraps the win32 CRITICAL_SECTION. Ideal for the COM STA or MTA.


More and more COM objects are written to be 'Both' threaded because it enables creating the object in either a COM STA or MTA. If the thread that creates the 'Both' threaded COM object is initialized to be in an STA by using CoInitialize(), CoInitializeEx(NULL, COINIT_APARTMENTTHREADED) or OleInitialize(), the object will be created in the same STA. If the thread is instead initialized to be in the MTA by using CoInitializeEx(NULL, COINIT_MULTITHREADED), the object will be created in the process's one and only MTA.

The developer of the object is thus forced to code for simultaneous access by multiple threads, typically by using a Critical Section. Note that any global and static variables need to be protected anyway. What I am presenting here optimizes the code that protects the per object instance state. I also want to point out that if the object in question aggregates the Free Threaded Marshaller (FTM), this technique should not be used. The class CComSmartAutoCriticalSection is an extremely simple class that wraps a WIN32 CRITICAL_SECTION. The definition of this class is as follows:

// CComSmartAutoCriticalSection 
class CComSmartAutoCriticalSection 
		if (SUCCEEDED(::CoInitialize(NULL))) 

			m_bInMTA = FALSE; 
			m_bInMTA = TRUE; 

		if (m_bInMTA) 

		if (m_bInMTA) 

	void Lock() 
		if (m_bInMTA) 

	void Unlock() 
		if (m_bInMTA) 

	BOOL              m_bInMTA; 
	CRITICAL_SECTION  m_csSafeAccess; 

The constructor detects whether the creating thread belongs to a COM STA or MTA and initializes the private member variable m_bInMTA. The CoInitialize() call will return S_FALSE if the calling thread belongs to an STA or RPC_E_CHANGED_MODE if the calling thread belongs to an MTA. If the object is being created in the MTA, the m_csSafeAccess critical section is initialized..

The destructor deletes the m_csSafeAccess critical section if the object is created in the MTA.

The Lock() and Unlock() methods invoke the WIN32 EnterCirticalSection() and LeaveCriticalSection() APIs respectively ONLY IF the object is created within the MTA.


Within the C++ class the implements your COM object, define a member variable of type CComSmartAutoCriticalSection.

class ATL_NO_VTABLE CSomeCOMClass : <inheritance list> 
	CComSmartAutoCriticalSection m_csSafeAccess; 

The implementation functions for the interface properties and methods should be wrapped within the Lock() and Unlock() functions.

STDMETHODIMP CSomeCOMClass::SomeMethod() 
	HRESULT hResult = S_OK; 


	... // Code to do what the method does. 


	return hResult; 


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jeremiah Talkar
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionhaving a 'CRITICAL_SECTION' as a member of 'struct' Pin
Member 391184315-Jul-08 1:41
memberMember 391184315-Jul-08 1:41 
QuestionDoes/will this work in COM+? Pin
Dave Hanlon18-May-00 9:15
sussDave Hanlon18-May-00 9:15 
AnswerRe: Does/will this work in COM+? Pin
Jeremiah S. Talkar3-Jun-00 5:33
sussJeremiah S. Talkar3-Jun-00 5:33 
GeneralRe: Does/will this work in COM+? Pin
Gevorg Ter-saakov7-Jun-00 11:33
sussGevorg Ter-saakov7-Jun-00 11:33 
AnswerRe: Does/will this work in COM+? Pin
Robert Little13-Sep-00 21:15
sussRobert Little13-Sep-00 21:15 
GeneralRe: Does/will this work in COM+? Pin
Jeremiah Talkar9-Apr-01 8:54
memberJeremiah Talkar9-Apr-01 8:54 
GeneralTimeouts Pin
Chris3-Apr-00 22:27
sussChris3-Apr-00 22:27 
GeneralRe: Timeouts Pin
Marcin Zielinski14-Nov-01 4:15
memberMarcin Zielinski14-Nov-01 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 30 Mar 2000
Article Copyright 2000 by Jeremiah Talkar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid