添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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

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

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

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.

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

What needs to be defined to change std::string into a container of wchar_t ? Is that even legal with respect to the Standard - I believe that std::string is required to be std::basic_string<char,...> . D.Shawley Jan 19, 2010 at 22:03 @Nemanja: PWSTR is a #define, it's not restricted to windows, it's used by anyone who believes in encoding type information into type names. John Knoeller Jan 20, 2010 at 4:19 @John: PWSTR is strictly speaking a typedef and a part of Windows SDK. Haven't seen it anywhere else. Nemanja Trifunovic Jan 20, 2010 at 12:57

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.

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

You can get a PCWSTR from a std::wstring with the c_str() method. CString is MFC, if I recall.

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

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

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

LPTSTR (as all these T strings is only needed when you want your code to compile with both Unicode and ANSI settings. IME that's rarely needed. (Once you've gone Unicode, why would you need ANSI?) sbi Jan 19, 2010 at 22:31

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.

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.

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

Coming into this very late; there's a somewhat better reason to use PWSTR and related types than there was a decade ago: Microsoft's Source-code Annotation Language . These are markers used in the code to give the compiler lint-like hints as to the intention of the code, which is like GNU __attribute__ or C++ [[attribute]] on mega steroids.

With SAL, you can annotate your code and data with tokens that indicate higher level intentions: this is an input parameter, that's output, this can't be null, you must check the return value of this function, etc.

It's a very, very long list, expanding on the older and much simpler IN , OUT , and INOUT parameter markers.

In the old days, the Windows header files included:

typedef wchar_t WCHAR;
typedef WCHAR *PWCHAR;  // implies pointing to just one wchar
typedef WCHAR *PWSTR;   // implies pointing to a string

Conceptually I understand what they were trying to accomplish, showing intent of how it's to be used, but was so verbose I never used them.

But now, it's:

typedef wchar_t WCHAR;   // same as before
typedef WCHAR *PWCHAR;   // same as before
typedef _Null_terminated WCHAR *PWSTR; // NEW

Here, the _Null_terminated attribute is carried along with the type and is used by their code analysis infrastructure to suss out errors. Using distinct types - pointing to just one character versus pointing to a string - really does help it drill down to intentional behavior.

The headers are full of these annotations: an example from <stdio.h>:

_Check_return_
extern int __cdecl rename(
   _In_z_ char const *_OldFilename,
   _In_z_ char const *_NewFilename);

Both of the parameters, though technically pointing to a single character, tell the compiler that we're in string territory after all.

I'm still not using the PWSTR-flavored types, but if you use them, you get some source checking for free. SAL has found quite a few bugs in my code just this morning.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.