ANSI and Unicode Application Development

Top  Previous  Next

Since DTWAIN comes in both ANSI and Unicode versions of the DTWAIN DLL, special care must be taken by your application to ensure that the strings being passed or returned by DTWAIN match the default character-set type your application is using.  ANSI uses an 8-bit character type, while Unicode uses a 16-bit character type.

 

For Microsoft Visual C++ and most other Windows-based C++ compilers, the header file <tchar.h> defines macros _T( ) and TEXT( ) to ensure a string literal is of the correct type.  In addition, the character type is denoted by TCHAR.  The TCHAR type will be an alias to wchar_t when building a Unicode-based application, and TCHAR is an alias to char when building ANSI applications.

 

Example:

 

#include <tchar.h>

 

int main( )

{
  TCHAR x[ ] = _T("abc123");     // correct initialization, whether you're applicaiton is ANSI or Unicode

  TCHAR y[ ] =  "abc123";         // Wrong, If build type is Unicode, this is either a warning (if 'C' code) or error (if C++).

  ...

 

Note the usage of the <tchar.h> header file, and the _T( ) macro to declare string literals.

 

If you are using a compiler other than Visual C++, and the compiler doesn't have <tchar.h>,  TCHAR does not exist, or _T( ) string macros are missing, you can create your own version.  Below is an example (assuming that there is a #defined constant called UNICODE or _UNICODE when the build is a Unicode build):

 

Example:

 

#if defined (UNICODE) || defined(_UNICODE)              // this is a Unicode build

   typedef wchar_t  TCHAR

   #define _T(x)  L ## x

#else

   typedef char TCHAR

   #define _T(x)  x             // this is not a UNICODE build

#endif

#define TEXT(x) _T(x)     // defines the TEXT macro

 

int main( )

{
  TCHAR x[ ] = _T("abc123");     // correct initialization.

  TCHAR y[ ] =  "abc123";         // Wrong, If build type is Unicode, this is either a warning (if 'C' code) or error (if C++).

 

 

Note that the #defines listed above will work whether you are using the Unicode-based or ANSI-based versions of DTWAIN,

 

The main point is that you should use TCHAR instead of char or wchar_t to declare string arrays or characters that will be used by DTWAIN, and you should use either the _T( ) or TEXT( ) macros to define string literals and character constants.  Otherwise, when you compile your code, you may get many warnings of string type mismatches, or you will get compiler errors (specifically if you're compiling for C++, and not C).

 

Microsoft Visual Studio and Unicode

ANSI and Unicode versions of DTWAIN functions