jmasterx
jmasterx

Reputation: 54193

Making use of WCHAR as a CHAR?

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

Upvotes: 1

Views: 1521

Answers (2)

Artefacto
Artefacto

Reputation: 97845

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");

Upvotes: 8

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99725

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).

Upvotes: 6

Related Questions