tags:

views:

122

answers:

2

GDI+ makes use of WCHAR instead of what the WinAPI allows which is CHAR. Usually I can do:

char *str = "C:/x.bmp";

but how do I do this for wchar? I can't juse do

wchar_t *file = "C:/x.bmp";

Thanks

+7  A: 
wchar_t *file = L"C:/x.bmp";

L introduces a wide string.

In Windows, it's customary to use macros that behave differently according to some preprocessor definitions. See http://msdn.microsoft.com/en-us/library/c426s321(VS.71).aspx

You would write:

_TCHAR *file = _TEXT("C:/x.bmp");
Artefacto
Ok perfect! Thanks
Milo
And if GDI+ _demands_ `wchar_t` (mind you, I wouldn't know), what does it help to have those macros? They are only good if you want to _switch between `char` and `wchar_t`_. (Since a Unicode-enabled application can do everything an ASCII-only application does, once you have code that deals with Unicode, why would you every switch to ASCII?)
sbi
@sbi see http://stackoverflow.com/questions/234365/is-tchar-still-relevant Of course, if GDI+ demans a `WCHAR`, he should pass it a `WCHAR`.
Artefacto
+6  A: 
const wchar_t *file = L"C:/x.bmp";

This is according to C++ Standard 2.13.4/1:

<...>A string literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type “array of n const wchar_t” and has static storage duration, where n is the size of the string as defined below, and is initialized with the given characters.

Note that you should use const qualifier here. The effect of attempting to modify a string literal is undefined (2.13.4/2).

Kirill V. Lyadvinsky
+1 for `const`-correctness. I hate people disregarding `const`. The ability of the compiler to point out stupid mistakes is severely underrated.
sbi