PDA

Click to See Complete Forum and Search --> : Ansi/unicode


noble
Jan 28th, 2002, 06:04 AM
If I want my application's character set to be neutral (as opposed
to strictly ANSI or UNICODE), I'm aware of the declaration of
TCHARs instead of CHARs and using functions such as _tcscat
instead of strcat, what else would I have to do for the
preprocessor exactly to be able to switch between an ANSI
and a UNICODE build?

Thanks

parksie
Jan 28th, 2002, 12:24 PM
Somewhere in the compiler options (Project Settings->Preprocessor in VC++) you need to remove MBCS and put UNICODE and _UNICODE (both of them, note the second has the preceding underscore).

Here's some little bits of code that might be useful if you use the STL under Windows at all...#ifdef UNICODE
typedef wstring tstring;
#else
typedef string tstring;
#endif

string WideToANSI(wstring sIn) {
int iReq = WideCharToMultiByte(CP_ACP, 0, sIn.c_str(), -1, NULL, 0, NULL, NULL);
char *pcBuf = new char[iReq];
WideCharToMultiByte(CP_ACP, 0, sIn.c_str(), -1, pcBuf, iReq, NULL, NULL);
string sTemp = pcBuf;
delete[] pcBuf;
return sTemp;
}

wstring ANSIToWide(string sIn) {
int iReq = MultiByteToWideChar(CP_ACP, 0, sIn.c_str(), -1, NULL, 0);
wchar_t *pcBuf = new wchar_t[iReq];
MultiByteToWideChar(CP_ACP, 0, sIn.c_str(), -1, pcBuf, iReq);
wstring sTemp = pcBuf;
delete[] pcBuf;
return sTemp;
}

tstring EnsureTStr(string sIn) {
#ifndef UNICODE
return sIn;
#else
return ANSIToWide(sIn);
#endif
}

tstring EnsureTStr(wstring sIn) {
#ifdef UNICODE
return sIn;
#else
return WideToANSI(sIn);
#endif
}

noble
Jan 28th, 2002, 12:56 PM
interesting, thx parksie

CornedBee
Jan 28th, 2002, 01:03 PM
And if you are too lazy to set both UNICODE and _UNICODE:

#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE
#endif
#endif
#ifdef _UNICODE
#ifndef UNICODE
#define UNICODE
#endif
#endif

Write that before you inclue ANY headers.

Also make sure that you use _tmain/_tWinMain. To make such apps working you should also write this:
#ifdef UNICODE
// this if you are writing a console app
#pragma comment(linker, "/entry:\"wmainCRTStartup\"")
// this if you are writing a GUI app
#pragma comment(linker, "/entry:\"wWinMainCRTStartup\"")
#else
// this if you are writing a console app
#pragma comment(linker, "/entry:\"mainCRTStartup\"")
// this if you are writing a GUI app
#pragma comment(linker, "/entry:\"WinMainCRTStartup\"")
#endif