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. Convert CString to char *

Convert CString to char *

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • V Offline
    V Offline
    Vassili
    wrote on last edited by
    #1

    How can I convert a CString to a char * variable? CString str; char *szBuffer; //code . . . szBuffer = (const char *) str; //why does this not work, and what do I have to do?

    A R 2 Replies Last reply
    0
    • V Vassili

      How can I convert a CString to a char * variable? CString str; char *szBuffer; //code . . . szBuffer = (const char *) str; //why does this not work, and what do I have to do?

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Use the getBuffer function.

      1 Reply Last reply
      0
      • V Vassili

        How can I convert a CString to a char * variable? CString str; char *szBuffer; //code . . . szBuffer = (const char *) str; //why does this not work, and what do I have to do?

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        CString str;
        TCHAR *szBuffer=(LPTSTR)(LPCTSTR)str;

        ~RaGE();

        J M 2 Replies Last reply
        0
        • R Rage

          CString str;
          TCHAR *szBuffer=(LPTSTR)(LPCTSTR)str;

          ~RaGE();

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          Rage wrote: TCHAR *szBuffer=(LPTSTR)(LPCTSTR)str; This only works is you do not write into the CString. If you really need to write into it, you have to use GetBuffer()/ReleaseBuffer().


          My opinions may have changed, but not the fact that I am right.

          R 1 Reply Last reply
          0
          • J jhwurmbach

            Rage wrote: TCHAR *szBuffer=(LPTSTR)(LPCTSTR)str; This only works is you do not write into the CString. If you really need to write into it, you have to use GetBuffer()/ReleaseBuffer().


            My opinions may have changed, but not the fact that I am right.

            R Offline
            R Offline
            Rage
            wrote on last edited by
            #5

            Yep, I know, but in his initial example he tried to typecaste in a const char *, so ... ~RaGE();

            J 1 Reply Last reply
            0
            • R Rage

              Yep, I know, but in his initial example he tried to typecaste in a const char *, so ... ~RaGE();

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              Yeah, but that type of stuff IS really dangerous. It works once you understood when to use it (at a InsertItem() with a LVITEM / TVITEM.pszText for example). But IMHO it is nothing to tell beginners. Shhhh!:-D


              My opinions may have changed, but not the fact that I am right.

              1 Reply Last reply
              0
              • R Rage

                CString str;
                TCHAR *szBuffer=(LPTSTR)(LPCTSTR)str;

                ~RaGE();

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                NO NO NO. :mad: Please don't encourage people to throw casts in without understanding the initial cause of their problem. The GetBuffer() method exists for precisely this case. --Mike-- Latest blog entry: *drool* (Alyson) [May 10] Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber "You have Erica on the brain" - Jon Sagara to me

                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