LPCWSTR 代表什么以及应该如何处理?

发布于 2024-08-20 10:28:23 字数 589 浏览 5 评论 0原文

首先,它到底是什么?我猜它是一个指针(LPC表示长指针常量),但是“W”是什么意思?是指向字符串的特定指针还是指向特定字符串的指针? 例如我想关闭一个名为“TestWindow”的窗口。

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

该代码是非法的并且不起作用,因为 const char[6] 无法转换为 CONST WCHAR。 我根本不明白。 我想清楚地了解所有这些 LPCWSTR、LPCSTR、LPSTR。我试图找到一些东西,但我更加困惑了。在msdn站点FindWindowEx被声明为

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

所以最后一个参数是LPCSTR,并且编译器要求LPCWSTR。 请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听你说爱我 2024-08-27 10:28:23

LPCWSTR 代表“指向常量宽字符串的长指针”。 W 代表 Wide,表示字符串存储在 2 字节字符中,而不是普通的 char。对于任何必须处理非 ASCII 字符串的 C/C++ 代码来说很常见。

要获取分配给 LPCWSTR 的普通 C 文字字符串,您需要在其前面加上 L 前缀:

LPCWSTR a = L"TestWindow";

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";
尤怨 2024-08-27 10:28:23

LPCWSTR 相当于 const wchar_t *。它是一个指向宽字符串的指针,不会被函数调用修改。

您可以通过在文字前面添加 L 前缀来将字符串文字分配给 LPCWSTR,例如:

LPCWSTR myStr = L"Hello World";

LPC TSTR 和任何其他 T 类型,根据项目的 Unicode 设置采用字符串类型。如果为您的项目定义了 UNICODE,则 T 类型的使用与宽 (Unicode) 字符形式相同,否则与窄 (Ansi) 字符形式相同。相应的 API 函数也将以这种方式调用,例如:

根据此定义,FindWindowEx 被定义为 FindWindowExAFindWindowExW

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

You can assign a string literal to LPCWSTR by prepending the literal with the L prefix, eg:

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 (Unicode) character forms, otherwise the narrow (Ansi) character forms. The appropriate API functions will also be called this way, eg:

FindWindowEx is defined as FindWindowExA or FindWindowExW depending on this definition.

入画浅相思 2024-08-27 10:28:23

它是一个指向常量宽字符串(即宽字符字符串)的长指针。

由于它是一个宽字符串,因此您希望常量看起来像:L"TestWindow"

我也不会创建中间 a ,我只是传递 L"TestWindow" 作为参数:

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

如果你想迂腐地正确,“LPCTSTR”是“文本”字符串——Unicode 构建中的宽字符串和 ANSI 构建中的窄字符串,因此您应该使用适当的宏:

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

尽管很少有人关心生成可以针对 Unicode 和 ANSI 字符集进行编译的代码,并且如果你不这样做,那么让它真正正常工作可能会付出相当多的额外工作,但收效甚微。在这种特殊情况下,没有太多额外的工作,但如果您正在操作字符串,则有一整套字符串操作宏可以解析为正确的函数。

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:

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文