windows PWSTR 究竟是什么,与 C++ 中的 char*、std::string 或 CString 相比,为什么要使用这种命名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2096762/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:47:22  来源:igfitidea点击:

What exactly is a PWSTR and why use this naming compared to char*, std::string, or CString in C++?

c++windowsstring

提问by Brian T Hannan

In various c++ code you often see different usage of strings: PWSTR, char*, std::string, CString, etc ...

在各种 C++ 代码中,您经常会看到字符串的不同用法:PWSTR、char*、std::string、CString 等……

When is the best time to use PWSTR as compared to any other string type?

与任何其他字符串类型相比,何时是使用 PWSTR 的最佳时间?

采纳答案by John Knoeller

a PWSTR would be a wchar_tstring pointer. That is a UNICODE (usually UCS2) string with each character taking 16 bits.

PWSTR 将是一个wchar_t字符串指针。那是一个 UNICODE(通常是 UCS2)字符串,每个字符占 16 位。

a char*would be a pointer 8 bits per character. this could be ASCII, ANSI, UTF8, or one of many hundreds of other encodings. Although you only need to worry about encodings if you need the string to hold languages other than English or special symbols.

achar*将是每个字符 8 位的指针。这可能是 ASCII、ANSI、UTF8 或数百种其他编码之一。虽然如果您需要字符串来保存英语或特殊符号以外的语言,您只需要担心编码。

In general, The Windows API is all UNICODE internally so most Windows programmers use wchar strings. But std::stringand CStringcan both be UNICODE if the right symbols are #defined, so your choice between PWSTR, std::stringand CStringwill be a matter of preference or the convention of the codebase you work with.

一般来说,Windows API 在内部都是 UNICODE,所以大多数 Windows 程序员使用 wchar 字符串。但是std::stringCString如果正确的符号是#defined,并且 和都可以是 UNICODE ,因此您在PWSTR,std::string和之间的选择CString将取决于您的偏好或您使用的代码库的约定。

回答by GManNickG

When whichever library you are working with wants a PWSTR. This is a, according to naming convention used in Windows, pointer to a string of wide-characters.

当您正在使用的任何图书馆想要一个PWSTR. 根据 Windows 中使用的命名约定,这是一个指向宽字符字符串的指针。

By default, you should use std::string/std::wstring. Only when you interface with someone expecting something else should you change that.

默认情况下,您应该使用std::string/ std::wstring。只有当你与期待别的东西的人接触时,你才应该改变它。

You can get a PCWSTRfrom a std::wstringwith the c_str()method. CStringis MFC, if I recall.

您可以使用该方法PCWSTR从 a 中获取 a 。如果我记得的话,是 MFC。std::wstringc_str()CString

回答by Terry Mahaffey

PWSTR= pointer to a wide string = WCHAR* in windows

PWSTR = 指向宽字符串的指针 = WCHAR* 在 Windows 中

The Windows SDK is very hung up on typedefs for types and pointers to types.

Windows SDK 非常关注类型的 typedef 和指向类型的指针。

"When in Rome" - use whatever string type the project you're working on requires. String types aren't important enough to stress out about or try to force your one true way upon whatever is being used.

“在罗马时” - 使用您正在处理的项目需要的任何字符串类型。字符串类型还不够重要,不足以强调或试图强迫你使用一种真正的方式来使用任何东西。

回答by sbi

PWSTRis an identifier from somewhere in <windows.h>. It's best used when dealing with Windows API function that require such a thing, and best avoided otherwise.

PWSTR是来自<windows.h>. 在处理需要这种东西的 Windows API 函数时最好使用它,否则最好避免。

回答by Daniel A. White

Its from the Windows API.

它来自 Windows API。

I remember it like this.

我记得是这样的。

Its a P-ointer to a W-ide string.

它是一个指向W字符串的P指针。

It would be used in a Unicode application. Its use would be when you are making calls to Windows. The best type would be actually LPTSTRP-ointer to a T-yped string (either Unicode or ANSI.

它将用于 Unicode 应用程序。它的用途是当您调用 Windows 时。最好的类型将是实际上LPTSTRP-ointer到Ť-yped串(Unicode或ANSI。

回答by Sam Holloway

The best time to use PWSTR is if all the code you are editing/extending is already using it. It is usually best to keep coding in the paradigm of whatever code you're working on.

使用 PWSTR 的最佳时间是您正在编辑/扩展的所有代码是否已经在使用它。通常最好按照您正在处理的任何代码的范式进行编码。

Similarly, if you're making heavy use of a particular library that declares its strings in a certain way, it's usually best to keep that type of string in your own code. So working with Windows MFC, you'd use CString. Old-style Win32 would usually be LPCTSTR and similar.

同样,如果您大量使用以某种方式声明其字符串的特定库,通常最好在您自己的代码中保留该类型的字符串。因此,使用 Windows MFC 时,您将使用 CString。旧式 Win32 通常是 LPCTSTR 和类似的。

It's often a matter of style rather than a particular implementation detail.

这通常是风格问题,而不是特定的实现细节。

回答by Nemanja Trifunovic

Take a look at the file WinNT.h in the Windows SDK - you'll see that PWSTR is a typedef for WCHAR*. It is not a macro.

查看 Windows SDK 中的 WinNT.h 文件 - 您会看到 PWSTR 是 WCHAR* 的 typedef。它不是一个宏。