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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. cstring to tchar conversion

cstring to tchar conversion

Scheduled Pinned Locked Moved C / C++ / MFC
30 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 Francis K Antony

    hi i have a application to convert from CSTRING to TCHAR . with advanced thanks francis

    Franics K Antony

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

    is this useful,

    CString szString( "MyString" );
    int istrlen= (szString.GetLength() + 1);
    LPTSTR lpsz = new TCHAR[ istrlen];
    _tcscpy_s(lpsz, istrlen, szString);

    F 1 Reply Last reply
    0
    • P Paulraj G

      [Message Deleted]

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #4

      No ! Never use GetBuffer without knowing what it implies. Did you read the documentation of this function ?

      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • F Francis K Antony

        hi i have a application to convert from CSTRING to TCHAR . with advanced thanks francis

        Franics K Antony

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #5

        CString has a operator LPCTSTR meaning that the convertion to a const TCHAR* is automatic. You probably meant converting to an array of TCHAR, not a TCHAR, right ?

        Cédric Moonen Software developer
        Charting control [v1.2]

        1 Reply Last reply
        0
        • P Paulraj G

          [Message Deleted]

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #6

          Interesting. Can you please explain why would you want to use GetBuffer() here.

          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

          R 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Interesting. Can you please explain why would you want to use GetBuffer() here.

            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

            R Offline
            R Offline
            Rajkumar R
            wrote on last edited by
            #7

            LPTSTR pBuffer = szString.GetBuffer( 10 );
            // Directly access CString buffer ad use as TCHAR
            _tcscpy_s( pBuffer, 10, "String" );
            szString.ReleaseBuffer( );

            :)

            R CPalliniC 2 Replies Last reply
            0
            • R Rajkumar R

              LPTSTR pBuffer = szString.GetBuffer( 10 );
              // Directly access CString buffer ad use as TCHAR
              _tcscpy_s( pBuffer, 10, "String" );
              szString.ReleaseBuffer( );

              :)

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #8

              I'd rather liked it if he had replied to me. I would not recommend the usage of GetBuffer() method for the sake of copying the string content to another TCHAR* . Your CString object is not integral between the GetBuffer() and ReleaseBuffer() calls. Which means two things: 1. You must never forget to call ReleaseBuffer() after you're done with it. 2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              CPalliniC R 2 Replies Last reply
              0
              • R Rajkumar R

                LPTSTR pBuffer = szString.GetBuffer( 10 );
                // Directly access CString buffer ad use as TCHAR
                _tcscpy_s( pBuffer, 10, "String" );
                szString.ReleaseBuffer( );

                :)

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                I see an error in your code. Hint: you're usign generic text mappings. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                [my articles]

                In testa che avete, signor di Ceprano?

                R 2 Replies Last reply
                0
                • R Rajesh R Subramanian

                  I'd rather liked it if he had replied to me. I would not recommend the usage of GetBuffer() method for the sake of copying the string content to another TCHAR* . Your CString object is not integral between the GetBuffer() and ReleaseBuffer() calls. Which means two things: 1. You must never forget to call ReleaseBuffer() after you're done with it. 2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #10

                  Rajesh R Subramanian wrote:

                  I would not recommend the usage of GetBuffer() method for the sake of copying the string content to another TCHAR* .

                  I agree.

                  Rajesh R Subramanian wrote:

                  Which means two things: 1. You must never forget to call ReleaseBuffer() after you're done with it. 2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                  In fact he took care about both the issues.

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  In testa che avete, signor di Ceprano?

                  R 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Rajesh R Subramanian wrote:

                    I would not recommend the usage of GetBuffer() method for the sake of copying the string content to another TCHAR* .

                    I agree.

                    Rajesh R Subramanian wrote:

                    Which means two things: 1. You must never forget to call ReleaseBuffer() after you're done with it. 2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                    In fact he took care about both the issues.

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    [my articles]

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #11

                    CPallini wrote:

                    In fact he took care about both the issues.

                    And the first line of my reply was "I'd rather liked it if he had replied to me." His post was something like "hey, use GetBuffer()". He wasn't bothered to explain the issues related to it, mostly because he didn't know it. And you agreed GetBuffer() isn't the best way around to do it. :)

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    CPalliniC 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      CPallini wrote:

                      In fact he took care about both the issues.

                      And the first line of my reply was "I'd rather liked it if he had replied to me." His post was something like "hey, use GetBuffer()". He wasn't bothered to explain the issues related to it, mostly because he didn't know it. And you agreed GetBuffer() isn't the best way around to do it. :)

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #12

                      Rajesh R Subramanian wrote:

                      And you agreed GetBuffer() isn't the best way around to do it

                      In fact it is the only way if you really need a pointer to the string buffer (i.e. whenever CString LPCTSTR cast operator isn't a viable option) though I agree, usually asking for a pointer to editable CString buffer is not a good idea. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        I see an error in your code. Hint: you're usign generic text mappings. :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        [my articles]

                        R Offline
                        R Offline
                        Rajkumar R
                        wrote on last edited by
                        #13

                        I type directly in the reply message, I didn't compile. I think, if i put _T(""), may be u will ask for #include <tchar.h> :)

                        CPalliniC 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          I'd rather liked it if he had replied to me. I would not recommend the usage of GetBuffer() method for the sake of copying the string content to another TCHAR* . Your CString object is not integral between the GetBuffer() and ReleaseBuffer() calls. Which means two things: 1. You must never forget to call ReleaseBuffer() after you're done with it. 2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                          R Offline
                          R Offline
                          Rajkumar R
                          wrote on last edited by
                          #14

                          Rajesh R Subramanian wrote:

                          1. You must never forget to call ReleaseBuffer()

                          It is similar to new TCHAR []; and delete

                          Rajesh R Subramanian wrote:

                          2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                          I expect we want to work on TCHAR rather than CString in this context

                          R 1 Reply Last reply
                          0
                          • R Rajkumar R

                            is this useful,

                            CString szString( "MyString" );
                            int istrlen= (szString.GetLength() + 1);
                            LPTSTR lpsz = new TCHAR[ istrlen];
                            _tcscpy_s(lpsz, istrlen, szString);

                            F Offline
                            F Offline
                            Francis K Antony
                            wrote on last edited by
                            #15

                            i tried like ur method . but i did not get perfect o/p. can u tell more

                            Franics K Antony

                            R 1 Reply Last reply
                            0
                            • R Rajkumar R

                              Rajesh R Subramanian wrote:

                              1. You must never forget to call ReleaseBuffer()

                              It is similar to new TCHAR []; and delete

                              Rajesh R Subramanian wrote:

                              2. All the member functions on your CString object are *not* guarenteed to work perfectly between the GetBuffer() and ReleaseBuffer() calls.

                              I expect we want to work on TCHAR rather than CString in this context

                              R Offline
                              R Offline
                              Rajesh R Subramanian
                              wrote on last edited by
                              #16

                              Rajkumar R wrote:

                              It is similar to new TCHAR []; and delete

                              Thanks, but I am not in need of this information.

                              Rajkumar R wrote:

                              I expect we want to work on TCHAR rather than CString in this context

                              We may do it, becasue we know it. You can't expect the O.P. to always know it. Also, I've stated several times this is not the best approach to extract the string from a CString object.

                              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                              R 1 Reply Last reply
                              0
                              • R Rajkumar R

                                I type directly in the reply message, I didn't compile. I think, if i put _T(""), may be u will ask for #include <tchar.h> :)

                                CPalliniC Offline
                                CPalliniC Offline
                                CPallini
                                wrote on last edited by
                                #17

                                Rajkumar R wrote:

                                may be u will ask for #include

                                of course. :-D

                                Rajkumar R wrote:

                                I type directly in the reply message, I didn't compile.

                                My brain-integrated-compiler issued the error! :laugh:

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                [my articles]

                                In testa che avete, signor di Ceprano?

                                R 1 Reply Last reply
                                0
                                • CPalliniC CPallini

                                  Rajkumar R wrote:

                                  may be u will ask for #include

                                  of course. :-D

                                  Rajkumar R wrote:

                                  I type directly in the reply message, I didn't compile.

                                  My brain-integrated-compiler issued the error! :laugh:

                                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                  [my articles]

                                  R Offline
                                  R Offline
                                  Rajkumar R
                                  wrote on last edited by
                                  #18

                                  Oh "My brilliant Brain" !!!

                                  CPalliniC 1 Reply Last reply
                                  0
                                  • F Francis K Antony

                                    i tried like ur method . but i did not get perfect o/p. can u tell more

                                    Franics K Antony

                                    R Offline
                                    R Offline
                                    Rajkumar R
                                    wrote on last edited by
                                    #19

                                    Can you tell what u exactly need, what is the issue u r facing now

                                    F 1 Reply Last reply
                                    0
                                    • R Rajkumar R

                                      Can you tell what u exactly need, what is the issue u r facing now

                                      F Offline
                                      F Offline
                                      Francis K Antony
                                      wrote on last edited by
                                      #20

                                      i am using encryption in the server and cliient . in that i want to encrypt my data in the server and to decrypt in the client side . to encrypt the data want to convert CSTRING into TCHAR ...

                                      Franics K Antony

                                      R 1 Reply Last reply
                                      0
                                      • F Francis K Antony

                                        i am using encryption in the server and cliient . in that i want to encrypt my data in the server and to decrypt in the client side . to encrypt the data want to convert CSTRING into TCHAR ...

                                        Franics K Antony

                                        R Offline
                                        R Offline
                                        Rajkumar R
                                        wrote on last edited by
                                        #21

                                        Man, u want to encrypt data i agree, but why u r encrypting the question? What is the issue, can u post the code snippet.

                                        1 Reply Last reply
                                        0
                                        • R Rajkumar R

                                          Oh "My brilliant Brain" !!!

                                          CPalliniC Offline
                                          CPalliniC Offline
                                          CPallini
                                          wrote on last edited by
                                          #22

                                          Rajkumar R wrote:

                                          Oh "My brilliant Brain" !!!

                                          Indeed. But not your, mine. :laugh:

                                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                          [my articles]

                                          In testa che avete, signor di Ceprano?

                                          R 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