atl >> CString and Unicode

by Peter Carlson » Wed, 17 Dec 2003 00:55:56 GMT

I am trying to convert my app to unicode and have run into the following
error message. The offending class / line is below..the class extends
CString for some missing functionality. The line should allow me to do
this:

SSoft::CString cs1;
ATL::CString cs2;
cs1 = cs2;

class CString : public ATL::CAtlString {
public:
CString& operator =(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str) {
CAtlString::operator =(str); return *this; }
};

Thanks!
Peter
Compiling...
stdafx.cpp
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(849) : error C2039: 'GetDefaultManager' :
is not a member of 'ATL::ChTraitsCRT'
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(750) : see declaration of
'ATL::ChTraitsCRT'
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(848) : while compiling class-template
member function 'ATL::CStringT::CStringT(const ATL::CSimpleStringT::YCHAR
*)'
with
[
BaseType=TCHAR,
StringTraits=ATL::ChTraitsCRT
]
c:\projects\ssoft_common\ssoft_misc.h(82) : see reference to class
template instantiation 'ATL::CStringT' being compiled
with
[
BaseType=TCHAR,
StringTraits=ATL::ChTraitsCRT
]
c:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\atlmfc\include\cstringt.h(849) : error C3861: 'GetDefaultManager':
identifier not found, even with argument-dependent lookup






atl >> CString and Unicode

by Peter Carlson » Wed, 17 Dec 2003 22:57:40 GMT


Soft is our library that extends CString for some missing functionality.
The whole definition is below...funny thing is this works when it's MBCS,
but fails when in unicode. As stated in my previous post the offending line
is
CString& operator =(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str) {
CAtlString::operator =(str); return *this; }

There's more functions in the class like GetToken(), GetNameValue(),
DBFormat(), URLEncode(), URLDecode() but the ones below are sufficient to
reproduce the problem.

Peter

#pragma once
namespace SSoft {
typedef TCHAR* SSPCS;
#ifdef _UNICODE
#define _ttof(sz) atof_unicode(sz)
#else
#define _ttof(sz) atof(sz)
#endif //_UNICODE

class CString : public ATL::CAtlString {
public:
static double atof_unicode(const wchar_t *wcstr) {
static char mb[100];
wcstombs(mb, wcstr, sizeof(mb));
return atof(mb);
};

// constructors
CString () {};
CString (TCHAR *psz) : CAtlString(psz) {}

// conversion operators
operator bool() { return (CompareNoCase(_T("true")) == 0 ||
Compare(_T("1")) == 0) ? true : false; }
operator int() { return (!IsEmpty()) ? _ttoi(GetBuffer()) : 0; }
operator double() { return (!IsEmpty()) ? _ttof(GetBuffer()) : 0; }
operator long() { return (!IsEmpty()) ? _tcstoul(GetBuffer(), NULL, 0) :
0; }
operator unsigned long() { return (!IsEmpty()) ? _tcstoul(GetBuffer(),
NULL, 0) : 0; }
operator SSPCS() { return GetBuffer(); }
operator unsigned char*() { return reinterpret_cast<unsigned char*>
(GetBuffer()); }

// conditional operators
operator == (TCHAR * psz) { return (psz == NULL || _tcslen (psz) == 0) ?
(GetLength() == 0) : (Compare(psz) == 0); }
operator == (CString &str) { return( Compare( str ) == 0 ); }
operator == (CAtlString &str) { return( Compare( str ) == 0 ); }
operator == (bool b) { return( (bool)*this == 0 ); }
operator != (TCHAR * psz) { return (psz == NULL || _tcslen (psz) == 0) ?
(GetLength() != 0) : (Compare(psz) != 0); }
operator != (CString &str) { return( Compare( str ) != 0 ); }
operator != (const CAtlString &str) { return( Compare( str ) != 0 ); }
operator != (CAtlString &str) { return( Compare( str ) != 0 ); }
operator != (ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > &str) {
return( Compare( str ) != 0 ); }
operator != (bool b) { return( (bool)*this != 0 ); }

CString& operator =(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str) {
CAtlString::operator =(str); return *this; }
CString& operator =(TCHAR *pstr) { CAtlString::operator =(pstr); return
*this; }
CString& operator =(TCHAR pstr) { CAtlString::operator =(pstr); return
*this; }
CString& CString::operator=(long nData) { Format(_T("%ld"), nData); return
*this; }

// stream operators
CString& operator<<(SSoft::CString str) { CAtlString::operator
+=((LPCTSTR)str); return (*this); }
CString& operator<<(CAtlString& str) { CAtlString::operator
+=((LPCTSTR)str); return (*this); }
CString& operator<<(TCHAR ch) { CAtlString::operator +=(ch); return
(*this); }
CString& operator<<(LPCTSTR lpsz) { CAtlString::operator +=(lpsz); return
(*this); }
CString& operator<<(bool b) {
char buf[2] = {0};
buf[0] = (b) ? '1' : '0';
CAtlString::operator +=(buf);
return (*this);
}
CString& operator<<(int n) {
CString csTemp; csTemp.Format(_T("%d"), n);
CAtlString::operator +=(csTemp);
return (*this);
}

int Re

atl >> CString and Unicode

by v-garych » Thu, 18 Dec 2003 17:31:28 GMT

Hi Peter,

Thanks for your quickly reply!

I have defined the following code in my test program which can repro your
problem:

#include <cstringt.h>
using namespace ATL;
#define CAtlString CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> >

Is that your macro for the CAtlString?


Meantime, I found when using the header file atlstr.h instead of the
cstring.t:

#include <atlstr.h>
using namespace ATL;
#define CAtlString CString

Your SSoft library will be compiled OK.


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

atl >> CString and Unicode

by v-garych » Wed, 24 Dec 2003 18:00:24 GMT

Hi Peter,

Thanks for your quickly response!

The following code need to construct an 'ATL::ChTraitsCRT<wchar_t>' object,
however this type has some problems to get GetDefaultManager() member
function:

CString& operator =(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str) {
CAtlString::operator =(str); return *this; }

How about to use a replaced argument type if possible?


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

atl >> CString and Unicode

by Peter Carlson » Thu, 25 Dec 2003 02:16:06 GMT

That's what I'm using to construct the object, but it wont compile becuase
of GetDefaultManager()....that's what I've been saying since the first post.
Is it not possible to do this or what will it take to fix it?

Peter



object,
rights.

atl >> CString and Unicode

by v-garych » Thu, 25 Dec 2003 16:58:42 GMT

Hi Peter,

Thanks for your quickly response!

With your sample project, I noticed there are two places :
if (found > 0) csNew = Left(found); //Line 211 in ssoft_misc.h
csNew = ""; //Line 220 in
ssoft_misc.h, if comment these 2 line, your code also compiled.

Addtional with your target operation:
SSoft::CString cs1;
ATL::CString cs2;
cs1 = cs2;

Use the operator:
CString& operator =(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str)

And meantime I found the Left(found) return a ATL:CString value as well as
the cs2's type.

So I think to replace the CString& operator
=(ATL::CStringT<TCHAR,ATL::ChTraitsCRT<TCHAR> > str){...} with CString&
operator =(ATL::CString str){...} maybe a workaround to your situation.


Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

atl >> CString and Unicode

by Peter Carlson » Fri, 26 Dec 2003 02:15:45 GMT

I had to do the following two things to get it to work:
1. (Not sure if this was necessary or not but it made things cleaner)
Change the class from
CString : public CAtlString to
template< typename BaseType, class StringTraits >
class ssCStringT : public CStringT<BaseType, StringTraits> {
with appropriate typedefs for unicode or ansi to CString

2. change the Left() and Right() functions in Replace to
baseClass::Right(right).GetBuffer(); - The addition of GetBuffer() seems
to be what fixed the ambiguity and the call to GetManager()

Peter



rights.

Similar Threads

1. printing UNICODE and convert CString to UNICODE

2. CString and Unicode

Hi,

I modify an exe to be able to use unicode, but this exe use adll which
is not compiled in unicode (no need to use Unicode string and i do not
have source files), my problem is that the dll has a function with a
CString argument, so i have an exception (Invalid Address specified to
RtlValidateHeap) because my CString is comming from my unicode exe .

I know how to write an ANSI string in a Unicode CString, but how can i
give to my dll a correct CString (ANSI) argument ?

I can not make any change in the dll (no source code)

Do you have any idea ?
thanks

3. CString to Unicode

4. Convert CString to unicode code points

Hello,

I need to display formatted unicode text in a CRichEditCtrl -
specifically Chinese or Arabic characters.

From the RTF specification, I need to write out to the rtf stream the
the following sequence: \uN?, where N represents the Unicode character
value expressed as a decimal number, and the question mark is what non-
unicode enabled readers would use to display the character/.

A snippet of an rtf with proper unicode encoding looks like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss
\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.21.2508;}\viewkind4\uc1\pard\f0\fs20\u-302?
\u-307?\u-287?\u-290?\u-327?\u-302?\u-318?\u-338?\u-342?\u-345?\u-368?
\u-1131?\u-1132?\u-1135?\u-1234?\u-1231?\u-1228?\par

My question is how do I find the unicode character value for each of
the characters in my CString? My CString is wide, and using
WideCharToMultiByte gives ???? instead of the actual characters, which
is normal as these characters can't be displayed in ANSI on my code
page (1252).

Thanks.

5. Calling a non-Unicode function with CString parameters from Unicode compiled code

6. From CString to int with Unicode - SOLVED

7. From CString to int with Unicode

8. Mixing unicode and ascii with CString

We have a project using CString that is written primarily in unicode.
However there are a significant number of elelmets that are ascii in nature.
For example
CString csFile(_T("test.out"));
FILE *f = fopen(csFile); ....
This works like a charm when it's in MBCS mode, but in UNICODE fopen chokes
on the double byte characters.

Or as another example
Socket::Send (CString...  CString.GetLength()) chokes, because web servers
and our own legacy server is expecting single byte ascii.

Is there a way to tell CString to use/NOT use UNICODE?  Is there a way to
retrieve ascii from CString without incurring the overhead of
USES_CONVERSION and the associated functions in all places (I am assuming
that each time I use it it inlines the apporpriate conversion functions and
that after a hundred or more of those, code bloat will be inevitable)?

Or am I just totally missing something and need to be smacked upside the
head?

Thanks
Peter