Before going any further, you should read CString-clone Using Standard C++: A Drop-In replacement for CString that builds on the Standard C++ Library's basic_string template by Joe O'Leary. You can get some more info about the CStdString here.
CStdString
This article shows how CStdString, initially designed to replace the MFC CString, may as well smoothly replace WTL:CString, in any WTL project, including WinCE projects compiled with eVC4 SP4 or VS2005. The attached StdString.h is adapted from the last original revision dated 2005-Jan-10 for full WTL:CString and WinCE compliance.
CString
WTL:CString
When including the attached StdString.h in your project, you access the class CStdString actually defined as either CStdStringA or CStdStringW, depending on your project Unicode/MBCS settings.
CStdStringA
CStdStringW
// FILE: StdString.h
// Changes tagged with '// AR' copyright (c) Alain Rist 2006:
// AUTHOR: Joe O'Leary (with outside help noted in comments)
//...
template <CT>class CStdStr : public std::basic_string<CT>
//...
typedef std::basic_string<TCHAR> tstring;
//...
typedef CStdStr<char> CStdStringA; // a better std::string
typedef CStdStr<wchar_t> CStdStringW; // a better std::wstring
typedef CStdStr<OLECHAR> CStdStringO; // almost always CStdStringW
//...
#ifdef UNICODE
typedef CStdStringW CStdString;
#else
typedef CStdStringA CStdString;
//...
So, you have four CStdString[X] classes deriving from std::basic_string at your fingertips. Each can safely be static_casted to and from one of std::string or std::wstring.
CStdString[X]
std::basic_string
static_cast
std::string
std::wstring
These classes have all possibly needed type constructors and assignment operators so that the following code will compile without errors and produce the same result in UNICODE and MBCS builds:
CStdString ss("My standard string"); // as with WTL::CString no _T() needed
CStdStringW sw = ss; // no WTL::CStringW and WTL::CStringA available
CStdStringA sa = "àáâãäåæçèéêëìíîïñòóôõöøùúûüýþÿ";
sw = sa; // different character widths
Note that getting rid of the _T() macros may be very handy for lazy typers like me, but costs, at runtime, the price of conversion if both side types are different.
_T()
As defined in the attached StdString.h, CStdString exposes all WTL::CString constructors, operators, and function members with one difference: the CStdString[X] constructors that take a character and a count takes them in the order (count, value), which is the opposite of the order WTL::CString(TCHAR ch, int nLength) declares them.
WTL::CString
WTL::CString(TCHAR ch, int nLength)
Except for this constructor, call on CStdString any WTL::CString member, operator, and (LPCTSTR) cast, for instance:
(LPCTSTR)
CStdString ss1(MAKEINTRESOURCE(IDR_MYID)); // construct from resource ID
ss1.LoadString(IDS_MYSTRING); // load from resource
CWindow(hWnd).SetDlgItemText(ID_MYCONTROL, ss1); // use (LPCTSTR)cast
ss1.MakeLower(); // change content
CStdString ss2 = ss1.Right(1); // extraction
if (!ss1.IsEmpty()) // anything here?
ss2.Replace(ss1[ss1.GetLength() -1], '?'); // just to play
To use CStdString in WTL projects, unzip the WtlStdString.zip files to some place accessible by your compiler through angle brackets, and:
#include <StdString.h>
#define _CRT_SECURE_NO_DEPRECATE
int nLen = lstrlen(m_pszMsg); // was ::lstrlen(m_pszMsg);
WTL 7.0 and over is designed to support one of ATL::CString or WTL::CString depending on the compile time conditions, some macro definitions, and the inclusion order of the headers; ATL::CString comes with ATL version 7.0; and VC++7.0 and over, eVC, and VCExpress/Platform SDK use ATL 3.0 and do not get it.
ATL::CString
The following rules apply when _ATL_NO_AUTOMATIC_NAMESPACE and _WTL_NO_AUTOMATIC_NAMESPACE are both undefined:
_ATL_NO_AUTOMATIC_NAMESPACE
_WTL_NO_AUTOMATIC_NAMESPACE
#define _WTL_NO_CSTRING
#include<atlmisc.h>
#include<atlstr.h>
#include<atlapp.h>
#define _WTL_USE_CSTRING
Depending on rule 2, the _CSTRING_NS macro defined in atlapp.h will expand to ATL or WTL and silently map the application CString to WTL::CString or ATL::CString. So, we can write code like:
_CSTRING_NS
ATL
WTL
// MyWindow.h
// ...
CFindFile ff;
ff.FindFile(_T("C:*.*"));
CListBox lb = GetDlgItem(ID_MYLB);
//
CString sText = ff.GetFileName();
// actually returns a _CSTRING_NS::CString
lb.GetText(0, sText);
// actually requires a _CSTRING_NS::CString&
//
To benefit the CStdString implementation of WTL::CString and WTL CString support, unzip WtlStdString.zip files to a directory accessible by your compiler through angle brackets. The included atlssmisc.h will:
#include "StdString.h"
::CStdString
#define
Note that atlssmisc.h relies on the implementation details in atlmisc.h and atlapp.h, so do not compile with WTL over version 7.5.
Change #include <atlmisc.h> to #include <atlssmisc.h> in the headers, and check the CStdString integration in WTL projects requirements. That's all.
#include <atlmisc.h>
#include <atlssmisc.h>
Experiment it with the WTL samples Alpha (Win32) and ImageView (WinCE).
Paste the following template to stdafx.h, and adjust the commented lines for your needs.
/////////////////////////////////////////////////////////////
// Recommended stdafx.h layout
//...
// Change these values to use different versions
//...
#define _CRT_SECURE_NO_DEPRECATE // avoid StdString.h deprecation warnings
#include <span class="code-keyword"><atlbase.h></span>
CMenuT<>::GetMenuString()
CDCT<>::GetTextFace()
typedef ::CStdString CString;
#define _WTL_USE_CSTRING // ...
class CString: public ::CStdString
For WinCE projects, check the CStdString integration in WTL projects requirements.
Edit stdafx.h to get WTL::CString support, and #include <atlssmisc.h> first after atlwin.h. Check the CStdString integration in WTL projects requirements.
For instance, to plug CStdString based WTL::CString into the great Wizard97Test sample with a VC71 compiler, edit stdafx.h like this:
// stdafx.h: Wizard97Test WTL sample
//...
// Includes
#include <span class="code-string">"resource.h"</span>
#include <span class="code-keyword"><atlbase.h></span>
/********************************* comment or cut from here
#if (_ATL_VER >= 0x0700)
#include <atlstr.h>
#include <atltypes.h>
#endif
// WTL related preprocessor definitions
#if (_ATL_VER >= 0x0700)
#define _WTL_NO_WTYPES
#define _WTL_NO_UNION_CLASSES
#define _WTL_NO_CSTRING
#endif
************************************** to here */
#define _WTL_NEW_PAGE_NOTIFY_HANDLERS
#include <span class="code-keyword"><atlapp.h></span>
extern CAppModule _Module;
#include <span class="code-keyword"><atlwin.h></span>
#include <span class="code-keyword"><atlcom.h></span>
#include <span class="code-keyword"><atlssmisc.h> // instead of <atlmisc.h></span>
//...
There is some dispute about the compared efficiencies of std::basic_string, ATL::CString, and other implementations of stringish objects in C++. I will not argue there.
With CStdString based WTL::CString:
char
Thanks again to Joe O'Leary who did all the real work here, and enjoy WTL!
This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)
Joe O'Leary wrote:It would be great to combine all this stuff together and have just one version of the code that did what people need.
CStdStr::LoadString()
#include <windows.h>
#include "CeStsNet.h"
c_str()
template <class CharType,
class Traits=char_traits<CharType>,
class Allocator=allocator<CharType> >
class basic_string;
//...
std::basic_string<TCHAR> str1("Hello world");
LPCTSTR pstr1 = str1.c_str();
//...
sDst
Defenestration wrote:the latest version of Joe O Leary's StdString.h has a modified date of 2005-DEC-19.
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.