How to convert std::string to LPCSTR?
Solution 1
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. W
means wide string (composed of wchar_t
instead of char
).
Solution 2
Call c_str()
to get a const char *
(LPCSTR
) from a std::string
.
It's all in the name:
LPSTR
- (long) pointer to string - char *
LPCSTR
- (long) pointer to constant string - const char *
LPWSTR
- (long) pointer to Unicode (wide) string - wchar_t *
LPCWSTR
- (long) pointer to constant Unicode (wide) string - const wchar_t *
LPTSTR
- (long) pointer to TCHAR (Unicode if UNICODE is defined, ANSI if not) string - TCHAR *
LPCTSTR
- (long) pointer to constant TCHAR string - const TCHAR *
You can ignore the L (long) part of the names -- it's a holdover from 16-bit Windows.
Solution 3
These are Microsoft defined typedefs which correspond to:
LPCSTR: pointer to null terminated const string of char
LPSTR: pointer to null terminated char string of char
(often a buffer is passed and used as an 'output' param)
LPCWSTR: pointer to null terminated string of const wchar_t
LPWSTR: pointer to null terminated string of wchar_t
(often a buffer is passed and used as an 'output' param)
To "convert" a std::string
to a LPCSTR depends on the exact context but usually calling .c_str()
is sufficient.
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.
To convert a std::string
to a LPWSTR
is more complicated. 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. 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).
e.g.
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;
}
Solution 4
Using LPWSTR
you could change contents of string where it points to. Using LPCWSTR
you couldn't change contents of string where it points to.
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. 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. 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
}
Solution 5
The MultiByteToWideChar
answer that Charles Bailey gave is the correct one. 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.
One minor tweak would be to use std::vector<WCHAR>
instead of a manually managed array:
// 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
. If you want to work with the Windows TCHAR
type, you can use 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
. It's when you're changing between ANSI and UTF-16 characters that MultiByteToWideChar
(and its inverse WideCharToMultiByte
) comes into the picture.
Cute
Updated on July 08, 2022Comments
-
Cute almost 2 years
How can I convert a
std::string
toLPCSTR
? Also, how can I convert astd::string
toLPWSTR
?I am totally confused with these
LPCSTR
LPSTR
LPWSTR
andLPCWSTR
.Are
LPWSTR
andLPCWSTR
the same? -
Joel almost 15 yearsMinor picky point: on x64 LPCSTR would be a 64-bit pointer to a (constant) null-terminated string.
-
Timmmm about 6 yearsWhy not use
std::wstring
? -
MSalters about 6 years@Timmmm: C++11 tightened the specification as implementations didn't take advantage of the old rules, today that would be OK.