Click to See Complete Forum and Search --> : copy a CString to a LPTSTR


Johnny DeMichael
May 31st, 1999, 10:59 AM
What would be the simplest way for me to copy a CString to a LPTSTR?

LPSTR pszText; //"This is type of LPSTR";
CString cs = "This is a test";

I want both variables to contain "This is a test". Thanks for any suggestions.
JD

laiason5
May 31st, 1999, 11:10 AM
i ran into the same problem and i think i fixed it usin GetBuffer.

try this:

LPSTR pszText = cs.GetBuffer(cs.GetLength());




tell me if it works

Yogen M
May 31st, 1999, 11:12 AM
CString has overloaded operator LPCTSTR. use that.
e.g.

LPSTR pszText; //"This is type of LPSTR";
CString cs = "This is a test";
pszTest = (LPSTR)(LPCTSTR)cs; //This will do the job; but will only return internal CString pointer
// If u want another copy of cs then try this
pszText = new TCHAR[cs.GetLength() + 1];
_tcscpy(pszText, (LPCTSTR)cs);

Yogen M
May 31st, 1999, 11:17 AM
Using GetBuffer will result in locking the CString object and any further modifications
will be lost untill u call ReleaseBuffer. Use the reply that I've posted before.

Johnny DeMichael
May 31st, 1999, 11:22 AM
Yogen M, thanks for your post. Works great!
JD

Johnny DeMichael
May 31st, 1999, 11:28 AM
laiason5, thanks for your post. It seems to work fine. See the reply to your post. Due to the locking problem, I am using the other method. I sure appreciate your reponse.
JD