93.5 Basic Steps

To convert code that processes strings to use Unicode, follow these steps:

1.Change all character/string types used for text to TCHAR and LPTSTR or LPTCH.

2.Make 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 types. Declare pointers of indeterminate type explicitly as void pointers by using LPVOID as appropriate.

3.Make pointer arithmetic type-independent: subtracting TCHAR * values yields an answer in bytes if TCHAR evaluates to char, an answer in terms of 16-bit elements, when TCHAR evaluates to WCHAR. The following expression always calculates the number of elements of type TCHAR, whether they are compiled as Unicode or not.

cCount = *lpEnd-*lpStart;

Change the expression to:

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

to make the expression return the size in bytes in a type-independent manner. There is no need to change a statement like the following:

chNext = ++*lpText;

because the pointer increment points to the next character element.

4.Replace literal strings and manifest character constants with macros. Use the TEXT macro, which evaluates to L'\\' when the UNICODE variable is defined, and to '\\' otherwise, so that

while( *lpFileName++ != '\\\\' )...

becomes

while( *lpFilename++ != TEXT('\\\\') )...

Consider moving literal strings into resources, especially if they contain characters outside the ASCII range (that is, 0x00 to 0x7f).

5.Change the code to call the wide generic versions of the run-time library string functions. For example, use the generic Win32 string functions listed in Section 0.10, “String Functions” or use macros like CharStrLen defined below instead of strlen. This generic macro is evaluated to wcslen if the UNICODE variable is #defined, otherwise to strlen.

Note:

The count returned is in number of characters in either case.

6.Change any code that relies implicitly on 255 as the largest value for a character, for example, by using a character value as index into a table of size 256.

7.Compile your code. It should create the same binary as before. Next, set the #define UNICODE variable, and your program will be compiled as a Unicode program.