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

Non-MFC String Class for ATL

, 11 Aug 2000
Rate this:
Please Sign up or sign in to vote.
A Non-MFC String Class for use in ATL components
  • 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

    Share

    About the Author

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

    Comments and Discussions

     
    Questionabout operator >> in string class Pinmembermingtotti20-Apr-07 19:10 
    GeneralPretty class! PinmemberSergey Kolomenkin3-Jun-05 14:17 
    GeneralUpdated CCOMString PinmemberSergey Kolomenkin12-Jun-05 13:01 
    QuestionAre there leaks in the class? PinmemberbigZidane16-Mar-05 22:15 
    AnswerRe: Are there leaks in the class? PinmemberbigZidane17-Mar-05 17:10 
    QuestionC2558 Error with VS.2003 ? Pinmemberbalbi2117-Oct-04 21:48 
    AnswerRe: C2558 Error with VS.2003 ? PinmemberTim Musschoot25-Oct-04 2:34 
    Generalbest Pinmemberaming9914-Mar-04 23:37 
    GeneralCCOMString::Format Pinmemberaming9914-Mar-04 23:07 
    GeneralConvertion from CCOMString Pinmemberolvsh19-Dec-03 7:36 
    Could someone please help with a piece of code to convert CCOMString back into BSTR?
     
    Sorry, I'm a beginner.
    GeneralRe: Convertion from CCOMString Pinmemberolvsh19-Dec-03 8:12 
    GeneralCCOMString::TrimLeft() PinsussRalph Hosking7-Dec-03 15:34 
    GeneralAdded FindReverse - here's the code PinmemberJohn Simmons / outlaw programmer31-Mar-03 6:06 
    GeneralPocketPC 2002 Version PinmemberJohn Simmons / outlaw programmer31-Mar-03 5:06 
    Generalconcatenation error when using long string PinsussAnonymous17-Nov-02 21:04 
    QuestionIs CCOMString COM code? PinsussAnonymous27-Sep-02 9:37 
    QuestionYet another string? Why? PinmemberVagif Abilov27-Jul-02 5:22 
    AnswerRe: Yet another string? Why? PinmemberTim Smith27-Jul-02 5:50 
    GeneralRe: Yet another string? Why? PinmemberVagif Abilov27-Jul-02 10:02 
    GeneralTerrible and bugsful code!!! PinsussAnonymous23-Jul-02 22:21 
    GeneralRe: Terrible and bugsful code!!! PinmemberJerry Brown24-Jul-02 10:53 
    GeneralRe: Terrible and bugsful code!!! PinmemberbigZidane17-Mar-05 17:31 
    Generalstring compare with wild card Pinmembercym16-Jun-02 23:11 
    GeneralRe: string compare with wild card Pinmemberreal name16-Jun-02 23:40 
    GeneralRe: string compare with wild card Pinmemberreal name16-Jun-02 23:44 

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

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

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