How to convert std::string to LPCSTR?

Cute picture Cute · Jul 29, 2009 · Viewed 201.7k times · Source

How can I convert a std::string to LPCSTR? Also, how can I convert a std::string to LPWSTR?

I am totally confused with these LPCSTR LPSTR LPWSTR and LPCWSTR.

Are LPWSTR and LPCWSTR the same?

Answer

Nick Meyer picture Nick Meyer · Jul 29, 2009

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.