ANSI / Unicode applications (Only Visual Project Type)


From version 1.04.0.0001 Easy Code Masm visual projects can built ANSI or Unicode applications in a very easy way. To switch between ANSI and Unicode mode, just check/uncheck the Build project as Unicode (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).

For maintaining compatibilty with all Windows platforms, Unicode applications built in the Easy Code Masm visual mode 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 Build project as Unicode option unchecked) will always be ANSI although they run on Windows NT or later systems. For you to know how an application is running on, Easy Code defines the APP_UNICODE constant ONLY if the the application is running as Unicode. If you prefer, you can 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.

WARNING: All this functionality just applies to visual projects. However, the APP_UNICODE constant can still be used for classics projects by selecting/unselecting this option.


In order to support this new format, the new TextAddrW macro for Unicode text has been included, and from version 1.05.0.0002 there is a new macro, TextStr, which automatically returns text in ANSI or Unicode format, depending on whether the Build project as Unicode (Windows NT or later) option is checked or not. Also, all Easy Code Masm methods using text strings have been improved by adding their corresponding ANSI and Unicode versions. For example, the SetText method has been 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 Masm 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, TextAddrW("Hello") ;Unicode mode
ELSE
    Mov Eax, TextAddr("Hello") ;ANSI mode
ENDIF
Invoke SetText, hWnd, Eax


Also, you can use ANSI strings in Unicode applications:

Invoke SetTextA, hWnd, TextAddr("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
    Invoke SetWindowTextW, hWnd, TextAddrW("Hello") ;Unicode mode
.Else
    Invoke SetWindowTextA, hWnd, TextAddr("Hello") ;ANSI mode
.EndIf


Also, you can write the previous code in the following way:

Invoke IsAppUnicode
.If Eax
    Mov Edx, TextAddrW("Hello") ;Unicode mode
    Mov Eax, SetWindowTextW
.Else
    Mov Edx, TextAddr("Hello") ;ANSI mode
    Mov Eax, SetWindowTextA
.EndIf
Push Edx
Push hWnd
Call Eax


Using the TextStr macro you can even save some lines of code. The following instructions will work both, ANSI or Unicode mode:

Mov Eax, TextStr("Hello")
Invoke SetText, hWnd, Eax


You can also simplify the code by using the MASM32 v11 SDK. If so, you can take advantage of its new __UNICODE__ constant, which makes the compiler to refer to Unicode or ANSI versions depending, respectively, on whether __UNICODE__ is defined or not. Easy Code 1.06.0.0013 and later versions add the new Masm32 Unicode mode option in the Project Properties, which internally takes care of the __UNICODE__ constant. Checking this option will make __UNICODE__ to be defined and Unicode versions to be used. On the other hand, if the Masm32 Unicode mode option is unchecked, __UNICODE__ will not be defined and ANSI versions will be used. 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).

Keep in mind that all Easy Code Masm 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. 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.

Also, remember that only applications built with the Build project as Unicode 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 Build project as Unicode option unchecked will be always ANSI.


WARNING: As said before, all file names and paths related to projects are always managed as ANSI text. When Easy Code runs in Unicode mode (that is, on Windows NT or later) the dialog box for opening and saving files is also a Unicode window. However, once a file is selected, its path and name are always converted to ANSI for compatibility reasons, so please take that into account when selecting folders or naming objects and files for your projects.

REMARKS: Although all names for projects, fonts, windows and controls are always ANSI names, methods retrieving or setting those names (i.e. GetFontName and SetFontName) will expect and return Unicode strings if the application is running as Unicode. On the other hand, when calling the Create method for creating a window, you can pass its name (first parameter) both, with an ANSI or a Unicode string.

IMPORTANT: The code editor ALWAYS works in ANSI mode and it ALWAYS needs an ANSI fixed-pitch font in order to properly work.