如何在 C++ 中将字符串转换为 LPWSTR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1198672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string to LPWSTR in c++
提问by Cute
Can anyone help in converting stringto LPWSTR
任何人都可以帮助转换string为LPWSTR
string command=obj.getInstallationPath()+"<some string appended>"
Now i wat to pass it as parameter for CreateProcessW(xx,command,x...)
现在我想把它作为参数传递给 CreateProcessW(xx,command,x...)
But createProcessW()accepts only LPWSTRso i need to cast stringto LPWSTR
但createProcessW()只接受LPWSTR所以我需要投射string到LPWSTR
Thanks in Advance
提前致谢
回答by Greg Hewgill
If you have an ANSI string, then have you considered calling CreateProcessAinstead? If there is a specific reason you need to call CreateProcessWthen you will need to convert the string. Try the MultiByteToWideCharfunction.
如果您有一个 ANSI 字符串,那么您是否考虑过调用CreateProcessA?如果有特定原因需要调用,CreateProcessW则需要转换字符串。试试这个MultiByteToWideChar功能。
回答by gbjbaanb
The easiest way to convert an ansi string to a wide (unicode) string is to use the string conversion macros.
将 ansi 字符串转换为宽(unicode)字符串的最简单方法是使用字符串转换宏。
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.
要使用这些,请将 USES_CONVERSION 放在函数的顶部,然后您可以使用 A2W() 之类的宏来非常轻松地执行转换。
eg.
例如。
char* sz = "tadaaa";
CreateProcessW(A2W(sz), ...);
The macros allocate space on the stack, perform the conversion and return the converted string.
宏在堆栈上分配空间,执行转换并返回转换后的字符串。
回答by Cobaia
Another way:
其它的办法:
mbstowcs_s
use with string.c_str(), you can find example hereor here
与 string.c_str() 一起使用,您可以在此处或此处找到示例
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);
}
回答by Domenic
Also, you might want to consider using TCHARthroughout... If I'm correct, the idea would be something like this:
此外,您可能需要考虑使用TCHAR...如果我是正确的,想法将是这样的:
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 CreateProcessinstead of CreateProcessWor CreateProcessA. The idea is that if UNICODEis defined, then TCHARis typedefed to WCHARand CreateProcessis #defined to be CreateProcessW, which accepts a LPWSTR; but if UNICODEis not defined, then TCHARbecomes char, and CreateProcessbecomes CreateProcessA, which accepts a LPSTR. But I might not have the details right here... this stuff seems somewhat needlessly complicated :(.
请注意,我们使用CreateProcess代替CreateProcessW或CreateProcessA。我们的想法是,如果UNICODE被定义,然后TCHAR被typedef编到WCHAR并且CreateProcess被#defined至是CreateProcessW,它接受一个LPWSTR; 但如果UNICODE未定义,则TCHAR变为char,并CreateProcess变为CreateProcessA,它接受一个LPSTR。但我可能没有这里的细节......这些东西似乎有些不必要的复杂:(。
回答by Joel
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())
并像这样包裹你的字符串。s2ws(volume.c_str())
回答by MrHIDEn
#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);

