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

CString to const char*

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 4 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 Victor Nijegorodov

    Is your build a UNICODE one? Then you will have to convert, not just cast your CString data. See [WideCharToMultiByte function (stringapiset.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) or ATL macros like W2A

    _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #4

    I have tried this also:

    const char\* chTemp = (const char\*)sTemp.GetBuffer(0);
    sTemp.ReleaseBuffer();
    TRACE(">>>>>>>>>>>>>>>>>>>%s\\n", chTemp);
    

    the project is not multi-byte ... the same result, just one letter (first letter) from the string ...

    V D 2 Replies Last reply
    0
    • _ _Flaviu

      I have tried this also:

      const char\* chTemp = (const char\*)sTemp.GetBuffer(0);
      sTemp.ReleaseBuffer();
      TRACE(">>>>>>>>>>>>>>>>>>>%s\\n", chTemp);
      

      the project is not multi-byte ... the same result, just one letter (first letter) from the string ...

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #5

      _Flaviu wrote:

      the project is not multi-byte

      Is it "not multi-byte" or "multi-byte"?

      _ 1 Reply Last reply
      0
      • V Victor Nijegorodov

        _Flaviu wrote:

        the project is not multi-byte

        Is it "not multi-byte" or "multi-byte"?

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #6

        Sorry, is multi-byte .. .anyway, if the project is unicode or is multi-byte, is the same thing.

        V 1 Reply Last reply
        0
        • V Victor Nijegorodov

          Is your build a UNICODE one? Then you will have to convert, not just cast your CString data. See [WideCharToMultiByte function (stringapiset.h) | Microsoft Docs](https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte) or ATL macros like W2A

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #7

          Is obviously something in project settings, because I have tried this code in a new test project and goes fine as multi-byte project ... as unicode, the same result.

          L 1 Reply Last reply
          0
          • _ _Flaviu

            Sorry, is multi-byte .. .anyway, if the project is unicode or is multi-byte, is the same thing.

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #8

            Try these tests:

            CStringA ansiText("TestA");
            LPCSTR ansi = (LPCSTR)ansiText;
            TRACE("%s\n", ansi);

            CString someText("Test");
            ansi = (LPCSTR)someText;
            TRACE("%s\n", ansi);

            What is the result?

            _ 1 Reply Last reply
            0
            • V Victor Nijegorodov

              Try these tests:

              CStringA ansiText("TestA");
              LPCSTR ansi = (LPCSTR)ansiText;
              TRACE("%s\n", ansi);

              CString someText("Test");
              ansi = (LPCSTR)someText;
              TRACE("%s\n", ansi);

              What is the result?

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #9

              with the first try:

              CStringA ansiText("TestA");
              LPCSTR ansi = (LPCSTR)ansiText;
              TRACE("%s\\n", ansi);
              

              the result is TestA for the second try, I got an error:

              1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'

              at

              CString someText("Test");
              ansi = (LPCSTR)someText;    // <-- here is the error
              TRACE("%s\\n", ansi);
              

              The project is unicode.

              V 1 Reply Last reply
              0
              • _ _Flaviu

                with the first try:

                CStringA ansiText("TestA");
                LPCSTR ansi = (LPCSTR)ansiText;
                TRACE("%s\\n", ansi);
                

                the result is TestA for the second try, I got an error:

                1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'

                at

                CString someText("Test");
                ansi = (LPCSTR)someText;    // <-- here is the error
                TRACE("%s\\n", ansi);
                

                The project is unicode.

                V Offline
                V Offline
                Victor Nijegorodov
                wrote on last edited by
                #10

                _Flaviu wrote:

                for the second try, I got an error:

                1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'

                at

                CString someText("Test");
                ansi = (LPCSTR)someText; // <-- here is the error
                TRACE("%s\n", ansi);

                The project is unicode.

                Of course you get the error since the CString contains the wide char text! Either change the build to be MBCS/ANSI or convert CString to wchar_t* (or use _T() macro).

                _ 1 Reply Last reply
                0
                • _ _Flaviu

                  Is obviously something in project settings, because I have tried this code in a new test project and goes fine as multi-byte project ... as unicode, the same result.

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

                  You cannot cast a CStingW (which is what the class will be in a Unicode environment) to an LPCSTR (which is const char*). You need to show again the actual failing code, and check the project settings.

                  1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    _Flaviu wrote:

                    for the second try, I got an error:

                    1>d:\tempx\test\testdoc.cpp(53): error C2440: 'type cast' : cannot convert from 'CString' to 'LPCSTR'

                    at

                    CString someText("Test");
                    ansi = (LPCSTR)someText; // <-- here is the error
                    TRACE("%s\n", ansi);

                    The project is unicode.

                    Of course you get the error since the CString contains the wide char text! Either change the build to be MBCS/ANSI or convert CString to wchar_t* (or use _T() macro).

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #12

                    "build to be MBCS/ANSI" you meant multi-byte project ?

                    L 1 Reply Last reply
                    0
                    • _ _Flaviu

                      "build to be MBCS/ANSI" you meant multi-byte project ?

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

                      Go to Project -> Properties -> General and make sure Character set is set to "Use Multi-Byte Charachter Set".

                      1 Reply Last reply
                      0
                      • _ _Flaviu

                        I have tried this also:

                        const char\* chTemp = (const char\*)sTemp.GetBuffer(0);
                        sTemp.ReleaseBuffer();
                        TRACE(">>>>>>>>>>>>>>>>>>>%s\\n", chTemp);
                        

                        the project is not multi-byte ... the same result, just one letter (first letter) from the string ...

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #14

                        _Flaviu wrote:

                        TRACE(">>>>>>>>>>>>>>>>>>>%s\n", chTemp);

                        What if you try:

                        TRACE(">>>>>>>>>>>>>>>>>>>%S\n", chTemp); // capital S

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        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