Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

winapi

lpcstr

First of all, what is it exactly? I guess it is a pointer (LPC means long pointer constant), but what does "W" mean? Is it a specific pointer to a string or a pointer to a specific string? For example I want to close a Window named "TestWindow".

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

The code is illegal and it doesn't work since const char[6] cannot be converted to CONST WCHAR. I don't get it at all. I want to get a clear understanding of all these LPCWSTR, LPCSTR, LPSTR. I tried to find something , however I got confused even more. At msdn site FindWindowEx is declared as

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

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

like image 471
lhj7362 Avatar asked Feb 09 '10 16:02

lhj7362


People also ask

What is an Lpcwstr?

An LPCWSTR is a 32-bit pointer to a constant string of 16-bit Unicode characters, which MAY be null-terminated.

What is Lpwstr C++?

The LPWSTR type is a 32-bit pointer to a string of 16-bit Unicode characters, which MAY be null-terminated. The LPWSTR type specifies a pointer to a sequence of Unicode characters, which MAY be terminated by a null character (usually referred to as "null-terminated Unicode").

What is a Lpstr?

LPSTR stands for "long pointer to string". Back before 32-bit processors, pointers to memory that might be in a different segment of memory (think, a long way away in memory), needed extra space to store. On 32-bit (and later) processors, they're exactly the same thing. Microsoft uses LPSTR solely for historic reasons.


2 Answers

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 = L"TestWindow"; 
like image 77
JaredPar Avatar answered Oct 12 '22 21:10

JaredPar


LPCWSTR is equivalent to wchar_t const *. It's a pointer to a wide character string that won't be modified by the function call.

You can assign to LPCWSTRs by prepending a L to a string literal: LPCWSTR *myStr = L"Hello 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.

like image 31
Matt Joiner Avatar answered Oct 12 '22 22:10

Matt Joiner