Answers:
调用c_str()以从中获取const char *(LPCSTR)std::string。
都是名字:
LPSTR -(长)指向字符串的指针- char *
LPCSTR -(长)指向常量字符串的指针- const char *
LPWSTR -(长)指向Unicode(宽)字符串的指针- wchar_t *
LPCWSTR -指向恒定Unicode(宽)字符串的(长)指针- const wchar_t *
LPTSTR -(长)指向TCHAR的指针(如果定义了UNICODE,则为Unicode,否则为ANSI)字符串- TCHAR *
LPCTSTR -(长)指向常量TCHAR字符串的指针- const TCHAR *
您可以忽略名称的L(长)部分-这是16位Windows的保留。
这些是Microsoft定义的typedef,它们对应于:
LPCSTR:指向空终止的const字符串的指针 char
LPSTR:指向以null结尾的char字符串的指针char(通常会传递一个缓冲区并将其用作“输出”参数)
LPCWSTR:指向以空值终止的const字符串的指针 wchar_t
LPWSTR:指向以NULL结尾的字符串的指针wchar_t(通常会传递一个缓冲区并将其用作“输出”参数)
将a转换std::string为LPCSTR取决于确切的上下文,但是通常调用.c_str()就足够了。
这可行。
void TakesString(LPCSTR param);
void f(const std::string& param)
{
TakesString(param.c_str());
}
请注意,您不应尝试执行此类操作。
LPCSTR GetString()
{
std::string tmp("temporary");
return tmp.c_str();
}
返回的缓冲区.c_str()归std::string实例所有,并且仅在下一次修改或销毁字符串之前才有效。
将a转换std::string为a LPWSTR更复杂。婉婷的LPWSTR暗示,你需要修改的缓冲区,你还需要确保你明白什么字符编码的std::string使用。如果std::string包含使用系统默认编码的字符串(此处为Windows),则可以找到所需的宽字符缓冲区的长度,并使用MultiByteToWideChar(Win32 API函数)执行转码。
例如
void f(const std:string& instr)
{
// Assumes std::string is encoded in the current Windows ANSI codepage
int bufferlen = ::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), NULL, 0);
if (bufferlen == 0)
{
// Something went wrong. Perhaps, check GetLastError() and log.
return;
}
// Allocate new LPWSTR - must deallocate it later
LPWSTR widestr = new WCHAR[bufferlen + 1];
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), widestr, bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// Do something with widestr
delete[] widestr;
}
使用LPWSTR您可以更改其指向的字符串的内容。使用LPCWSTR您无法更改其指向的字符串的内容。
std::string s = SOME_STRING;
// get temporary LPSTR (not really safe)
LPSTR pst = &s[0];
// get temporary LPCSTR (pretty safe)
LPCSTR pcstr = s.c_str();
// convert to std::wstring
std::wstring ws;
ws.assign( s.begin(), s.end() );
// get temporary LPWSTR (not really safe)
LPWSTR pwst = &ws[0];
// get temporary LPCWSTR (pretty safe)
LPCWSTR pcwstr = ws.c_str();
LPWSTR只是指向原始字符串的指针。您不应该使用上面的示例从函数中返回它。为了不临时,LPWSTR您应该在堆上复制原始字符串。检查以下示例:
LPWSTR ConvertToLPWSTR( const std::string& s )
{
LPWSTR ws = new wchar_t[s.size()+1]; // +1 for zero at the end
copy( s.begin(), s.end(), ws );
ws[s.size()] = 0; // zero at the end
return ws;
}
void f()
{
std::string s = SOME_STRING;
LPWSTR ws = ConvertToLPWSTR( s );
// some actions
delete[] ws; // caller responsible for deletion
}
MultiByteToWideChar查尔斯·贝利(Charles Bailey)给出的答案是正确的。因为LPCWSTR这只是typedef的类型const WCHAR*,所以widestr在示例代码中,可以在LPWSTR期望a或LPCWSTR期望a的任何地方使用它。
一个小的调整是使用std::vector<WCHAR>而不是手动管理的数组:
// using vector, buffer is deallocated when function ends
std::vector<WCHAR> widestr(bufferlen + 1);
::MultiByteToWideChar(CP_ACP, 0, instr.c_str(), instr.size(), &widestr[0], bufferlen);
// Ensure wide string is null terminated
widestr[bufferlen] = 0;
// no need to delete; handled by vector
另外,如果您需要使用宽字符串作为开头,则可以使用std::wstring代替std::string。如果要使用Windows TCHAR类型,可以使用std::basic_string<TCHAR>。从转换std::wstring到LPCWSTR或者从std::basic_string<TCHAR>到LPCTSTR只需要调用的事c_str。当您在ANSI和UTF-16字符之间进行切换时MultiByteToWideChar(及其反面WideCharToMultiByte)会出现在图片中。
转换很简单:
std::string myString;
LPCSTR lpMyString = myString.c_str();
这里要注意的一件事是c_str不会返回myString的副本,而只会返回指向std :: string包装的字符串的指针。如果您想要/需要一份副本,则需要使用strcpy自己制作一份。
我认为将a转换std::string为a 的最简单方法LPWSTR是:
std::string为std::vector<wchar_t>wchar_t取向量中第一个的地址。std::vector<wchar_t>有一个模板化的ctor,它将使用两个迭代器,例如std::string.begin()和.end()。但这会将每个字符转换为wchar_t。std::string由于Unicode值类似于Latin-1值的方式,因此仅当包含ASCII或Latin-1 时才有效。如果它包含CP1252或任何其他编码的字符,则更为复杂。然后,您需要转换字符。
std::wstring?