-1

I found a very old post, which gives the answer as:

LPCWSTR is a pointer to a const string buffer. LPWSTR is a pointer to a non-const string buffer. Just create a new array of wchar_t and copy the contents of the LPCWSTR to it and use it in the function taking a LPWSTR.

I think I understand the first part - create a new array of wchar_t and copy the contents of the LPCWSTR to it.

wstring wstrSrc(myString.begin(), myString.end());
const int lngth = sizeof(wstrSrc) / sizeof(int);
PCWSTR str2PCWSTR = wstrSrc.c_str();
const wchar_t* filepath[lngth]{ str2PCWSTR };

However, I am not sure how to do the second part - use it in the function taking a LPWSTR.

How do I use the contents of the wchar_t array in a PWSTR?

I tried searching for a clearer answer to the one I found, and tried, or made an effort to understand the suggestion.

I have not gotten any further than the code above.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
CDv
  • 1
  • 1
  • 2
    `const int lngth = sizeof(wstrSrc) / sizeof(int);` makes no sense at all. The size of a `wstring` is constant and has nothing to do with the data contained in it. It has a `size()` member function for a reason. – Retired Ninja Jul 28 '23 at 14:59
  • Please provide a [mre], i.e. some code that is at least complete. At the moment, we don't know what `myString` is. – Thomas Weller Jul 28 '23 at 15:01
  • 1
    You don't. LPWSTR is a pointer to a string, not a string. This is one of those rare cases where you might need `const_cast(wstrSrc.c_str())`. After that forget const_cast exists again. – Pepijn Kramer Jul 28 '23 at 15:01
  • 2
    I'm pretty sure the right answer is just `wstrSrc.data()` – Mooing Duck Jul 28 '23 at 15:08
  • Wherever it is that you want an `LPWSTR` just write `wstrSrc.data()` and the problem is solved. C++ strings and vectors are designed to work where pointers are needed. You are trying too hard and getting yourself very confused in the process. – john Jul 28 '23 at 15:45
  • @RetiredNinja thanks. Do I provide that code, in the original post? – CDv Jul 28 '23 at 18:52

1 Answers1

2

LPWSTR is an alias for wchar_t*. So if you have an array of wchar_t elements, you already have a LPWSTR (remember that arrays decay to pointers to their first element).

As for getting a LPWSTR from a wstring object, just get a pointer to its first element: &wstrSrc[0]. That will be a wchar_t*, which as said above is the same as LPWSTR. With later standards of C++ you can also use wstrSrc.data() to get the pointer.

If you want a pointer to constant string (i.e. LPCWSTR) then use wstrSrc.c_str().


As for other problems sizeof returns the size of the object you pass to it, which in the case of wstrSrc will be the size of a std::wstring (assuming wstring is std::wstring). And the size of std::wstring is not the length of the string, as std::wstring only keeps a pointer to the actual string data.

To get the length in wchar_t elements from the string object use its length function: wstrSrc.length().

Also note that on Windows using the MSVC compiler, sizeof(wchar_t) != sizeof(int). On Windows using MSVC the size of wchar_t is currently 2 while the size of a 32-bit int is 4.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 4
    `wstrSrc.data()` is a more understandable way to get a mutable pointer. – Mooing Duck Jul 28 '23 at 15:08
  • 2
    @MooingDuck only in C++17 and later, though. `&wstrSrc[0]` is guaranteed to work fine in C++11 and C++14. Before C++11, it is not *guaranteed* to work, but *usually does* in any sane implementation. – Remy Lebeau Jul 28 '23 at 16:26
  • My problem was not being able to convert a string to PWSTR, I only am able to convert to PCWSTR, which does not work in LoadBitmapFromFile. @MooingDuck your code solved the problem I was having. I needed the PWSTR to drop in as the source file. Not being able to correctly convert the string to PWSTR, LoadBitmapFromFile was failing. It succeeded when I dropped wstrSrc.data() in there, as source. Thanks – CDv Jul 28 '23 at 20:19