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

How to Convert CString to _TCHAR *

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialcareer
30 Posts 11 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.
  • M Matthew Faithfull

    No problem, in that case my recommendation not to use CString stands :)

    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

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

    Umm... I'll have to agree. But you're a tough guy with that principle man. One must either know what CString is, or should not use it at all. :-D

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

    M 1 Reply Last reply
    0
    • M Maxim Zarus

      Hi experts... What about this? _tcscpy(sEndDate,CurrentDate); I am using this style. Is this wrong or right way?

      modified on Tuesday, May 27, 2008 7:42 AM

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

      That makes a copy. It is fine sometimes (and the other times it is wrong). :)

      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.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      M 1 Reply Last reply
      0
      • M Maxim Zarus

        Hi experts... What about this? _tcscpy(sEndDate,CurrentDate); I am using this style. Is this wrong or right way?

        modified on Tuesday, May 27, 2008 7:42 AM

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #11

        In general an explicit function call should be preferred over an implicit cast so it's not wrong but a GetBuffer() call would be better style and of course you should otherwise be using _tcscpy_s :-D

        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

        C 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          Umm... I'll have to agree. But you're a tough guy with that principle man. One must either know what CString is, or should not use it at all. :-D

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

          M Offline
          M Offline
          Matthew Faithfull
          wrote on last edited by
          #12

          Rajesh R Subramanian wrote:

          you're a tough guy

          :laugh: Not really. I certainly wouldn't apply that everywhere but CString is a bit if an exceptional case. CString abuse is so rife and so easy to fall into, and CString itself so potentially inefficient and error prone that I would say understand it or don't use it. I would not say the same for example for stl::vector or stl::map where misuse is less likely and understanding the source very much harder.

          "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

          1 Reply Last reply
          0
          • C CPallini

            That makes a copy. It is fine sometimes (and the other times it is wrong). :)

            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.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            M Offline
            M Offline
            Maxim Zarus
            wrote on last edited by
            #13

            Sir, just tell me where it will fail? I want to clear my confusion. Thanks:confused:

            C 1 Reply Last reply
            0
            • M Maxim Zarus

              Sir, just tell me where it will fail? I want to clear my confusion. Thanks:confused:

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #14

              Whenever you need to actually modify CString's internal buffer. It is not a common usage, I know, but it is perfectly legal.

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              M 1 Reply Last reply
              0
              • C CPallini

                Whenever you need to actually modify CString's internal buffer. It is not a common usage, I know, but it is perfectly legal.

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                M Offline
                M Offline
                Maxim Zarus
                wrote on last edited by
                #15

                Thank you sir :)

                1 Reply Last reply
                0
                • M Matthew Faithfull

                  In general an explicit function call should be preferred over an implicit cast so it's not wrong but a GetBuffer() call would be better style and of course you should otherwise be using _tcscpy_s :-D

                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #16

                  Matthew Faithfull wrote:

                  it's not wrong but a GetBuffer() call would be better style

                  I don't agree. Implicit (or explicit) cast is not the same as GetBuffer() and you shouldn't use optionally one or the other: GetBuffer returns LPTSTR, while the cast returns LPCTSTR: the added C have his significance. :)

                  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.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  M T 2 Replies Last reply
                  0
                  • M manju 123

                    Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju

                    Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

                    S Offline
                    S Offline
                    SandipG
                    wrote on last edited by
                    #17

                    You can even use macrs A2W and W2A for conversions from multibyte-widechar and widechar-multibyte For these macros you need to include "atlconv.h" and also you need to write USES_CONVERSION macro before using these macros inside function.

                    1 Reply Last reply
                    0
                    • C CPallini

                      Matthew Faithfull wrote:

                      it's not wrong but a GetBuffer() call would be better style

                      I don't agree. Implicit (or explicit) cast is not the same as GetBuffer() and you shouldn't use optionally one or the other: GetBuffer returns LPTSTR, while the cast returns LPCTSTR: the added C have his significance. :)

                      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.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      M Offline
                      M Offline
                      Matthew Faithfull
                      wrote on last edited by
                      #18

                      CPallini wrote:

                      the added C have his significance.

                      Indeed it has and although I was talking general C++ style I do think it applies in this case. The CString impilcit cast returns LPCSTR because it isn't safe for it to hand out a pointer to its internal buffer without locking it but it also isn't good C++ for it to 'silently' give you a const pointer to something that is inherently not const. It's a compromise brought on by a compromised design.

                      "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                      C 1 Reply Last reply
                      0
                      • M Matthew Faithfull

                        CPallini wrote:

                        the added C have his significance.

                        Indeed it has and although I was talking general C++ style I do think it applies in this case. The CString impilcit cast returns LPCSTR because it isn't safe for it to hand out a pointer to its internal buffer without locking it but it also isn't good C++ for it to 'silently' give you a const pointer to something that is inherently not const. It's a compromise brought on by a compromised design.

                        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #19

                        Well, let's try to get it from the CString's consumer point of view: (1) requesting, via (explicit) cast a pointer to a const buffer means: "OK, I need the buffer but I'll not change it". (2) requesting via GetBuffer() a pointer to the internal buffer means: "I need the buffer to make all the weirdest things I know to it". Clearly method (2) is a bit crude for a mere copy operation. :-D

                        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.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                        R 1 Reply Last reply
                        0
                        • C CPallini

                          Well, let's try to get it from the CString's consumer point of view: (1) requesting, via (explicit) cast a pointer to a const buffer means: "OK, I need the buffer but I'll not change it". (2) requesting via GetBuffer() a pointer to the internal buffer means: "I need the buffer to make all the weirdest things I know to it". Clearly method (2) is a bit crude for a mere copy operation. :-D

                          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.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

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

                          hi der, da getbuffr iz renamed in da latast sdk as GetBufferIKnowWhatImDoing()

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

                          C 1 Reply Last reply
                          0
                          • J Jijo Raj

                            manju#123 wrote:

                            sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me

                            the (LPCSTR)CurrentDate returns a constant TCHAR pointer. you can make it compilable by making sEndDate as const. For instance,

                            const _TCHAR *sEndDate;

                            If you want to modify the sEndDate, then you can use GetBuffer() as suggested by Matthew Faithfull, But dont forget to call ReleaseBuffer(). Regards, Jijo.

                            _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                            T Offline
                            T Offline
                            toxcct
                            wrote on last edited by
                            #21

                            Jijo raj wrote:

                            the (LPCSTR)CurrentDate returns a constant TCHAR pointer

                            wrong. it returns a const char pointer. (LP-C-T-STR) returns a const TCHAR*

                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                            J 1 Reply Last reply
                            0
                            • C CPallini

                              Matthew Faithfull wrote:

                              it's not wrong but a GetBuffer() call would be better style

                              I don't agree. Implicit (or explicit) cast is not the same as GetBuffer() and you shouldn't use optionally one or the other: GetBuffer returns LPTSTR, while the cast returns LPCTSTR: the added C have his significance. :)

                              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.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                              T Offline
                              T Offline
                              toxcct
                              wrote on last edited by
                              #22

                              I strongly second that. GetBuffer() is really to be forbidden for cast purpose

                              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                              1 Reply Last reply
                              0
                              • M Maxim Zarus

                                Hi experts... What about this? _tcscpy(sEndDate,CurrentDate); I am using this style. Is this wrong or right way?

                                modified on Tuesday, May 27, 2008 7:42 AM

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

                                Maxim Zarus wrote:

                                Is this wrong or right way?

                                It's wrong since sEndDate has no storage space; it's just a pointer (to wherever).

                                "Love people and use things, not love things and use people." - Unknown

                                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                M 1 Reply Last reply
                                0
                                • R Rajesh R Subramanian

                                  hi der, da getbuffr iz renamed in da latast sdk as GetBufferIKnowWhatImDoing()

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

                                  C Offline
                                  C Offline
                                  CPallini
                                  wrote on last edited by
                                  #24

                                  Indeed! :-D

                                  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.
                                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                                  1 Reply Last reply
                                  0
                                  • T toxcct

                                    Jijo raj wrote:

                                    the (LPCSTR)CurrentDate returns a constant TCHAR pointer

                                    wrong. it returns a const char pointer. (LP-C-T-STR) returns a const TCHAR*

                                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                    J Offline
                                    J Offline
                                    Jijo Raj
                                    wrote on last edited by
                                    #25

                                    Typo. :-O Actually I mean LPCTSTR. But a T means lot! :) Thanks for pointing it out. Regards, Jijo.

                                    _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                                    1 Reply Last reply
                                    0
                                    • D David Crow

                                      Maxim Zarus wrote:

                                      Is this wrong or right way?

                                      It's wrong since sEndDate has no storage space; it's just a pointer (to wherever).

                                      "Love people and use things, not love things and use people." - Unknown

                                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                      M Offline
                                      M Offline
                                      Maxim Zarus
                                      wrote on last edited by
                                      #26

                                      you mean, first allocate memory for sEndDate then i can use _tcscpy_s(). its ok. i agree. but is it also neccessary to allocate memory when CString::GetBuffer() has been used? because i have seen in pervious post nobody ask to allocate memory for CString::GetBuffer(). regards, Maxim... :)

                                      D 1 Reply Last reply
                                      0
                                      • M manju 123

                                        Hi all.. I want to convert CString to _TCHAR* ////////////////////////////// _TCHAR *sEndDate; CString CurrDate; ///////////////////// I am trying this code.. sEndDate = (LPCSTR)CurrentDate; Its not working ... plz help me manju

                                        Hi.. I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++ manju

                                        H Offline
                                        H Offline
                                        Hamid Taebi
                                        wrote on last edited by
                                        #27

                                        And see The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^] for more info about converts. ;)

                                        1 Reply Last reply
                                        0
                                        • M Maxim Zarus

                                          you mean, first allocate memory for sEndDate then i can use _tcscpy_s(). its ok. i agree. but is it also neccessary to allocate memory when CString::GetBuffer() has been used? because i have seen in pervious post nobody ask to allocate memory for CString::GetBuffer(). regards, Maxim... :)

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

                                          Maxim Zarus wrote:

                                          you mean, first allocate memory for sEndDate then i can use _tcscpy_s().

                                          Yes.

                                          Maxim Zarus wrote:

                                          but is it also neccessary to allocate memory when CString::GetBuffer() has been used?

                                          Not necessarily. It all depends on what you are going to be doing with the returned pointer.

                                          Maxim Zarus wrote:

                                          because i have seen in pervious post nobody ask to allocate memory for CString::GetBuffer().

                                          CString::GetBuffer() is very often misused.

                                          "Love people and use things, not love things and use people." - Unknown

                                          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                                          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