Results 1 to 4 of 4

Thread: Ansi/unicode

  1. #1

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471

    Ansi/unicode

    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
    Bababooey
    Tatatoothy
    Mamamonkey

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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...
    Code:
    #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
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    interesting, thx parksie
    Bababooey
    Tatatoothy
    Mamamonkey

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And if you are too lazy to set both UNICODE and _UNICODE:
    Code:
    #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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width