Is there a standard macro to convert a char string literal that is substituted with a preprocessor macro to a wchar_t string using Visual Studio 2005?
e.g. this works:
wchar_t* test = L"Hello World";
but this doesn't:
#define HELLOWORLD "Hello World"
wchar_t* test = L(HELLOWORLD);
I am sharing a header file containing many internationalised strings with several different projects on different platforms, so I don't want to alter the header itself, nor add _T() since this is platform dependent. So I want the unicode conversion to be in my source, not in the header. I know I can write my own substitution macro as shown here, but am wondering if there is a standard method in VS?
This would seem a very common scenario for anyone writing internationalised code, but I can't seem to find a predefined macro delivered with VS2005.
No, there is no "standard" macro for this. The standard is to prefix wide strings with an L
and omit the prefix from narrow strings.
The only reason why such a macro even needs to exist is when you're variously targeting platforms that don't support Unicode. In that case, everything needs to be a narrow string. If you're not dealing with platforms that lack Unicode support, then everything should probably be a wide string all the time.
The _T
and TEXT
macros are provided in the Windows headers for precisely this purpose: maintaining a single code base that can be compiled both for Windows NT, which supports Unicode, and Windows 9x, which lacks Unicode support.
I can't imagine why you would need such a macro if you aren't already including the Windows headers, but if you do, it's pretty simple to write one yourself. Except than you're going to need to know when string literals should be wide strings and when they should be narrow strings. There's no "standard" #define
for this, either. The Windows headers use the UNICODE
pre-processor symbol, but you can't rely on this being defined on other platforms. So now you're back to where you started from.
Why do you think you need a macro for this, again? If you're hardcoding the type of the string as wchar_t*
, then you're always going to want to use a wide character literal, so you always want to use the L
prefix.
When you're using the _T
and/or TEXT
macros from the Windows headers, you're also not hard-coding the type of the string as wchar_t*
. Instead, you're using the TCHAR
macro, which automatically resolves to the appropriate character type depending on the definition of the UNICODE
symbol.