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