Click here to Skip to main content
12,689,695 members (23,285 online)
Click here to Skip to main content


26 bookmarked

WTL CString Class Implemented with the Standard C++ Library

, 28 Sep 2006 CPL
Plug-in CodeProject's CStdString as WTL::CString and get the WTL CString support with a Standard C++ Library implementation.
// atlssmisc.h 
// Use class CStdString as WTL::CString: 
//		#include this file instead of <atlmisc.h> if you want WTL::CString to be a  
//		CStdString deriving from std::basic_string<TCHAR>.
// Copyright (c) Alain Rist 2006
// The use and distribution terms for this software are covered by the
// Common Public License 1.0 (
// By using this software in any fashion, you are agreeing to be bound by
// the terms of this license. You must not remove this notice, or
// any other, from this software.
// Notes: 
//		atlssmisc.h should be included first of WTL headers after atlwin.h.
//		StdString.h must be accessible by the compiler through angle brackets: #include <StdString.h>.
//		VS2005 and VCExpress projects should '#define _CRT_SECURE_NO_DEPRECATE' before '#include <atlbase.h>'
//		eVC4 projects should compile with /GX to enable unwind semantics for C++ exception handler.

// Recommended stdafx.h layout
// stdafx.h 
// Change these values to use different versions

#include <atlbase.h>

//#include <atlstr.h>  // uncomment for WTL ATL::CString support
//#define _WTL_NO_CSTRING // uncomment for WTL ATL::CString support or no WTL CString suppport
//#define _WTL_USE_CSTRING // uncomment for full WTL WTL::CString support

#include <atlapp.h>

extern CAppModule _Module;

#include <atlwin.h>

//#include <atlmisc.h> // uncomment AND comment next line for original WTL:CString defintion
#include <atlssmisc.h> // CStdString based WTL:CString defintion

#ifndef __ATLSSMISC_H__
	#define __ATLSSMISC_H__

#pragma once

#if _WTL_VER > 0x750
	#error atlssmisc.h requires WTL version under or equal to 7.5

#ifdef __ATLMISC_H__
	#error atlssmisc.h cannot be included after atlmisc.h

#if defined(_WTL_USE_CSTRING) && (defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__) || defined(_WTL_NO_CSTRING))
	#error Incompatible '#define xxSTRxx's found.

// push CStdString in global namespace
#include "StdString.h"	

#if defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__) // project uses ATL::CString
	#define _WTL_NO_CSTRING
#endif // defined(_ATL_TMP_NO_CSTRING) || defined(__ATLSTR_H__)

	#define _WTL_USE_CSTRING 0

#if !defined(_WTL_USE_CSTRING) && !defined(_WTL_NO_CSTRING)
	#define _WTL_USE_CSTRING 1
#endif // !defined(_WTL_USE_CSTRING) && !defined(_WTL_NO_CSTRING)


// push CStdString as WTL::CString
namespace WTL
#if _WTL_USE_CSTRING == 1	// class CString not forward declared
	// push CStdString as WTL::CString
	typedef ::CStdString CString;
#else						// atlapp.h may have declared class CString;
	// derive CString from CStdString
	class CString : public ::CStdString
	// These typedefs are private to CStdString so are their CString equivalents
		typedef ::CStdString				MYBASE;	 // my base class 
		typedef CString						MYTYPE;	 // myself
		typedef  MYBASE::const_iterator		MYCITER; // you get the idea...
		typedef  MYBASE::size_type			MYSIZE;   
		typedef  MYBASE::value_type			MYVAL; 
		typedef  MYBASE::allocator_type		MYALLOC;
	// CStdString constructors propagated to CString
		CString() : ::CStdString(){}
		CString(const MYTYPE& str) : ::CStdString(str){}
		CString(const std::string& str) : ::CStdString(str){}
		CString(const std::wstring& str) : ::CStdString(str){}
		CString(PCSTR pA, MYSIZE n) : ::CStdString(pA, n){}
		CString(PCWSTR pW, MYSIZE n) : ::CStdString(pW, n){}
		CString(PCUSTR pU) : ::CStdString(pU){}
		CString(PCSTR pA) : ::CStdString(pA){}
		CString(PCWSTR pW) : ::CStdString(pW){}
		CString(MYCITER first, MYCITER last) : ::CStdString(first, last){}
		CString(MYSIZE nSize, MYVAL ch, const MYALLOC& al=MYALLOC()) : ::CStdString(nSize, ch, al){}
		CString(const _bstr_t& bstr) : ::CStdString(bstr){}
#endif // SS_INC_COMDEF
#endif // _WTL_USE_CSTRING == 1
}; // Namespace WTL

// prepare inclusion of atlmisc.h without WTL::CString
	#define _WTL_NO_CSTRING
	#define __ATLSTR_H__ // for CFindFile and CRecentDocumentList CString support
#endif // _WTL_USE_CSTRING


// set back proper macro definitions
	#undef __ATLSTR_H__

#endif // __ATLSSMISC_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

Alain Rist
France France
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170113.4 | Last Updated 29 Sep 2006
Article Copyright 2006 by Alain Rist
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid