Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to LPWSTR in c++

Tags:

c++

visual-c++

Can anyone help in converting string to LPWSTR

string command=obj.getInstallationPath()+"<some string appended>"  

Now i wat to pass it as parameter for CreateProcessW(xx,command,x...)

But createProcessW() accepts only LPWSTR so i need to cast string to LPWSTR

Thanks in Advance

like image 450
Cute Avatar asked Jul 29 '09 07:07

Cute


People also ask

How do you convert a string to Lpwstr?

Converting a std::string to LPCWSTR in C++ (Unicode) Converting a string to LPCWSTR is a two-step process. Step1: First step is to convert the initialized object of the String class into a wstring. std::wstring is used for wide-character/Unicode (UTF-16) strings.

How do you convert Lpcwstr to Lpwstr?

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.

How do you convert Wstring to Lpcstr?

To convert std::wstring to wide character array type string, we can use the function called c_str() to make it C like string and point to wide character string.

What is Lpwstr?

The LPWSTR type is a 32-bit pointer to a string of 16-bit Unicode characters, which MAY be null-terminated. The LPWSTR type specifies a pointer to a sequence of Unicode characters, which MAY be terminated by a null character (usually referred to as "null-terminated Unicode").


2 Answers

If you have an ANSI string, then have you considered calling CreateProcessA instead? If there is a specific reason you need to call CreateProcessW then you will need to convert the string. Try the MultiByteToWideChar function.

like image 193
Greg Hewgill Avatar answered Oct 15 '22 22:10

Greg Hewgill


Another way:

mbstowcs_s

use with string.c_str(), you can find example here or here

OR

USES_CONVERSION_EX;

std::string text = "text";
LPWSTR lp = A2W_EX(text.c_str(), text.length());

OR

{
    std::string str = "String to LPWSTR";
    BSTR b = _com_util::ConvertStringToBSTR(str.c_str());
    LPWSTR lp = b;
    Use lp before SysFreeString...
    SysFreeString(b);
}
like image 21
Cobaia Avatar answered Oct 16 '22 00:10

Cobaia