Can someone explain to me these data types WSTR ,LPCWSTR and how to initialize them?

Ioann 61 Reputation points

Can someone explain to me these data types WSTR ,LPCWSTR and how to initialize them?

There is a function that needs a LPCWSTR input.

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 51,016 Reputation points

    WSTR stands for wide string or otherwise known as a Unicode string. But note that generally you'll see LPWSTR as strings are pointers in C++. How you get one depends upon several things. If your code is compiling for ANSI then you'll likely only get this if you call wide string functions. If you need to convert from an ANSI string to a Unicode one you have to use one of the Win32 conversion functions. If you're compiling for Unicode you're already going to be using the LPWSTR types.

    If you have a string literal then you can tell the compiler it is a Unicode string using the L prefix.

       //ANSI or Unicode, depends on compilation  

    If you need to compile for either ANSI or Unicode then you need to use LPTSTR instead so the compiler choses the correct type.

    LP stands for long pointer which is just a pointer in modern processors. Since strings in C++ are character arrays and hence pointers you will either use the standard STL string type (or equivalent) or you'll stick with C-style strings and you'll be using LPSTR, LPTSTR or perhaps LPWSTR for your types. They are mean pointer to a string (Ansi, Either, Unicode).

    The C in the middle means a constant pointer meaning the string cannot be changed. Many functions require an LPCSTR, LPCTSTR or LPCWSTR which means a pointer to a constant (Ansi, either, Unicode) string. The callee won't be able to modify the string. Typically you can just pass a string literal here (which is constant).

       //void Foo ( LPCWSTR name )  
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 83,101 Reputation points
    0 comments No comments