c++ – What does LPCWSTR stand for and how should it be handled with?

c++ – What does LPCWSTR stand for and how should it be handled with?

LPCWSTR stands for Long Pointer to Constant Wide String. The W stands for Wide and means that the string is stored in a 2 byte character vs. the normal char. Common for any C/C++ code that has to deal with non-ASCII only strings.=

To get a normal C literal string to assign to a LPCWSTR, you need to prefix it with L

LPCWSTR a = LTestWindow;

LPCWSTR is equivalent to wchar_t const *. Its a pointer to a wide character string that wont be modified by the function call.

You can assign to LPCWSTRs by prepending a L to a string literal: LPCWSTR *myStr = LHello World;

LPCTSTR and any other T types, take a string type depending on the Unicode settings for your project. If _UNICODE is defined for your project, the use of T types is the same as the wide character forms, otherwise the Ansi forms. The appropriate function will also be called this way: FindWindowEx is defined as FindWindowExA or FindWindowExW depending on this definition.

c++ – What does LPCWSTR stand for and how should it be handled with?

Its a long pointer to a constant, wide string (i.e. a string of wide characters).

Since its a wide string, you want to make your constant look like: LTestWindow. I wouldnt create the intermediate a either, Id just pass LTestWindow for the parameter:

ghTest = FindWindowEx(NULL, NULL, NULL, LTestWindow);

If you want to be pedantically correct, an LPCTSTR is a text string — a wide string in a Unicode build and a narrow string in an ANSI build, so you should use the appropriate macro:

ghTest = FindWindow(NULL, NULL, NULL, _T(TestWindow));

Few people care about producing code that can compile for both Unicode and ANSI character sets though, and if you dont getting it to really work correctly can be quite a bit of extra work for little gain. In this particular case, theres not much extra work, but if youre manipulating strings, theres a whole set of string manipulation macros that resolve to the correct functions.

Leave a Reply

Your email address will not be published.