UNICODE _UNICODE tchar.h L and _T

You must know they are all related to unicode but how to use them?

UNICODE definition is used in Windows SDK source code. If it is defined, functions like RegSetValueEx will be defined as RegSetValueExW, else it is defined as RegSetValueExA.

The definition _UNICODE is used in C/C++ library source code such as in tchar.h so it is at a lower level than UNICODE. If defined, the macro _T(or _TEXT) will be defined as L, if not defined, _T(or _TEXT) will be defined as empty.

L is neither a function nor macro. It is prefixed before a string literal (like L”name”) to tell the compiler(such as g++) to compile the following string(“name”) as WCHARs(a character uses two bytes).

These definitions/macro/prefix are used to control whether to build a unicode version of app or an ansi version of app from a single version of source code. The following is how to use them in a source code to achieve this aim:

//at the beginning of source code

#ifdef UNICODE
#define _UNICODE
#endif
#include <tchar.h>
.....
......
.....
RegSetValueEx(key, _T("name"), 0, REG_SZ, (BYTE*)xx, strlen(xx));

When building with gcc or a framework such as Qt, you only need to use UNICODE instead of _UNICODE like:

g++ -DUNICODE yoursource.cpp

This will build a unicode version of your app.

If you don’t define UNICODE like

g++ yoursource.cpp

It will build an ansi version of your app.

Reference: https://stackoverflow.com/questions/7953025/why-both-unicode-and-unicode

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:

Leave a Reply