ANSI / Unicode applications


Easy Code visual projects can build ANSI or Unicode applications in a very easy way. To switch between ANSI and Unicode mode, just check/uncheck the This project will run in Unicode mode (Windows NT or later) option in the Project Properties. If that option is checked, the built application will be Unicode, otherwise it will be ANSI (default).

In order to maintain compatibilty with all Windows platforms, 32-bit Unicode applications will dynamically switch to ANSI when running on Windows 95/98/ME. On the other hand, applications not built as Unicode (that is, having the This project will run in Unicode mode option unchecked) will always be ANSI although they run on Windows NT or later systems. For you to know which mode your application is running in, call the IsAppUnicode method, which returns TRUE or FALSE depending on whether the application is running as Unicode or not. That way, you can prepare an application to work fine in all Windows versions. To do so, just take care of using the appropiate functions and messages when they are related to text, and passing the text strings in the right format.

Easy Code also defines the APP_UNICODE constant, ONLY if the application is built as Unicode. This constant is defined at compile time and it is also available for classic projects. However, if the project is running on Windows95/98/ME, it will be running as ANSI even though the APP_UNICODE is defined.

There are some macros to deal with ANSI/Unicode text. TextStrA always returns an ANSI string, TextStrW always returns a Unicode string and TextStr returns an ANSI or Unicode string, depending on whether the This project will run in Unicode mode option is checked or not. Also, all Easy Code methods using text strings have their corresponding ANSI and Unicode versions. For example, the SetText method is complemented with SetTextA and SetTextW. The SetText method automatically calls the ANSI or Unicode version depending on which platform of Windows it is running on and whether the application was built as Unicode or not, while SetTextA and SetTextW always call, respectively, the ANSI and Unicode version of the method (see Easy Code methods). That way you can mix ANSI and Unicode text in the same application. Let's see some sample code (where hWnd is the handle to the object which text is to be set):

IFDEF APP_UNICODE
    Mov Eax/Rax, TextStrW("Hello") ;Unicode mode
ELSE
    Mov Eax/Rax, TextStrA("Hello") ;ANSI mode
ENDIF
Invoke SetText, hWnd, Eax/Rax


Also, you can use ANSI strings in Unicode applications:

Invoke SetTextA, hWnd, TextStrA("Hello")


If you are using API functions, just take care of calling the appropiate function and setting the right format for strings:

Invoke IsAppUnicode
.If Eax/Rax
    Invoke SetWindowTextW, hWnd, TextStrW("Hello") ;Unicode mode
.Else
    Invoke SetWindowTextA, hWnd, TextStrA("Hello") ;ANSI mode
.EndIf


By using the TextStr (or TextStr) macro you can even save some lines of code. The following instruction will work both, ANSI or Unicode mode:

Invoke SetText, hWnd, TextStr("Hello")


For GoAsm projects you may want to use the DSS type (Easy Code defined), which makes a text string to be ANSI, in ANSI projects, or to be a Unicode string in Unicode projects.

szHello DSS 'Hello', 0


Also, Easy Code defines the CHARSIZE constant, which has a value of 1 or 2 depending on whether ANSI or Unicode mode is being used. So, if you need to increase a string pointer by one character, you can use the CHARSIZE constant as follows:

Mov Esi, [lpszString]
Add Esi, CHARSIZE

REMARKS: All lines above has been written in MASM syntax. Please take that into account when working with other assemblers in order to make the necessary syntax changes.



If your using the MASM32 v11 SDK for 32-bit JWASM / MASM / POASM projects (recommended), you can take advantage of its new __UNICODE__ constant, which makes the compiler to refer to Unicode or ANSI versions of the API functions depending, respectively, on whether __UNICODE__ is defined or not. Easy Code and the Masm32 Unicode mode option in the Project Properties, internally take care of the __UNICODE__ constant. Checking this option will make __UNICODE__ to be defined and Unicode versions to be called. On the other hand, if the Masm32 Unicode mode option is unchecked, __UNICODE__ will not be defined and ANSI versions will be called. For example, a call to SetWindowText will result in a call to SetWindowTextA (if Masm32 Unicode mode option is unchecked) or to SetWindowTextW (if Masm32 Unicode mode option is checked).

REMARKS: The Masm32 Unicode mode option only has effect if the MASM32 SDK include files are used (by default, JWASM, MASM and POASM 32-bit projects).



Keep in mind that all Easy Code methods using text strings expect and return Unicode strings if the application has been built as Unicode and it is running on Windows NT or later (unless you specify their ANSI "A" or Unicode "W" version). Otherwise they will expect and return ANSI strings. For example, the GetText method may return an ANSI or Unicode string depending on where and how the application is running on, so use the IsAppUnicode method, or the APP_UNICODE constant, to always know the right format for text strings. If you want to call an ANSI method in a Unicode application or a Unicode method in an ANSI application, call its especific version. For example, GetTextA always returns an ANSI string, while GetTextW always returns a Unicode string.

Also, remember that only applications built with the This project will run in Unicode mode option checked, in the Project Properties, will be Unicode if they are running on Windows NT or later. On the other hand, applications built with the This project will run in Unicode mode option unchecked will be always ANSI.