Click here to Skip to main content
13,762,666 members
Rate this:
Please Sign up or sign in to vote.
See more:
How to convert string to LPCTSTR?
Posted 2-Jun-12 22:30pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

ATL provides some macros to do this.
#include <atlbase.h>

The relevant macros are:
CA2T (const ANSI to TCHAR)
CW2T (const wide to TCHAR).

Google USES_CONVERSION. See also ATL and MFC String Conversion Macros [^]

If you're using STL strings, you may want to typedef std::basic_string<tchar> tstring</tchar>.
Lately, I find myself using more and more explicit calls to the Unicode versions of the Windows API functions, and using std::wstring for all my strings. ANSI is a bit dated...

Hope this helps,
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

The actual answer to your question is: No, there is no way to convert a string to an LPCTSTR. The first is an object that is represented by a chunk of memory, the latter is a pointer to such an object.

Now, let's interpret your question as: "I have a string and need an LPCTSTR, e.g. for passing an argument to a function. How can I do that?". Then the problem is solvable and turns out to have to major components:

1) Memory allocation

2) Character representation (8-bit vs. 16-bit)

Let's start with (2). LPTCSTR is defined as pointer to a char string or wchar_t string, depending on your compilation settings (Multi-byte string or Unicode string in your VC++ project settings). If your source string happens to be in the other format, you have to use some conversion mechanism to translate wide characters (16-bit) to 8-bit characters or vice versa. You can use functions like MultiByteToWideChar or its counterpart to do that. Or, if you are using CString, the task may be as easy as writing:

// assuming we are compiling for Unicode
CString s1;
CStringA s2 (s1); // translates s1 to an 8-bit char string

If your source string happens to have the "right" character size, you don't have to convert anything. CString has a built-in cast function to "pointer to const char", so you can write

CString s1;
LPCTSTR pS2 = s1;

and s1 will give you a pointer to its internal buffer.

If you are using STL::string you must do the cast explicitly by calling the c_str member function, for example:

// assuming you are compiling for multi-byte (8-bit) strings
STL::string s1;
LPCTSTR pS2 = s1.c_str();

Now to problem (1), buffer management. If you don't have to convert, because character formats do match, you can simply use a pointer to the existing string buffer, just as shown in the two examples above.

If you do have to convert you need an additional buffer for the conversion result. Again you can use several techniques to acquire such a buffer. In the first example we used another CString to provide the buffer (and used CString's capability to convert string of the other "gender"). You can do equally well with STL::string rsp. STL::wstring. Or if you want to do it all by hand you have to allocate the buffer by new or malloc.

I know, there are many options and alternatives. If you let us know, which type of string you are using and whether you are compiling for mulit-byte character strings or Unicode and what you want to do with the LPCTSTR we could give some concrete advice on how to proceed.
Richard MacCutchan 3-Jun-12 6:57am
Good answer, lots of useful detail. +5
nv3 3-Jun-12 7:02am
Thank you, Richard!
Harmanjeet Singh 3-Jun-12 10:12am
i tried what u said..
the 3rd option
i have
std::string str;

i converted it to LPCTSTR using
LPCTSTR lstr = str.c_str();

its giving error
"a value of type const char* cant be used to initialize an entity of type LPCTSTR"
nv3 3-Jun-12 11:33am
I assume that your project is being compiled for Unicode. You can check that by opening the project properties, click the General item on the left and than look under "Character Set".

Now, your string str is defined as an 8-bit character string and hence c_str() delivers a "const char*". What LPCTSTR however expects is a "const wchar_t*". The solution: use wstring instead of string.

If you happend to have an existing string of type string the you need to first convert it to a wstring, for example like that:

string s1 ("abc");
wstring s2;
s2.assign(s1.begin(), s1.end());
LPCTSTR p = s2.c_str();

Hope that solves your problem; otherwise don't hesitate to add another comment.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

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;

std::string s;

#ifdef UNICODE
std::wstring stemp = s2ws(s); // Temporary buffer is required
LPCWSTR result = stemp.c_str();
LPCWSTR result = s.c_str();

fjdiewornncalwe 20-Sep-12 18:45pm
Please credit the source when you copy/paste something that isn't your own.
Source(Andrew Revvo)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

See following variants:
1) The conversion is simple:
std::string myString;
LPCSTR lpMyString = myString.c_str();

std::string myString;

LPWSTR ws = new wchar_t[myString.size()+1];
copy( myString.begin(), myString.end(), ws );
ws[myString.size()] = 0; // zero at the end
return ws;

std::string myString;
LPSTR pst = &myString[0];  // get temporary LPSTR, not very safe

4)You can also use _stprintf ....

It may be very useful for you:[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.181112.3 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100