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

Non-MFC String Class for ATL

By , 11 Aug 2000
 
  • Download demo project - 22 Kb
  • Download source files - 6 Kb
  • Introduction

    When writing ATL components, I have found that one of the hardest things for Windows programmers to overcome is the dependency on the MFC library. For dates and strings, MFC simplifies the programmer's job by handling memory allocations and type conversions within its class encapsulation. However, with that simplification comes the price of having to distribute the MFC DLL's with your component.

    This article focuses on the issues surrounding the use of strings in an ATL component. If you don't include MFC support in your ATL component, you obviously won't have the CString available to use. There have been several articles published that address this issue. Several of the ones that I have read are:

    • NonMFC:CString (posted on www.worldofatl.com) - This article provides a new CString class based on the standard template library (STL).
    • Add MFC's CString to ATL w/ No MFC dependencies (by K. Shane Triem (www.codeguru.com) - This article illustrates how to use a Visual Studio macro to extract the CString class source code from the MFC library and then use this class in your ATL component.

    CCOMString

    While both of these articles are viable alternatives to the MFC CString class, I wanted a class that would compile under both ANSI and Unicode and had no dependencies whatsoever: neither MFC or STL. My alternative was to write a string handling class entitled CCOMString which is based entirely on Visual C++'s TCHAR datatype.

    The TCHAR datatype is defined as follows:

    • As a char when _MBCS is defined in your program.
    • As a wchar_t when _UNICODE is defined in your program.
    • As a char when neither _MBCS or _UNICODE is defined in your program.

    Therefore, by using the TCHAR datatype, CCOMString is compatible with both ANSI and Unicode compile modes. Please note that there is one caveat to this class in its present state of development: it does not currently include exception handling for the memory allocation functions. I am currently trapping any memory allocation errors with the ATLASSERT macro, but these will compile away in Release mode (I'm still researching a way to handle these errors without adding the exception handling overhead to the class).

    The current version of the CCOMString includes the following functionality. Please note, if there are any functions in the CCOMString class that you download that are not listed in the text below, they are considered to be undocumented and, therefore, possibly untest as well:

    Constructors

  • CCOMString() - Constructs an empty string.
  • CCOMString(CCOMString&) - Constructs a string from another CCOMString.
  • CCOMString(LPCTSTR) - Constructs a string from a LPCTSTR (i.e., const TCHAR*).
  • CCOMString(BSTR) - Constructs a string from a BSTR.
  • CCOMString(TCHAR, int) - Constructs a string from a character repeated an indicated number of times.
  • Assignment Operations

  • operator=(CCOMString&) - Copies another CCOMString object to the CCOMString object.
  • operator=(LPCTSTR) - Copies a LPCTSTR to the CCOMString object.
  • operator=(BSTR) - Copies a BSTR to the CCOMString object.
  • operator LPCTSTR() - Returns a const TCHAR* from the CCOMString object.
  • TCHAR* GetString() - Obtains a pointer to the string contained in the CCOMString object.
  • BSTR AllocSysString() - Allocates a BSTR from the CCOMString object.
  • Concatenation

  • <code>operator+=(CCOMString&) - Concatenates a CCOMString object to the end of the CCOMString object.
  • operator+=(LPCTSTR) - Concatenates a const TCHAR* to the end of the CCOMString object.
  • operator+=(BSTR) - Concatenates a BSTR to the end of the CCOMString object.
  • operator+=(TCHAR) - Concatenates a TCHAR to the end of the CCOMString object.
  • operator+(CCOMString&, CCOMString&) - Concatenates two CCOMString objects together.
  • operator+(CCOMString&, LPCTSTR) - Concatenates a CCOMString object and a const TCHAR* together.
  • operator+(LPCTSTR, CCOMString&) - Concatenates a const TCHAR* and CCOMString object together.
  • operator+(CCOMString&, BSTR) - Concatenates a CCOMString object and a BSTR together.
  • operator+(BSTR, CCOMString&) - Concatenates a BSTR and a CCOMString object together.
  • Accessors for the String as an Array

  • GetLength() - Returns the length of the CCOMString string as an integer.
  • IsEmpty() - Returns TRUE or FALSE depending on whether or not the CCOMString string is empty.
  • Empty() - Sets the CCOMString string to an empty string (i.e., _T("")).
  • GetAt(int) - Returns a TCHAR character from the CCOMString string at the specified location.
  • operator[] (int) - Same functionality as GetAt(int).
  • SetAt(int, TCHAR) - Sets the character at the specified location of the CCOMString string to the specified character.
  • Conversions

  • MakeUpper() - Converts the CCOMString string into all uppercase characters.
  • MakeLower() - Converts the CCOMString string into all lowercase characters.
  • MakeReverse() - Reverses the sequence of the characters contained in the CCOMString string.
  • TrimLeft() - Removes all spaces from the left-hand side of the CCOMString string.
  • TrimRight() - Removes all spaces from the right-hand side of the CCOMString string.
  • Searching

  • Find(TCHAR) - Returns the first position in the CCOMString string of the specified character.
  • Find(TCHAR, int nStart) - Returns the first position after the nStart position in the CCOMString string of the specified character.
  • Find(LPCTSTR lpszSub) - Returns the first position in the CCOMString string of the specified character string.
  • Find(LPCTSTR lpszSub, int nStart) - Returns the first position after the nStart position in the CCOMString string of the specified character string.
  • Extraction

  • Mid(int) - Returns a CCOMString object containing the character starting at the specified position to the end of the CCOMString string.
  • Mid(int, int) - Returns a CCOMString object containing the character starting at the specified position and extending for the specified length of the CCOMString string.
  • Left(int nCount) - Returns nCount characters starting at the left-hand side of the CCOMString string.
  • Right(int nCount) - Returns nCount characters starting at the right-hand side of the CCOMString string.
  • Replacing

  • Replace(TCHAR chOld, TCHAR chNew) - Replaces all the chOld characters in the CCOMString string with the chNew character.
  • Replace(LPCTSTR lpszOld, LPCTSTR lpszNew) - Replaces all the lpszOld strings in the CCOMString string with the lpszNew string.
  • Comparison

  • Compare(CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if equal and non-zero if not equal. This comparision is case sensitive.
  • Compare(LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if equal and non-zero if not equal. This comparision is case sensitive.
  • operator==(const CCOMString&, const CCOMString&) - Same functionality as Compare(CCOMString&).
  • operator==(const CCOMString&, LPCTSTR) - Same functionality as Compare(LPCTSTR).
  • operator==(LPCTSTR, const CCOMString&) - Same functionality as Compare(LPCTSTR).
  • operator!=(const CCOMString&, const CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if not equal and non-zero if equal. This comparision is case sensitive.
  • operator!=(const CCOMString&, LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if not equal and non-zero if equal.This comparision is case sensitive.
  • operator!=(LPCTSTR, const CCOMString&) - Same functionality as operator !=(const CCOMString&, LPCTSTR).
  • CompareNoCase(CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if equal and non-zero if not equal. This comparision is not case sensitive.
  • CompareNoCase(LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if equal and non-zero if not equal. This comparision is not case sensitive.
  • Formatting

  • Format(LPCTSTR, ...) - Formats the string similar to the C function printf().
  • History

    28 June 2000 - memory leak fix in source

    12 August 2000 - updated source ZIP file

    License

    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

    Paul E. Bible
    Software Developer (Senior)
    United States United States
    Member
    Eat, Sleep, Code, Bike. That about sums it up!!

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    Questionabout operator >> in string classmembermingtotti20 Apr '07 - 18:10 
    GeneralPretty class!memberSergey Kolomenkin3 Jun '05 - 13:17 
    GeneralUpdated CCOMStringmemberSergey Kolomenkin12 Jun '05 - 12:01 
    QuestionAre there leaks in the class?memberbigZidane16 Mar '05 - 21:15 
    AnswerRe: Are there leaks in the class?memberbigZidane17 Mar '05 - 16:10 
    QuestionC2558 Error with VS.2003 ?memberbalbi2117 Oct '04 - 20:48 
    AnswerRe: C2558 Error with VS.2003 ?memberTim Musschoot25 Oct '04 - 1:34 
    Generalbestmemberaming9914 Mar '04 - 22:37 
    GeneralCCOMString::Formatmemberaming9914 Mar '04 - 22:07 
    GeneralConvertion from CCOMStringmemberolvsh19 Dec '03 - 6:36 
    GeneralRe: Convertion from CCOMStringmemberolvsh19 Dec '03 - 7:12 
    GeneralCCOMString::TrimLeft()sussRalph Hosking7 Dec '03 - 14:34 
    GeneralAdded FindReverse - here's the codememberJohn Simmons / outlaw programmer31 Mar '03 - 5:06 
    GeneralPocketPC 2002 VersionmemberJohn Simmons / outlaw programmer31 Mar '03 - 4:06 
    Generalconcatenation error when using long stringsussAnonymous17 Nov '02 - 20:04 
    QuestionIs CCOMString COM code?sussAnonymous27 Sep '02 - 8:37 
    QuestionYet another string? Why?memberVagif Abilov27 Jul '02 - 4:22 
    AnswerRe: Yet another string? Why?memberTim Smith27 Jul '02 - 4:50 
    GeneralRe: Yet another string? Why?memberVagif Abilov27 Jul '02 - 9:02 
    GeneralTerrible and bugsful code!!!sussAnonymous23 Jul '02 - 21:21 
    GeneralRe: Terrible and bugsful code!!!memberJerry Brown24 Jul '02 - 9:53 
    GeneralRe: Terrible and bugsful code!!!memberbigZidane17 Mar '05 - 16:31 
    Generalstring compare with wild cardmembercym16 Jun '02 - 22:11 
    GeneralRe: string compare with wild cardmemberreal name16 Jun '02 - 22:40 
    GeneralRe: string compare with wild cardmemberreal name16 Jun '02 - 22:44 
    GeneralConstructorsmemberHolger Persch19 Nov '01 - 20:42 
    GeneralBug in CCOMString::FormatmemberHolger Persch19 Nov '01 - 20:11 
    Questionhow do i do memberhan5 Sep '01 - 4:39 
    GeneralNice classmemberJoe O'Leary25 Aug '01 - 17:25 
    GeneralWTL CStringmemberSteve Maier19 Aug '01 - 7:52 
    GeneralRe: WTL CStringsussAnonymous10 Oct '02 - 16:20 
    GeneralRe: WTL CStringmemberSteve Maier10 Oct '02 - 22:16 
    GeneralRe: WTL CStringsussAnonymous13 Oct '02 - 16:01 
    GeneralStatic linkingmemberck14 Aug '01 - 9:53 
    GeneralBug in Format MethodmemberSteven Gardell24 Jan '01 - 1:34 
    GeneralException handling.memberDemir Ateser12 Dec '00 - 5:53 
    GeneralRe: Exception handling.memberAnonymous12 Dec '00 - 8:13 
    GeneralRe: Exception handling.memberDemir Ateser13 Dec '00 - 10:52 
    GeneralRe: Exception handling.memberDemir Ateser13 Dec '00 - 11:10 
    QuestionTrimLeft()/TrimRight() params?memberTony Tanzillo7 Nov '00 - 3:36 
    AnswerRe: TrimLeft()/TrimRight() params?memberManoj Patra4 May '01 - 14:20 
    GeneralCCOMString::CCOMString problemsussAnand Ranganathan27 Sep '00 - 4:21 
    GeneralRe: CCOMString::CCOMString problemsussAnand Ranganathan27 Sep '00 - 4:35 
    GeneralRe: CCOMString::CCOMString problemsussAnand Ranganathan27 Sep '00 - 4:45 
    Generalusing a CCOMString as format argumentsussAnand20 Sep '00 - 5:01 
    GeneralRe: using a CCOMString as format argumentmemberJoe O'Leary25 Aug '01 - 17:12 
    GeneralReverseFind & FindOneOf missingsussBenjamin Mayrargue18 Aug '00 - 3:32 
    GeneralCommendable EffortsussBrian Hart4 Aug '00 - 4:23 
    GeneralRe: Commendable EffortsussAnonymous23 Jul '02 - 20:25 
    GeneralBug in CCOMString::CCOMString() fixedsussMarcus Fries27 Jul '00 - 0:50 

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

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.6.130513.1 | Last Updated 12 Aug 2000
    Article Copyright 2000 by Paul E. Bible
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid