Click here to Skip to main content
15,888,006 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
How to convert string to LPCTSTR?
Posted

ATL provides some macros to do this.
C++
#include <atlbase.h>
USES_CONVERSION;
</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,
Pablo.
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
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.
Michael Wilfred 19-Nov-21 15:46pm    
I could kiss you right now, this is the exact answer I needed and you were the only person who answered this with a detailed explanation. Thank you
C++
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();
#else
LPCWSTR result = s.c_str();
#endif>

Thanks
 
Share this answer
 
Comments
fjdiewornncalwe 20-Sep-12 18:45pm    
Please credit the source when you copy/paste something that isn't your own.
Source(Andrew Revvo)
See following variants:
1) The conversion is simple:
std::string myString;
LPCSTR lpMyString = myString.c_str();

2)
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;

3)
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:
http://cboard.cprogramming.com/windows-programming/127052-lpctstr-operations-convertion.html[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900