1

I am calling a C++ method which returns LPWSTR (the string contains a filepath), but I need to convert this to a LPSTR to continue.

I never used C++ before and even though the different string types are explained in detail here: http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc, I got confused.

When I cast LPWSTR to LPTSTR (which should equal LPSTR in my app), I get exactly one Unicode Symbol. While I guess that casting isn't appropriate in this case, I don't understand why it returns only one character instead of interpreting all the 2-byte-characters of the LPWSTR somehow as 1-byte-characters. I found references to the WideCharToMultiByte function, which apparently can convert LPWSTR to std::string, which I in turn can not cast to LPSTR either.

So is there an somewhat easy way to obtain an LPSTR from the LPWSTR?

the_mandrill
  • 29,792
  • 6
  • 64
  • 93
H W
  • 2,556
  • 3
  • 21
  • 45
  • You don't need to cast, you need to make conversion. http://msdn.microsoft.com/en-us/library/87zae4a3.aspx For your case, use CW2A – Alex F Aug 28 '14 at 13:32
  • 2
    See [this question](http://stackoverflow.com/questions/13447756/issue-in-converting-wchar-t-to-char) for sample conversion code. (The person has a question about the conversion result, but you should focus on the conversion itself.) – Raymond Chen Aug 28 '14 at 13:41
  • I tried assigning CW2A pszA(lpwstr) to my LPSTR variable, which works fine. However it doesn't compile since i try to return this variable as LPSTR at the end of my method. Can i return a value that i assigned, using CW2A? – H W Aug 28 '14 at 13:44
  • be forewarned that unless you ensure that your narrow string is unicode (UTF-8) you could corrupt the contents by converting it. – Mgetz Aug 28 '14 at 14:08
  • CW2A creates a local variable. To return it from the function, you need to assign it to some other variable, for example, std::string, and make your function return type std::string. This is not related to conversion question. Or copy the string to caller-allocated buffer like `YourFunction(LPSTR buffer, size_t size);` – Alex F Aug 28 '14 at 14:12

1 Answers1

1

Casting a pointer to another type of pointer will not alter or convert what the original pointer points at.

You should be able to call c_str() on a std::string, and cast that to LPSTR, I think. So, use WideCharToMultiByte (if it does indeed, as you say, convert to a std::string) and call c_str on the result. This will give you a constant string pointer (LPCSTR). You can copy it (eg via strdup) and cast the result to LPSTR.

The returned pointer will then refer to a character string allocated using strdup, so should be passed to free at some point when the string is no longer needed.

The WideCharToMultiByte function that I am familiar with from the Windows API (see documentation) converts directly from a wide-character string to a multi-byte encoded string without going via std::string, although it still requires allocation of a buffer to hold the output string. You may be able to use it instead.

davmac
  • 20,150
  • 1
  • 40
  • 68
  • It tells me that a value of type "const char *" cannot be assigned to an entity of type "LPSTR", if i try this. – H W Aug 28 '14 at 13:39
  • hm, this does compile and doesn't produce any Errors, but i still get only one character as Output (a pipe | for most paths that i enter). Why could i cast the copy produced by _strdup to LPSTR, when i couldn't cast the original string? – H W Aug 28 '14 at 14:13
  • Because the `c_str` method retuns a `const`-qualified pointer, you cannot cast it directly to a non-`const` qualified pointer (which a `LPSTR` is). Creating a modifiable duplicate using `strdup` yields a non-`const` qualified pointer. Don't forget that you must `free` the duplicate at some point, i.e. you are responsible for memory management. – davmac Aug 28 '14 at 16:04
  • " but i still get only one character as Output" - how exactly are you observing this output? Is it possible you're displaying the pointer, and the not the character string that it points to? – davmac Aug 28 '14 at 16:06