Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to covert CString to TCHAR and CString to LPCWSTR?

How to covert CString to TCHAR and CString to LPCWSTR?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    julych
    wrote on last edited by
    #1

    How to covert CString to TCHAR and CString to LPCWSTR? Where can I read about this?

    R P 2 Replies Last reply
    0
    • J julych

      How to covert CString to TCHAR and CString to LPCWSTR? Where can I read about this?

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      If you need to copy a CString object to an array of TCHAR you could do:
      // assume strText is your CString ... TCHAR *czArray = NULL; // allocate character array czArray = new TCHAR[strText.GetLength() + 1]; _tcscpy(czArray, (LPCTSTR)strText); // DO WHATEVERR // Cleanup delete [] czArray;
      The CString object has the LPCTSTR operator cast, so you can just cast your CString object to a LPCWSTR (LPCSTR if not unicode). Note that this is used in the above example for the _tcscpy function For further reading, have a look at the strings section of Codeproject, there are a few good articles that is a must-read.


      "..Even my comments have bugs!"
      Inspired by Toni78

      1 Reply Last reply
      0
      • J julych

        How to covert CString to TCHAR and CString to LPCWSTR? Where can I read about this?

        P Offline
        P Offline
        Peter Weyzen
        wrote on last edited by
        #3

        the easy way: USES_CONVERSION; CString Str(_T("This is my string")); // explicit copy as single byte chars (LCPSTR) char charBuf[255]; strcpy( charbuf, T2CA(Str) ); // explicit copy as wide chars (LPCWSTR) wchar_t wcharBuf[255]; wsccpy( wcharBuf, T2CW(Str) ); // generic TCHAR copy (LPCTSTR) TCHAR tcharBuf[255]; _tcscpy( tcharBuf, Str ); this works in UNICODE and non-UNICODE. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer Santa Cruz Networks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups