What does this typedef mean involving LPWSTR?

3k Views Asked by At

I am trying to find a substitute for LPWSTR for porting a project to gcc.

typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;

What is null terminated ? so would it be safe if I did something like this:

typedef WCHAR   *LPWSTR 
1

There are 1 best solutions below

0
On BEST ANSWER

typedef __nullterminated WCHAR *NWPSTR, *LPWSTR, *PWSTR;

The __nullterminated part is a SAL annotation. SAL is a Microsoft specific technology to annotate function parameters, return values, function behaviors, etc. to help finding bugs and reduce C/C++ code defects using the Visual Studio Code Analysis tool. You can read about SAL here on MSDN:

Using SAL Annotations to Reduce C/C++ Code Defects

WCHAR is defined in the Windows Platform SDK headers basically as a typedef for wchar_t, which is a 16-bit character type in the Microsoft Visual C++ compiler. This is used as a "character unit" for the Unicode UTF-16 encoding, which is the default de facto Unicode encoding of Windows APIs.
Note that other compilers like GNU GCC on Linux consider wchar_t to be a 32-bit character unit (not 16-bit), so you have to pay attention here for the portability of your code.

(Note: Modern versions of Windows support Unicode UTF-16 with the so called surrogate pairs, so you can have a couple of adjacent WCHARs defining a surrogate pair.)

NWPSTR, LPWSTR and PWSTR are all synonyms, defined as pointers to WCHAR, i.e. considering also the __nullterminated SAL annotation, they are pointers to "raw" C-style NUL-terminated Unicode UTF-16 strings.

Basically, this is the Windows Win32 Unicode equivalent of the classical C's char*.

I've been programming Windows in C++ for several years, and I've never met this NWPSTR to be honest :)

The name PWSTR is built using the following elements:

  • P: pointer
  • W: "wide", i.e. WCHAR/wchar_t-based, i.e. Unicode UTF-16
  • STR: string

So, PWSTR means a pointer to a WCHAR/wchar_t string, i.e. a Unicode UTF-16 string, as already stated above.

LPWSTR is just an old name for the same thing; the initial L means "long", and that dates back to a time when there were "long pointers" which could access memory farther than "short" or "near" pointers :) Those days are no more.

And, if you put a C in those names, you have typedefs for the read-only const counterparts, e.g.: PCWSTR or LPCWSTR are basically const wchar_t* NUL-terminated Unicode UTF-16 strings.

You will find PCWSTR and the older equivalent LPCWSTR used a lot in Windows header files and Windows API documentation to represent Unicode UTF-16 "input" (i.e. const) C-style NUL-terminated strings.