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
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.
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.
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.
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").
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With