Click here to Skip to main content
6,597,576 members and growing! (23,668 online)
Email Password   helpLost your password?
General Programming » String handling » General     Intermediate

Non-MFC String Class for ATL

By Paul E. Bible

A Non-MFC String Class for use in ATL components
VC6, ATL, Dev
Posted:27 Mar 2000
Updated:11 Aug 2000
Views:163,317
Bookmarked:26 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
36 votes for this article.
Popularity: 6.99 Rating: 4.49 out of 5
1 vote, 8.3%
1
1 vote, 8.3%
2

3

4
10 votes, 83.3%
5
  • 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


    Member
    Eat, Sleep, Code, Bike. That about sums it up!!
    Occupation: Software Developer (Senior)
    Location: United States United States

    Other popular String handling articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 64 (Total in Forum: 64) (Refresh)FirstPrevNext
    Questionabout operator >> in string class Pinmembermingtotti19:10 20 Apr '07  
    GeneralPretty class! PinmemberSergey Kolomenkin14:17 3 Jun '05  
    GeneralUpdated CCOMString PinmemberSergey Kolomenkin13:01 12 Jun '05  
    GeneralAre there leaks in the class? PinmemberbigZidane22:15 16 Mar '05  
    GeneralRe: Are there leaks in the class? PinmemberbigZidane17:10 17 Mar '05  
    GeneralC2558 Error with VS.2003 ? Pinmemberbalbi2121:48 17 Oct '04  
    GeneralRe: C2558 Error with VS.2003 ? PinmemberTim Musschoot2:34 25 Oct '04  
    Generalbest Pinmemberaming9923:37 14 Mar '04  
    GeneralCCOMString::Format Pinmemberaming9923:07 14 Mar '04  
    GeneralConvertion from CCOMString Pinmemberolvsh7:36 19 Dec '03  
    GeneralRe: Convertion from CCOMString Pinmemberolvsh8:12 19 Dec '03  
    GeneralCCOMString::TrimLeft() PinsussRalph Hosking15:34 7 Dec '03  
    GeneralAdded FindReverse - here's the code PinmemberJohn Simmons / outlaw programmer6:06 31 Mar '03  
    GeneralPocketPC 2002 Version PinmemberJohn Simmons / outlaw programmer5:06 31 Mar '03  
    Generalconcatenation error when using long string PinsussAnonymous21:04 17 Nov '02  
    GeneralIs CCOMString COM code? PinsussAnonymous9:37 27 Sep '02  
    GeneralYet another string? Why? PinmemberVagif Abilov5:22 27 Jul '02  
    GeneralRe: Yet another string? Why? PinmemberTim Smith5:50 27 Jul '02  
    GeneralRe: Yet another string? Why? PinmemberVagif Abilov10:02 27 Jul '02  
    GeneralTerrible and bugsful code!!! PinsussAnonymous22:21 23 Jul '02  
    GeneralRe: Terrible and bugsful code!!! PinmemberJerry Brown10:53 24 Jul '02  
    GeneralRe: Terrible and bugsful code!!! PinmemberbigZidane17:31 17 Mar '05  
    Generalstring compare with wild card Pinmembercym23:11 16 Jun '02  
    GeneralRe: string compare with wild card Pinmemberreal name23:40 16 Jun '02  
    GeneralRe: string compare with wild card Pinmemberreal name23:44 16 Jun '02  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 11 Aug 2000
    Editor: Chris Maunder
    Copyright 2000 by Paul E. Bible
    Everything else Copyright © CodeProject, 1999-2009
    Web21 | Advertise on the Code Project