Click here to Skip to main content
15,126,209 members
Articles / Desktop Programming / ATL
Posted 21 Apr 2002


99 bookmarked

What's new in ATL7

Rate me:
Please Sign up or sign in to vote.
4.77/5 (19 votes)
1 May 2002CPOL4 min read
Overview of new classes in ATL7

This article doesn't cover new ATL7 Server Classes and Attributes. This is not a complete list - only what I have found so far.

Memory Management



String conversion macros had a few limitations. It was allocating memory from stack, with potential to overflow the stack with large strings. The string was freed when the function exited and not at any introduced scope inside the function. It was using outdated OLE2ANSI define. And if you look at the remarks for _alloca (which was used for the conversions) on MSDN, it says that it has limitations when used inside SEH or C++ EH. 

For example look at the ATL3 String Conversion Macros:

      for(int i=0; i< 100000; ++i)
          LPTSTR str = OLE2T(L"Some string string string string string........");  
			// stack overflow, memory on the stack grows...

      throw int(1); // if above doesn't fail, then make sure catch(int) is executed 
      LPTSTR str = OLE2T(L"Some string string string string string........"); 
		// _alloca can't be used in SEH or C++ EH
                    // Generates Unhandled Exception
                    // Will be marked as error in VS.NET during compile


String Conversion macros/classes improve in those areas mentioned above. It uses stack memory for small strings and heap for large strings so there is no stack overflow when stack space is not enough. The string is freed when it's scope ends and not when function exits. Can be used in exception handlers. Can be used in loops (since it can be controlled by scope).

The macro names are easy to read. The form is as before: SourceType2[C]DestinationType

A-ANSI/W-Unicode/T-generic To [Constant] A-ANSI/W-Unicode/T-generic.

CA2T/CA2CT - from ANSI to generic string based on _UNICODE define
CT2A/CT2CA - from T generic string to ANSI string based on _UNICODE define
CT2W/CT2CW - from T generic string to UNICODE string
CW2T/CW2CT - from UNICODE to generic string based on _UNICODE define
CT2WEX/CT2CWEX - from T generic to UNICODE string and buffer can be specified
CW2TEX/CW2CTEX - from UNICODE to T generic string and buffer can be specified

- from ANSI to UNICODE
- from UNICODE to ANSI
CW2AEX/CA2WEX - the actual class that some of the above typedef/macros use

So above code would look like this and without crash:

     for(int i=0; i< 100000; ++i)
         CW2A str(L"Some string string string string string........");
     throw int(1);
      CW2A str(L"Some string string string string string........");

Be aware of one caveat. All of the above macros can't be used in same code construct as in ATL3:

LPTSTR str = CW2A(L"some string"); 

The above code is wrong. After that line the temporary object created by CW2AEX is destroyed.
Just be aware that the macros represent C++ classes and are controlled by scope rules.
To see the problem try this:

// use size 2 for initial string length to make sure stack size 
// is not enough and heap is used for memory
LPCTSTR szr = CW2AEX<2>(L"some string"); 

szr; // at this line szr should point to garbage


Another Addition is the CStringT class for manipulating strings. Even though this class exposes the same methods as previous MFC CString. It's not the old MFC CString. This class is templated. It has few typedefs to work with char, wchar_t, and TCHAR. CString is a typedef:

CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;

which is redefined based on MFC being used or not to:

// #ifdef _AFX
typedef CAtlString CString;

The CStringT itself is shared between MFC/ATL. By default it uses CRT, but that can be changed by defining either _ATL_MIN_CRT or _ATL_CSTRING_NO_CRT. Which in turn makes the appropriate typedef visible to the compiler, for example either:

// use CRT and have more functionality
CStringT< TCHAR, StrTraitATL< TCHAR, ChTraitsCRT< TCHAR > >


// don't use CRT, but use win32API string functions, (wvsprintf, lstrlen, CharNext, etc)
CStringT< TCHAR, StrTraitATL< TCHAR >, ChTraitsOS<TCHAR> > 

Another class CFixedStringT provides fixed pre-allocated contiguous buffer for optimized memory management.

The memory management for either one can be customized through implementation of IAtlStringMgr and that implementation already exists CAtlStringMgr. Its ctor takes a pointer to IAtlMemMgr, which can be any of predefined implementations or your own: 

CCRTHeap - use CRT heap
CWin32Heap - use win32 heap
CComHeap - use COM Task memory allocator

etc. So for example:

CComHeap comHeap;
CAtlStringMgr mg(&comHeap);
CString str(&mg);
str += "some string";

and we get ::CoTaskMemAlloc being used for memory allocation.

"String and Text Classes"(MSDN)

"ATL/MFC String Conversion Macros"(MSDN)


ATL7 introduces new collection classes. The classes have similar interface as previous MFC collection classes. These are templated classes
and we can pass our own CElementTraits for handling specialized cases, such as CopyElements, RelocateElements, CompareElements, and Hash.


// use CStringElementTraits to have a valid comparison for Find 
CAtlArray<CString, CStringElementTraits<CString> > array; 

// other traits are CStringRefElementTraits, CStringElementTraitsI
CAtlList<CString, CStringElementTraits<CString> > list;   

POSITION pos = list.Find("hello");
if(pos != NULL)
    CString str = list.GetAt(pos); 


CAtlMap<CString, int, CStringElementTraits<CString> > map;
map["key1"] = 2;
map["key2"] = 2;
int value;
if(map.Lookup("key1", value));

CRBTree/CRBMap /CRBMultiMap - Red-Black tree based implementation

CRBMap<int, int> rbMap;
rbMap.SetAt(10, 20);
rbMap.SetAt(30, 40);
CRBMap<int, int>::CPair* p = rbMap.Lookup(30);
//p->m_key; p->m_value;

Various other more specialized classes:

CAutoPtrArray/CAutoPtrList /CComUnkArray /CHeapPtrList /CInterfaceArray /CInterfaceList

"Collection Classes" (MSDN)



Quick example:

class WorkerClass : public IWorkerThreadClient
    HANDLE m_hEvent;
    CWorkerThread<Win32ThreadTraits> m_thread;
        m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        m_thread.AddHandle(m_hEvent, this, NULL);
    void ResumeThread()
    void FreezeThread()
 // IWorkerThreadClient
    HRESULT Execute(DWORD_PTR dwParam, HANDLE hObject)
        return S_OK;
    HRESULT CloseHandle(HANDLE hHandle)
        return S_OK;

    WorkerClass work;

And here is imagined usage of CThreadPool:

struct IDoSomeWork
    virtual void Work()=0;

class Work1 : public IDoSomeWork
    void Work(){}
class Work2 : public IDoSomeWork
    void Work(){}
class ThreadPoolWorker
// support for CThreadPool Worker Archetype
    typedef IDoSomeWork* RequestType;
    BOOL Initialize(void* pvWorkerParam)
        return TRUE;
    void Execute(RequestType request, void* pvWorkerParam,OVERLAPPED* pOverlapped)
    void Terminate(void* pvWorkerParam)

    CThreadPool<ThreadPoolWorker> pool;
IDoSomeWork* pWork1=new Work1();
IDoSomeWork* pWork2=new Work2();
    delete pWork1; delete pWork2;

Memory Management

CHeapPtr<CComAllocator | CCRTAllocator>:

CHeapPtr<int, CComAllocator> ptr;

// this class is not for arrays of objects, because it doesn't call delete[],
// but only delete.
CAutoPtr<CString> ptr(new CString("string"));  

CAutoVectorPtr<CString> pVec(new CString[100]); // this calls delete[]


Manages the pointer in GIT table. Creates the GIT and calls the IGlobalInterfaceTable methods for you.

Other more specialized classes include:

  • CAutoPtrArray
  • CAutoPtrList
  • CComHeap
  • CComHeapPtr
  • CComPtrBase
  • CCRTAllocator
  • CCRTHeap
  • CGlobalHeap
  • CHandle
  • CHeapPtrBase
  • CHeapPtrList
  • CLocalHeap
  • CWin32Heap

Memory Management Classes(MSDN)


CAtlFile is a thin wrapper around the win32API for files. CreateFile, ReadFile, WriteFile, etc
CAtlTemporaryFile can be used for temporary files. They are automatically named, opened, closed, and deleted.
CAtlFileMappingBase/CAtlFileMapping can be used for mapped files:

CAtlTemporaryFile file;
CAtlFileMapping<char> filemap;
filemap.MapFile(file, 1024, 0, PAGE_READWRITE, FILE_MAP_READ|FILE_MAP_WRITE);
char* pMem = filemap;
strcpy(pMem, "hello");

"File Handling Classes"(MSDN)


ATL7 includes wrappers for win32API security.

Quick Example:

CSid sidDenied;
CDacl dacl;
// try either deny or allow and see how OpenEvent later on behaves
dacl.AddDeniedAce(sidDenied, GENERIC_ALL);
// dacl.AddAllowedAce(sidDenied, GENERIC_ALL);
CSecurityDesc desc;

CSecurityAttributes secattr(desc);
// limit the access rights to the named event
CHandle hEvent(CreateEvent(&secattr, FALSE, FALSE, "NamedEvent"));

// try open it with all access rights
CHandle hEventOpen(OpenEvent(EVENT_ALL_ACCESS, FALSE, "NamedEvent"));
// if deny ace was added above it should fail. If allow ace
// was added, it should succeed
if(GetLastError() == ERROR_ACCESS_DENIED)
    ATLTRACE("\n\nAccess denied when opening event\n\n");

CSecurityDesc descCheck;
AtlGetSecurityDescriptor("NamedEvent",SE_KERNEL_OBJECT, &descCheck);
CDacl daclCheck;
CSid::CSidArray sidarr;
for(UINT i=0; i<sidarr.GetCount(); ++i)
    ATLTRACE("%s\n", (LPCTSTR)sidarr[i].Sid());
    ATLTRACE("%s\n", (LPCTSTR)sidarr[i].AccountName());
    ATLTRACE("%s\n", (LPCTSTR)sidarr[i].Domain());

"Security Classes"(MSDN)


#import can now take progid/libid and not just hard coded filename. For example:

#import <libid:20000000C-500A-100B-B005-000000000000> version("2.2")
#import "progid:SomeServer.Blah.1" embedded_idl, no_namespace

CComModule is replaced with other classes, such as: CAtlComModule/CAtlWinModule/CAtlDllModuleT/CAtlExeModuleT/CAtlServiceModuleT/etc
No more need to call Init()/Term() explicitly. The methods are now called by the appropriate ctor/dtor of the module classes.

No more need for BEGIN_OBJECT_MAP/END_OBJECT_MAP. OBJECT_ENTRY_AUTO is used instead. It's placed at global scope
and is auto generated into the ATL object map at link time. It uses linker segments to gather all the globally declared map entries into one list.

CComCurrency - wrapper for CURRENCY type
CAtlException - defines ATL related exception

CImage - depends on GDI+. Can work with jpeg, gif, bmp, png. See "CImage Limitations with Earlier Operating Systems" (MSDN)

_U_STRINGorID/_U_MENUorID/_U_RECT/CPoint/CRect/CSize - various simple wrappers. See "Utility Classes" (MSDN)

Synchronization wrappers defined in atlsync.h: CCriticalSection, CEvent, CMutex, and CSemaphore. But they are not documented in MSDN.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Leon Finker
United States United States
No Biography provided

Comments and Discussions

GeneralCInterfaceList Pin
alireza_shokoie4-Oct-05 23:24
Memberalireza_shokoie4-Oct-05 23:24 
GeneralJust another MFC Pin
LiangChen13-Jun-05 20:31
MemberLiangChen13-Jun-05 20:31 
GeneralCComAutoThreadModule Pin
Eric Smith15-Jan-04 4:10
MemberEric Smith15-Jan-04 4:10 
GeneralRe: CComAutoThreadModule Pin
Leon Finker15-Jan-04 5:54
MemberLeon Finker15-Jan-04 5:54 
GeneralRe: CComAutoThreadModule Pin
esmithx15-Jan-04 11:16
Memberesmithx15-Jan-04 11:16 
GeneralRe: CComAutoThreadModule Pin
Leon Finker15-Jan-04 14:33
MemberLeon Finker15-Jan-04 14:33 
Generalnice article Pin
Balkrishna Talele16-Dec-03 23:04
MemberBalkrishna Talele16-Dec-03 23:04 
GeneralLarge strings in ATL3 Pin
Irek Zielinski25-Jul-03 2:23
MemberIrek Zielinski25-Jul-03 2:23 
GeneralRe: Large strings in ATL3 Pin
Leon Finker25-Jul-03 5:52
MemberLeon Finker25-Jul-03 5:52 
GeneralCString MFC/non-MFC. Pin
Peter Weyzen30-Jun-03 10:05
MemberPeter Weyzen30-Jun-03 10:05 
GeneralIErrorInfo Pin
Anonymous19-Nov-02 4:21
MemberAnonymous19-Nov-02 4:21 
GeneralRe: IErrorInfo Pin
Boze23-Dec-02 6:53
MemberBoze23-Dec-02 6:53 
GeneralATL7 Book Pin
Soliant3-Jul-02 11:49
MemberSoliant3-Jul-02 11:49 
GeneralRe: ATL7 Book Pin
Kurt Muellner22-Jul-02 6:26
MemberKurt Muellner22-Jul-02 6:26 
GeneralRe: ATL7 Book Pin
Aldamo26-Aug-03 18:59
MemberAldamo26-Aug-03 18:59 
GeneralRe: ATL7 Book Pin
Kurt Muellner26-Aug-03 21:12
MemberKurt Muellner26-Aug-03 21:12 
GeneralRe: ATL7 Book Pin
GotCodeToo15-Nov-06 13:42
MemberGotCodeToo15-Nov-06 13:42 
GeneralRunning code when the compenent is being registered Pin
AndyC6-May-02 23:02
MemberAndyC6-May-02 23:02 
GeneralRe: Running code when the compenent is being registered Pin
Leon Finker7-May-02 7:58
MemberLeon Finker7-May-02 7:58 
GeneralThanks for the Info Pin
Joao Vaz30-Apr-02 7:00
MemberJoao Vaz30-Apr-02 7:00 
GeneralThanx Pin
Leon Finker30-Apr-02 7:11
MemberLeon Finker30-Apr-02 7:11 
QuestionNot too much things changed to jump from 3 to 7 version number ? Pin
Braulio Dez29-Apr-02 22:09
MemberBraulio Dez29-Apr-02 22:09 
AnswerRe: Not too much things changed to jump from 3 to 7 version number ? Pin
Leon Finker30-Apr-02 6:45
MemberLeon Finker30-Apr-02 6:45 
GeneralMMC SnapIn Pin
Slava Vassiliev29-Apr-02 21:44
MemberSlava Vassiliev29-Apr-02 21:44 
GeneralRe: MMC SnapIn Pin
Leon Finker30-Apr-02 6:37
MemberLeon Finker30-Apr-02 6:37 

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.