Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which syntax for Unicode strings in VC++?

How should you use unicode strings in VC++? Of course you should to #define UNICODE, but what about your strings?

Should the TEXT() or _T() macro be used around all text or should you just put an L in front of strings? Its my belief that all programs should use unicode these days, so wouldn't it be cleanest to use use the L prefix?

Opinions?

like image 477
Kyle Avatar asked Oct 10 '22 23:10

Kyle


2 Answers

It depends on what you want to achieve. If you want to make sure your code will compile and work correctly both with and without Unicode, use the TEXT or _T macros, and call the "default" Win32 function names (for example CreateWindow).

If you want to make sure your program always uses the Unicode API, then you should use a L prefix in front of your strings, and call the wide versions of Win32 functions (such as CreateWindowW).

In the latter case, you'll get unicode behavior whether or not UNICODE is defined. In the former case, your application will change its behavior based on whether UNICODE is defined.

I agree with you that the non-unicode versions haven't really been relevant since Win98, so I'd go with the second approach.

like image 152
jalf Avatar answered Oct 14 '22 06:10

jalf


Declare Unicode string literals with L prefix.

The TEXT() or _T() macros were for the bad old days when you wanted single source to compile for both Unicode and non-Unicode versions of Windows (Windows 9x). Thankfully you can safely ignore Windows 9x today.

like image 41
David Heffernan Avatar answered Oct 14 '22 07:10

David Heffernan