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. C / C++ / MFC
  3. CString to CHAR *

CString to CHAR *

Scheduled Pinned Locked Moved C / C++ / MFC
8 Posts 6 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.
  • F Offline
    F Offline
    Frank Deo
    wrote on last edited by
    #1

    I know this has to be simple, but when compiling...I get this error: cannot convert from 'class CString' to 'char *' How do I convert a CString to 'char *'? Thanks! Frank

    A 1 Reply Last reply
    0
    • F Frank Deo

      I know this has to be simple, but when compiling...I get this error: cannot convert from 'class CString' to 'char *' How do I convert a CString to 'char *'? Thanks! Frank

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

      CString str; conversion -> LPCSTR(str);

      F 1 Reply Last reply
      0
      • A Anonymous

        CString str; conversion -> LPCSTR(str);

        F Offline
        F Offline
        Frank Deo
        wrote on last edited by
        #3

        I need to convert from CString to LPSTR. The conversion from CString to Constant CHAR* doesnt work for this function. Any other ideas? :) Frank

        T M L S 4 Replies Last reply
        0
        • F Frank Deo

          I need to convert from CString to LPSTR. The conversion from CString to Constant CHAR* doesnt work for this function. Any other ideas? :) Frank

          T Offline
          T Offline
          Tim Deveaux
          wrote on last edited by
          #4

          The CString provides operator LPCTSTR to perform the conversion to const char*. This conversion is implicit if you call a method taking a const char * with a CString. I find this a bit clunky, but you can use it explicitly and cast away the const like so: (LPTSTR)(LPCTSTR)str

          1 Reply Last reply
          0
          • F Frank Deo

            I need to convert from CString to LPSTR. The conversion from CString to Constant CHAR* doesnt work for this function. Any other ideas? :) Frank

            M Offline
            M Offline
            Mike Dunn
            wrote on last edited by
            #5

            CString str = _T("fluff");
            LPTSTR psz = str.GetBuffer(0);

            // use psz here...

            str.ReleaseBuffer();

            F 1 Reply Last reply
            0
            • F Frank Deo

              I need to convert from CString to LPSTR. The conversion from CString to Constant CHAR* doesnt work for this function. Any other ideas? :) Frank

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Have you tried this: CString str; (char*)(LPCTSTR)str

              1 Reply Last reply
              0
              • F Frank Deo

                I need to convert from CString to LPSTR. The conversion from CString to Constant CHAR* doesnt work for this function. Any other ideas? :) Frank

                S Offline
                S Offline
                Sandra
                wrote on last edited by
                #7

                str.GetBuffer(0) does the work. Don't forget to call str.ReleaseBuffer Also (LPSTR)(LPCSTR)str will be ok in the case you don't plan to play with the buffer

                1 Reply Last reply
                0
                • M Mike Dunn

                  CString str = _T("fluff");
                  LPTSTR psz = str.GetBuffer(0);

                  // use psz here...

                  str.ReleaseBuffer();

                  F Offline
                  F Offline
                  Frank Deo
                  wrote on last edited by
                  #8

                  Worked great. Thanks Mike.

                  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