C++ LPCWSTR 代表什么,应该如何处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2230758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does LPCWSTR stand for and how should it be handled with?
提问by lhj7362
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".
首先,它究竟是什么?我猜它是一个指针(LPC 表示长指针常量),但是“W”是什么意思?它是指向字符串的特定指针还是指向特定字符串的指针?例如,我想关闭一个名为“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
该代码是非法的,它不起作用,因为 const char[6] 不能转换为 CONST WCHAR。我完全不明白。我想清楚地了解所有这些 LPCWSTR、LPCSTR、LPSTR。我试图找到一些东西,但我更加困惑了。在 msdn 站点FindWindowEx
上声明为
HWND FindWindowEx(
HWND hwndParent,
HWND hwndChildAfter,
LPCTSTR lpszClass,
LPCTSTR lpszWindow
);
So the last parameter is LPCSTR, and the compiler demands on LPCWSTR. Please help.
所以最后一个参数是LPCSTR,编译器要求LPCWSTR。请帮忙。
回答by JaredPar
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.=
LPCWSTR
代表“指向常量宽字符串的长指针”。W 代表 Wide 表示字符串存储在一个 2 字节的字符中,而不是普通的char
. 任何必须处理非 ASCII 字符串的 C/C++ 代码都是通用的。=
To get a normal C literal string to assign to a LPCWSTR
, you need to prefix it with L
要获得一个普通的 C 文字字符串来分配给 a LPCWSTR
,你需要在它前面加上 L
LPCWSTR a = L"TestWindow";
回答by Matt Joiner
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.
LPCWSTR
相当于wchar_t const *
。它是一个指向不会被函数调用修改的宽字符串的指针。
You can assign to LPCWSTR
s by prepending a L to a string literal: LPCWSTR *myStr = L"Hello World";
您可以LPCWSTR
通过在字符串文字前添加 L 来分配给s:LPCWSTR *myStr = L"Hello World";
LPCTSTR and any other Ttypes, 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.
LPC TSTR 和任何其他T类型,根据项目的 Unicode 设置采用字符串类型。如果_UNICODE
为您的项目定义了 ,则 T 类型的使用与宽字符形式相同,否则为 Ansi 形式。适当的函数也将被这样调用:FindWindowEx
被定义为FindWindowExA
或FindWindowExW
依赖于这个定义。
回答by Jerry Coffin
It's a long pointer to a constant, wide string (i.e. a string of wide characters).
它是一个指向常量、宽字符串(即宽字符字符串)的长指针。
Since it's a wide string, you want to make your constant look like: L"TestWindow"
. I wouldn't create the intermediate a
either, I'd just pass L"TestWindow"
for the parameter:
因为它是一个广泛的字符串,你想使你不断的样子:L"TestWindow"
。我也不会创建中间体a
,我只是传递L"TestWindow"
参数:
ghTest = FindWindowEx(NULL, NULL, NULL, L"TestWindow");
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:
如果您想学究地正确,“LPCTSTR”是“文本”字符串——Unicode 构建中的宽字符串和 ANSI 构建中的窄字符串,因此您应该使用适当的宏:
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 don't getting it to really work correctly can be quite a bit of extra work for little gain. In this particular case, there's not much extra work, but if you're manipulating strings, there's a whole set of string manipulation macros that resolve to the correct functions.
很少有人关心生成可以为 Unicode 和 ANSI 字符集编译的代码,如果你不能让它真正正常工作,可能会付出相当多的额外工作,但收益甚微。在这种特殊情况下,没有太多额外的工作,但是如果您要操作字符串,则有一整套字符串操作宏可以解析为正确的函数。