2

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 
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
MistyD
  • 16,373
  • 40
  • 138
  • 240
  • see: http://stackoverflow.com/questions/11648246/compiler-error-in-dev-c . The accepted answer talks about those symbols a bit. – aruisdante Jan 28 '15 at 23:10
  • `__nullterminated` looks like a compiler-specific extension. In any case, `LPWSTR` is simply a pointer to a wide character (with the intent being that that character is the first in an array of characters, ending with a null character, thus forming a string). Your typedef is fine (though it's missing the semicolon). – Cameron Jan 28 '15 at 23:11
  • so `typedef WCHAR *LPWSTR ` can be used as a substitue ? – MistyD Jan 28 '15 at 23:11
  • @Misty: Yes. You read faster than I could ninja-edit my comment :-) If you're using GCC on Windows, though, there should already be a header somewhere that defines these types. (e.g. MinGW has its own version of `windows.h`.) – Cameron Jan 28 '15 at 23:12
  • I am using cygwin-gcc – MistyD Jan 28 '15 at 23:15
  • It is just a do-nothing macro, not a keyword, code analyzers can use it. – Hans Passant Jan 28 '15 at 23:29

1 Answers1

7

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.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162