Re: Unicode and ANSI strings in the same project

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"Toto" <toto@xxxxxxxx> wrote in message news:uSb2HqDVIHA.2000@xxxxxxxxxxxxxxxxxxxxxxx

Is it possible to use TCHAR with Unicode strings (wchar_t *) in File1.cpp, and TCHAR with ANSI strings (char *) in File2.cpp, in the same project ? How should I do ?

TCHAR will be char or wchar_t depending on project definitions. In my experience it is best to have this defined on a solution-wide basis. If you don't there will be all sorts of problems passing (eg) CStrings, TCHAR and TCHAR *, variables from a function in one file to one in another. Having a type TCHAR depending on which file it is in is a recipe for disaster.

When I converted my (rather large) program to unicode I did it as follows:

I changed most of my char variables to TCHAR and all the associated stuff like strlen() to _tcslen().

In some places I used WCHAR explicitly ( and wcslen() etc) as I changed my document file format to contain unicode strings before I changed the program to use unicode everywhere.

Where I wanted a char to remain a char, I changed char to CHAR. In this way any remaining instances of char signalled something I'd overlooked.

I used CString, CStringW and CStringA in different places where appropriate.

I did this bit by bit at my leisure. Then (about 8 months after I started) I defined UNICODE and _UNICODE in the project(s). Basically it worked. Since then I have found one or two subtleties where it wasn't immediately 100% correct, but they haven't been too hard to fix.

[The latest, a week or two ago, was a WM_COPYDATA message where an old non-unicode version of my program was sending it to the new one. The old one sent a single byte string, the new one expected unicode. But it was easy enough to fix.]

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm








.



Relevant Pages

  • Re: Window types
    ... typedef WCHAR TCHAR, * PTCHAR; ... typedef char TCHAR, * PTCHAR; ... Under a Unicode build, ... If the only text your app needs to deal with can be handled with ASCII ...
    (microsoft.public.vc.language)
  • Re: VC2005 Pro: IDE (Compiler ?) cant find Stdafx.h
    ... For new code I would obviously use UNICODE but for existing stuff ... > Use Multi-Byte Character Set ... > If you use TCHAR etc., and want your code to run on another platform, then ... > typedef char TCHAR; ...
    (microsoft.public.vc.language)
  • Re: passing char * to dll
    ... char is a single byte thing so there are only 256 different possible values. ... This makes it difficult to have fonts of more than 256 characters. ... Unicode is a way of having all possible characters (latin, chinese, arabic, ... The idea was that you write your code using TCHAR everywhere and TCHAR is ...
    (microsoft.public.vc.mfc)
  • Re: passing char * to dll
    ... char is a single byte thing so there are only 256 different possible values. ... This makes it difficult to have fonts of more than 256 characters. ... Unicode is a way of having all possible characters in a single set. ... To make the transition as painless as possible Microsoft invented the transitional type TCHAR. ...
    (microsoft.public.vc.mfc)
  • Re: When CString doesnt work
    ... because I would never code something that could not be put into Unicode unless ... >It's tempting to use CStrings throughout the back-end in order to gain ... compiler for their embedded work; they can't afford gcc because they need the fancy ... long as it's small and fast" and, of course, "it only compiles on our platform with our ...
    (microsoft.public.vc.mfc)