views:

383

answers:

7

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?

+5  A: 

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.

GMan
c_str() cannot work, it is not a PCWSTR.
Hans Passant
@nobugz: I said `const` for a reason :P
GMan
There's no such animal in the zoo of string types.
Hans Passant
@nobugs: Oh, indeed the `const` is misplaced, yes?
GMan
Oh shi- I meant nobugz not nobugs.
GMan
@sbi: His comment was made when my post read: `const PWSTR` instead of `PCWSTR`. The former is `wchar_t* const`, while the later is `const what_t*`. Though maybe I'm reading his comment wrong.
GMan
A: 

Its from the Windows API.

I remember it like this.

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

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 LPTSTR P-ointer to a T-yped string (either Unicode or ANSI.

Daniel A. White
`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
+1  A: 

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.

Sam
+1  A: 

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.

Terry Mahaffey
+1  A: 

PWSTR is 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.

sbi
A: 

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.

John Knoeller
On Windows, wchar_t is always UTF-16.
Nemanja Trifunovic
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
@Nemanja: True, but he didn't say he was on Windows.
John Knoeller
@D.Shawley: it would be std:wstring if you want to be pedantic about it.
John Knoeller
@John: PWSTR is Windows-only
Nemanja Trifunovic
@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
@John: PWSTR is strictly speaking a typedef and a part of Windows SDK. Haven't seen it anywhere else.
Nemanja Trifunovic
@Nemanja. Wrong. On windows wchar_t is always 16 bits. This doesn't guarantee UTF16. You can quite happily use a wchar_t* to hold utf32 where 2 wchar_t's are each symbol. Perhaps you meant that in the windows api wchar_t is always utf16?
KitsuneYMG
@kts: That's exactly what I mean - as far as Windows API is concerned, wchar_t* is UTF-16 only. For your own purposes, of course you can put anything you want into wchar_t.
Nemanja Trifunovic
A: 

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.

Nemanja Trifunovic