[英]How to convert std::string to LPCSTR?
How can I convert a std::string
to LPCSTR
? 如何将
std::string
转换为LPCSTR
? Also, how can I convert a std::string
to LPWSTR
? 另外,如何将
std::string
转换为LPWSTR
?
I am totally confused with these LPCSTR
LPSTR
LPWSTR
and LPCWSTR
. 我对这些
LPCSTR
LPSTR
LPWSTR
和LPCWSTR
完全感到困惑。
Are LPWSTR
and LPCWSTR
the same? LPWSTR
和LPCWSTR
是否相同?
Call c_str()
to get a const char *
( LPCSTR
) from a std::string
. 调用
c_str()
从std::string
获取const char *
( LPCSTR
)。
It's all in the name: 都是名字:
LPSTR
- (long) pointer to string - char *
LPSTR
指向字符串的(长LPSTR
)指针char *
LPCSTR
- (long) pointer to constant string - const char *
LPCSTR
指向常量字符串的(长LPCSTR
)指针const char *
LPWSTR
- (long) pointer to Unicode (wide) string - wchar_t *
LPWSTR
指向Unicode(宽)字符串的(长)指针LPWSTR
wchar_t *
LPCWSTR
- (long) pointer to constant Unicode (wide) string - const wchar_t *
LPCWSTR
指向恒定Unicode(宽)字符串的(长)指针const wchar_t *
LPTSTR
- (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
LPTSTR
指向TCHAR的(长)指针(如果定义了UNICODE,则为Unicode,否则为ANSI)字符串TCHAR *
LPCTSTR
- (long) pointer to constant TCHAR string - const TCHAR *
LPCTSTR
(长整数)指向常量TCHAR字符串的指针-const const TCHAR *
You can ignore the L (long) part of the names -- it's a holdover from 16-bit Windows. 您可以忽略名称的L(长)部分-这是16位Windows的保留。
str.c_str()
gives you a const char *
, which is an LPCSTR
(Long Pointer to Constant STRing) -- means that it's a pointer to a 0
terminated string of characters. str.c_str()
为您提供const char *
,它是LPCSTR
(常量STRing的长指针)-表示它是一个以0
结尾的字符串的指针。 W
means wide string (composed of wchar_t
instead of char
). W
表示宽字符串(由wchar_t
代替char
)。
These are Microsoft defined typedefs which correspond to: 这些是Microsoft定义的typedef,它们对应于:
LPCSTR: pointer to null terminated const string of char
LPCSTR:指向以null结尾的
char
字符串的指针
LPSTR: pointer to null terminated char string of char
(often a buffer is passed and used as an 'output' param) LPSTR:指针的空终止字符串
char
(通常是一个缓冲器被传递并用作“输出” PARAM)
LPCWSTR: pointer to null terminated string of const wchar_t
LPCWSTR:指向const
wchar_t
空终止字符串的指针
LPWSTR: pointer to null terminated string of wchar_t
(often a buffer is passed and used as an 'output' param) LPWSTR:指向
wchar_t
空终止字符串的指针(通常会传递一个缓冲区并将其用作“输出”参数)
To "convert" a std::string
to a LPCSTR depends on the exact context but usually calling .c_str()
is sufficient. 将
std::string
转换为LPCSTR取决于确切的上下文,但是通常调用.c_str()
就足够了。
This works. 这可行。
void TakesString(LPCSTR param);
void f(const std::string& param)
{
TakesString(param.c_str());
}
Note that you shouldn't attempt to do something like this. 请注意,您不应尝试执行此类操作。
LPCSTR GetString()
{
std::string tmp("temporary");
return tmp.c_str();
}
The buffer returned by .c_str()
is owned by the std::string
instance and will only be valid until the string is next modified or destroyed. .c_str()
返回的缓冲区归std::string
实例拥有,并且仅在下一次修改或销毁该字符串之前才有效。
To convert a std::string
to a LPWSTR
is more complicated. 将
std::string
转换为LPWSTR
更复杂。 Wanting an LPWSTR
implies that you need a modifiable buffer and you also need to be sure that you understand what character encoding the std::string
is using. 想要
LPWSTR
意味着您需要一个可修改的缓冲区,并且还需要确保您了解使用std::string
编码的std::string
。 If the std::string
contains a string using the system default encoding (assuming windows, here), then you can find the length of the required wide character buffer and perform the transcoding using MultiByteToWideChar
(a Win32 API function). 如果
std::string
包含使用系统默认编码的字符串(假定为Windows,在此处),则可以找到所需的宽字符缓冲区的长度,并使用MultiByteToWideChar
(Win32 API函数)执行转码。
eg 例如
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;
}
Using LPWSTR
you could change contents of string where it points to. 使用
LPWSTR
您可以更改它指向的字符串的内容。 Using LPCWSTR
you couldn't change contents of string where it points to. 使用
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
is just a pointer to original string. LPWSTR
只是指向原始字符串的指针。 You shouldn't return it from function using the sample above. 您不应该使用上面的示例从函数中返回它。 To get not temporary
LPWSTR
you should made a copy of original string on the heap. 要获得非临时的
LPWSTR
,应在堆上复制原始字符串。 Check the sample below: 检查以下示例:
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
}
The MultiByteToWideChar
answer that Charles Bailey gave is the correct one. Charles Bailey给出的
MultiByteToWideChar
答案是正确的。 Because LPCWSTR
is just a typedef for const WCHAR*
, widestr
in the example code there can be used wherever a LPWSTR
is expected or where a LPCWSTR
is expected. 由于
LPCWSTR
是只是一个typedef const WCHAR*
, widestr
在示例代码中有可用于任何一个LPWSTR
预期或者一个LPCWSTR
预期。
One minor tweak would be to use std::vector<WCHAR>
instead of a manually managed array: 一个较小的调整是使用
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
Also, if you need to work with wide strings to start with, you can use std::wstring
instead of std::string
. 另外,如果您需要使用宽字符串开头,则可以使用
std::wstring
代替std::string
。 If you want to work with the Windows TCHAR
type, you can use std::basic_string<TCHAR>
. 如果要使用Windows
TCHAR
类型,可以使用std::basic_string<TCHAR>
。 Converting from std::wstring
to LPCWSTR
or from std::basic_string<TCHAR>
to LPCTSTR
is just a matter of calling c_str
. 从
std::wstring
转换为LPCWSTR
或从std::basic_string<TCHAR>
为LPCTSTR
只需调用c_str
。 It's when you're changing between ANSI and UTF-16 characters that MultiByteToWideChar
(and its inverse WideCharToMultiByte
) comes into the picture. 当您在ANSI和UTF-16字符之间进行切换时,就会出现
MultiByteToWideChar
(及其反向的WideCharToMultiByte
)。
Converting is simple: 转换很简单:
std::string myString;
LPCSTR lpMyString = myString.c_str();
One thing to be careful of here is that c_str does not return a copy of myString, but just a pointer to the character string that std::string wraps. 这里要注意的一件事是c_str不会返回myString的副本,而只会返回指向std :: string包装的字符串的指针。 If you want/need a copy you'll need to make one yourself using strcpy.
如果您想要/需要一份副本,则需要使用strcpy自己制作一份。
The conversion is simple: 转换很简单:
std::string str; std :: string str; LPCSTR lpcstr = str.c_str();
LPCSTR lpcstr = str.c_str();
The easiest way to convert a std::string
to a LPWSTR
is in my opinion: 我认为将
std::string
转换为LPWSTR
的最简单方法是:
std::string
to a std::vector<wchar_t>
std::string
转换为std::vector<wchar_t>
wchar_t
in the vector. wchar_t
的地址。 std::vector<wchar_t>
has a templated ctor which will take two iterators, such as the std::string.begin()
and .end()
iterators. std::vector<wchar_t>
具有模板化ctor,它将使用两个迭代器,例如std::string.begin()
和.end()
迭代器。 This will convert each char to a wchar_t
, though. 不过,这会将每个char转换为
wchar_t
。 That's only valid if the std::string
contains ASCII or Latin-1, due to the way Unicode values resemble Latin-1 values. 这仅在
std::string
包含ASCII或Latin-1时才有效,这是由于Unicode值类似于Latin-1值的方式。 If it contains CP1252 or characters from any other encoding, it's more complicated. 如果它包含CP1252或任何其他编码的字符,则更为复杂。 You'll then need to convert the characters.
然后,您需要转换字符。
std::string myString("SomeValue");
LPSTR lpSTR = const_cast<char*>(myString.c_str());
myString is the input string and lpSTR is it's LPSTR equivalent. myString是输入字符串, lpSTR是等效的LPSTR 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.