Using Generic Data Types

If you use generic data types in your code, it can be compiled for Unicode simply by defining UNICODE before the include statements for the header files. To compile the code for ANSI, omit the UNICODE definition.

To convert code that processes strings so it can be compiled for either ANSI or Unicode, follow these steps.

  1. Change all character and string types used for text to TCHAR, LPTSTR, or LPTCH.
  2. Be sure that pointers to nontext data buffers or binary byte arrays are coded with the LPBYTE type and not mistakenly with the LPTSTR or LPTCH type. Declare pointers of indeterminate type explicitly as void pointers by using LPVOID, as appropriate.
  3. Make pointer arithmetic type-independent. Using units of TCHAR size yields variables that are two bytes if UNICODE is defined, and one byte if UNICODE is not defined. Using pointer arithmetic always returns the number of elements pointed to by the pointer, whether the elements are one or two bytes in size. The following expression always returns the number of elements, regardless of whether UNICODE is defined:
    cCount = lpEnd - lpStart; 
     

    The following expression determines the number of bytes used:

    cByteCount = (lpEnd - lpStart) * sizeof(TCHAR); 
     

    There is no need to change a statement like the following one, because the pointer increment points to the next character element.

    chNext = *++lpText; 
     
  4. Replace literal strings and manifest character constants with macros. Change expressions like the following one.
    while(*lpFileName++ != '\\') 
        { 
        // ...
        } 
     

    Use the TEXT macro as follows in this expression.

    while(*lpFileName++ != TEXT('\\')) 
        { 
        // ...
        } 
     

    The TEXT macro causes strings to be evaluated as L"string" when UNICODE is defined, and to "string" otherwise. For easier management, move literal strings into resources, especially if they contain characters outside the ASCII range (that is, 0x00 through 0x7F).

  5. Call only the Unicode versions of the standard C library string functions, as listed in Standard C Functions.
  6. Change any code that relies on 255 as the largest value for a character.

When you compile code that you have changed as outlined above, the compiler creates the same binary file as it did before you made the changes. When you define UNICODE during compilation, however, it is compiled as a Unicode application.