Sign in to follow this  

How to convert a string to a LPTSTR?

This topic is 4599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Assuming that by "LPTSTR" you mean a char* (call things by their proper names, damnit) then the answer is not quite.

std::string::c_str() will return a const char*.

If you are dying for a char*, you can either copy out the string to a buffer using std::string::copy(), or take your chances and cast away the constness with a const_cast<char*>. Of course, modifying the string that way may break everything. You're on your own if that's what you decide to do.

Now, since you mentioned LPTSTR, and since the "T" in LPTSTR seems to imply you are using MS' TCHAR typedef, you'll want to either use std::string or std::wstring, depending on whether TCHAR resolves to char or wchar_t.

In fact, you might as well get used to using std::basic_string<TCHAR>. Or better, make a typedef for it. In fact, there might already be one conveniently defined for you in <tchar.h> (I think that's what the header is - not being part of standard C++, I'm not sure).

Share this post


Link to post
Share on other sites

This topic is 4599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this