What does LPCWSTR mean and how should it be managed?

First of all, what is it? I assume this is a pointer (LPC stands for constant constant pointer), but what does β€œW” mean? Is this a specific pointer to a string or a pointer to a specific string? For example, I want to close a window called "TestWindow".

HWND g_hTest; LPCWSTR a; *a = ("TestWindow"); g_hTest = FindWindowEx(NULL, NULL, NULL, a); DestroyWindow(g_hTest); 

The code is illegal and does not work, because const char [6] cannot be converted to CONST WCHAR. I do not understand this. I want to get a clear view of all these LPCWSTR, LPCSTR, LPSTR. I tried to find something, but I was even more embarrassed. On msdn website FindWindowEx declared as

 HWND FindWindowEx( HWND hwndParent, HWND hwndChildAfter, LPCTSTR lpszClass, LPCTSTR lpszWindow ); 

So the last parameter is LPCSTR, and the compiler requires LPCWSTR. Please, help.

+62
c ++ winapi lpcstr
Feb 09 2018-10-09
source share
3 answers

LPCWSTR means "long pointer to constant wide line." W stands for Wide and means the string is stored in a 2-byte character versus the regular char . Common to any C / C ++ code that should deal only with non-ASCII strings. =

To get the standard C string string assigned by LPCWSTR , you need to prefix it with L

 LPCWSTR a = L"TestWindow"; 
+94
Feb 09 '10 at 16:45
source share

This is a long pointer to a constant, a wide string (i.e. a string with wide characters).

Since this is a wide line, you want your constant to look like this: L"TestWindow" . I would also not create an intermediate a , I would just pass L"TestWindow" for the parameter:

 ghTest = FindWindowEx(NULL, NULL, NULL, L"TestWindow"); 

If you want to be pedantically correct, β€œLPCTSTR” is a β€œtext” string β€” a wide string in the Unicode assembly and a narrow string in the ANSI assembly, so you should use the appropriate macro:

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

Few people care about creating code that can compile for both Unicode character sets and ANSI, and if you don't get it really right, there can be quite a bit of extra work for a small gain. In this particular case, there is not much extra work, but if you are manipulating strings, there is a whole set of string manipulation macros that allow the correct functions.

+5
Feb 09 2018-10-09
source share

LPCWSTR equivalent to wchar_t const * . This is a pointer to a wide string of characters that will not be changed by a function call.

You can assign LPCWSTR by adding L to the string literal: LPCWSTR *myStr = L"Hello World";

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

+5
Feb 09 '10 at 17:00
source share



All Articles