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
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);
}
The easiest way to convert an ansi string to a wide (unicode) string is to use the string conversion macros .
To use these, put USES_CONVERSION at the top of your function, then you can use macros like A2W() to perform the conversion very easily.
eg.
char* sz = "tadaaa";
CreateProcessW(A2W(sz), ...);
The macros allocate space on the stack, perform the conversion and return the converted string.
Also, you might want to consider using TCHAR
throughout... If I'm correct, the idea would be something like this:
typedef std::basic_string<TCHAR> tstring
// Make any methods you control return tstring values. Thus, you could write:
tstring command = obj.getInstallationPath();
CreateProcess(x, command.c_str(), ...);
Note that we use CreateProcess
instead of CreateProcessW
or CreateProcessA
. The idea is that if UNICODE
is defined, then TCHAR
is typedef
ed to WCHAR
and CreateProcess
is #define
d to be CreateProcessW
, which accepts a LPWSTR
; but if UNICODE
is not defined, then TCHAR
becomes char
, and CreateProcess
becomes CreateProcessA
, which accepts a LPSTR
. But I might not have the details right here... this stuff seems somewhat needlessly complicated :(.
You could store it in a CString
and call the LPWSTR operator on it:
const char* sz = "tadaaa";
const CString s( sz );
LPCWSTR ws = static_cast<LPCWSTR>( s ); //calling CString::operator (LPCWSTR)()const;
Note that the WSTR cast operator is only defined for unicode builds.
Here is another option. I've been using this function to do the conversion.
//C++ string to WINDOWS UNICODE string
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
And wrap your string like this. s2ws( volume.c_str())
#include "vcclr.h" //for PtrToStringChars
// Convert function
LPWSTR S2W(String^ string) {
pin_ptr<const wchar_t> wstr = PtrToStringChars(string);
return (LPWSTR)wstr;
}
// Example
String^ s = "10.0.0.1";
LPWSTR psz = S2W(s);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.