1

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

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
jmasterx
  • 52,639
  • 96
  • 311
  • 557

2 Answers2

8
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");
James McNellis
  • 348,265
  • 75
  • 913
  • 977
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 1
    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 Jun 06 '10 at 20:39
  • @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 Jun 06 '10 at 21:30
6
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
  • 97,037
  • 24
  • 136
  • 212
  • 2
    +1 for `const`-correctness. I hate people disregarding `const`. The ability of the compiler to point out stupid mistakes is severely underrated. – sbi Jun 06 '10 at 20:36