Should TCHAR be excluded from Windows code?

I am revising very old (10 years) C code. The code compiles on Unix / Mac with GCC and cross-compilers for Windows with MinGW. There are currently TCHAR strings. I would like to get rid of TCHAR and use C ++ instead. Still need to use Windows features in a wide range, or can I do everything with Unicode and UTF-8?

+7
source share
5 answers

Windows also uses UTF16 and most likely will always be. So you need to use wstring , not string . The Windows APIs do not offer UTF8 support directly, because Windows Unicode was developed before UTF8.

Thus, it is rather difficult to write Unicode code that will compile on Windows and Unix platforms.

+9
source

Is it necessary to use Windows Wide Features, or can I do everything now with Unicode and UTF-8?

Yes. Unfortunately, Windows does not have native support for UTF-8. If you need proper Unicode support, you need to use the wchar_t version of the Windows API functions, not the char version.

Should TCHAR be excluded from Windows code?

Yes you need. The reason for TCHAR is to support both Unicode and non-Unicode versions of Windows. Non-Unicode support may have been a major issue back in 2001, when Windows 98 was still popular, but not today.

And it is unlikely that any Windows-independent library would have the same char / wchar_t overload that uses TCHAR .

So go ahead and replace all your TCHAR with wchar_t s.

The code compiles on Unix / Mac with GCC and cross-compilers for Windows with MinGW.

I used to have to write cross-platform C ++ code. (Now my job is to write cross-platform code in C #.) Character encoding is rather painful when Windows does not support UTF-8 and Un * x does not support UTF-16. In the end, I used UTF-8 as our main encoding and conversion as needed on Windows.

+4
source

Yes, now writing applications other than Unicode shoot in the foot. Just use the wide API everywhere and you won’t have to cry about it later. You can still use UTF8 on UNIX and wchar_t for Windows if you don’t need (network) communication between platforms (or convert wchar_t with Win32 API to UTF-8), or go in a complicated way and use UTF-8 everywhere and convert to wchar_t when you use Win32 API functions (what I do).

0
source

To directly answer your question:

Do I still need to use the Windows features, or can I do everything with Unicode and UTF-8?

No, (not ASCII) UTF-8 is not accepted by the vast majority of the Windows API functions. You still have to use wide APIs.

One might also fear that other OSs still do not support wchar_t . Therefore, you must also support UTF-8.

Other answers give some useful tips on how to manage this in a cross-platform code base, but it sounds like you already have an implementation that supports different types of characters. As desired, as this can lead to code breaking, to simplify the code, do not do this.

0
source

And I predict that someday, although probably not earlier than 2020, Windows will add support for UTF-8 by simply adding the U versions of all the API functions, along with A and W, plus the same hack for the linker. 8-bit A functions are just a translation layer on top of W's own functions (UTF-16). I bet they can generate a U-layer semi-automatically from an A-layer.

Once they were teased enough, long enough, about their support for the 20th century Unicode ...

They will still be embarrassed to write, ugly to read and not carry over by default, using carefully selected macros and Visual Studio default settings.

0
source

All Articles