CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 1999
    Posts
    74

    copy a CString to a LPTSTR

    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



  2. #2
    Join Date
    May 1999
    Posts
    37

    Re: copy a CString to a LPTSTR

    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


  3. #3
    Join Date
    May 1999
    Location
    OR, USA
    Posts
    65

    Re: copy a CString to a LPTSTR

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





  4. #4
    Join Date
    May 1999
    Location
    OR, USA
    Posts
    65

    Re: copy a CString to a LPTSTR

    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.


  5. #5
    Join Date
    Apr 1999
    Posts
    74

    Re: copy a CString to a LPTSTR

    Yogen M, thanks for your post. Works great!
    JD


  6. #6
    Join Date
    Apr 1999
    Posts
    74

    Re: copy a CString to a LPTSTR

    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured