LPCTSTR to std::string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucy Ludmiller

    LPCTSTR to std::string

    Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem to
    finda nyting (that dosen't blab on for several pages) on the net about
    how to do this

  • Carl Daniel [VC++ MVP]

    #2
    Re: LPCTSTR to std::string

    Lucy Ludmiller wrote:
    Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem
    to finda nyting (that dosen't blab on for several pages) on the net
    about how to do this
    typedef std::basic_stri ng<TCHARtstring ;

    LPCTSTR pstz;
    // ...
    tstring tstr(ptsz); // construct a tstring from an LPCTSTR
    // ...

    Now, if you always want a narrow string (std::string), regardless of whether
    it's a Unicode build or not, then you'll have to insert a narrowing
    conversion from TSTR to char*, e.g. using wcstombs or WideCharToMulti Byte.

    Generally though, what you want it a std::basic_stri ng of the same
    "wideness" as TCHAR, so the simple typedef will do the trick.

    -cd


    Comment

    • Bruno van Dooren [MVP VC++]

      #3
      Re: LPCTSTR to std::string

      >Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem
      >to finda nyting (that dosen't blab on for several pages) on the net
      >about how to do this
      >
      typedef std::basic_stri ng<TCHARtstring ;
      >
      LPCTSTR pstz;
      // ...
      tstring tstr(ptsz); // construct a tstring from an LPCTSTR
      Just make sure that ptsz is different from NULL before you do this.

      --

      Kind regards,
      Bruno van Dooren
      bruno_nos_pam_v an_dooren@hotma il.com
      Remove only "_nos_pam"


      Comment

      • Ray

        #4
        Re: LPCTSTR to std::string

        Bruno van Dooren [MVP VC++] wrote:
        >>Anyone knows how to convert a LPCTSTR to an STL striung?. Can't seem
        >>to finda nyting (that dosen't blab on for several pages) on the net
        >>about how to do this
        >typedef std::basic_stri ng<TCHARtstring ;
        >>
        >LPCTSTR pstz;
        >// ...
        >tstring tstr(ptsz); // construct a tstring from an LPCTSTR
        >
        Just make sure that ptsz is different from NULL before you do this.
        >
        Hi Bruno,

        Any idea why Windows developers (not people who develop apps for
        Windows, but people who develop Windows + the SDK) likes doing a
        misleading #define on pointer so much?

        I've been doing this for sometime so it doesn't surprise me anymore, but
        I've seen many cases of people who just started and don't realize that
        LPCTSTR is a const TCHAR* and think that it's some non-pointer magic
        string and as such no memory allocation necessary, etc. What's wrong
        with const TCHAR*, really?

        Thanks,
        Ray

        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: LPCTSTR to std::string

          Ray wrote:
          Bruno van Dooren [MVP VC++] wrote:
          >>>Anyone knows how to convert a LPCTSTR to an STL striung?. Can't
          >>>seem to finda nyting (that dosen't blab on for several pages) on
          >>>the net about how to do this
          >>typedef std::basic_stri ng<TCHARtstring ;
          >>>
          >>LPCTSTR pstz;
          >>// ...
          >>tstring tstr(ptsz); // construct a tstring from an LPCTSTR
          >>
          >Just make sure that ptsz is different from NULL before you do this.
          >>
          >
          Hi Bruno,
          >
          Any idea why Windows developers (not people who develop apps for
          Windows, but people who develop Windows + the SDK) likes doing a
          misleading #define on pointer so much?
          >
          I've been doing this for sometime so it doesn't surprise me anymore,
          but I've seen many cases of people who just started and don't realize
          that LPCTSTR is a const TCHAR* and think that it's some non-pointer
          magic string and as such no memory allocation necessary, etc. What's
          wrong with const TCHAR*, really?
          >
          In the days of different pointer sizes (near, far, huge, etc), it was
          helpful to have typedefs. Even without those issues anymore, many C and C++
          programmer prefer to use a typedef instead of retyping a compound type every
          time.

          As to why someone would think it's something special, or doesn't need the
          same care for memory management that any other pointer deserves, I couldn't
          say.

          -cd


          Comment

          • Ray

            #6
            Re: LPCTSTR to std::string

            Carl Daniel [VC++ MVP] wrote:
            In the days of different pointer sizes (near, far, huge, etc), it was
            helpful to have typedefs. Even without those issues anymore, many C and C++
            programmer prefer to use a typedef instead of retyping a compound type every
            time.
            Oh... no wonder. That explains it. Thanks Carl :)
            As to why someone would think it's something special, or doesn't need the
            same care for memory management that any other pointer deserves, I couldn't
            say.
            Yeah... I suppose it's because one has expected a pointer to have a *
            when you define it. e.g.: it's clearer to have

            void blah(const TCHAR* b)

            instead of

            void blah(LPCTSTR b)

            although after a while of Win programming it's kinda becomes a habit.

            Cheers
            Ray
            >
            -cd
            >
            >

            Comment

            • r norman

              #7
              Re: LPCTSTR to std::string

              On Tue, 05 Sep 2006 20:49:27 +0800, Ray <ray_usenet@yah oo.comwrote:
              >Carl Daniel [VC++ MVP] wrote:
              >In the days of different pointer sizes (near, far, huge, etc), it was
              >helpful to have typedefs. Even without those issues anymore, many C and C++
              >programmer prefer to use a typedef instead of retyping a compound type every
              >time.
              >
              >Oh... no wonder. That explains it. Thanks Carl :)
              >
              >As to why someone would think it's something special, or doesn't need the
              >same care for memory management that any other pointer deserves, I couldn't
              >say.
              >
              >Yeah... I suppose it's because one has expected a pointer to have a *
              >when you define it. e.g.: it's clearer to have
              >
              >void blah(const TCHAR* b)
              >
              >instead of
              >
              >void blah(LPCTSTR b)
              >
              >although after a while of Win programming it's kinda becomes a habit.
              >
              It all makes sense when you understand the naming conventions.

              "LP" means "long pointer", the "long" referring as mentioned to the
              days when Intel CPU's used either long (segment-offset) or short
              (offset only) pointers. The "C" means constant. And, of course,
              "TSTR" means string of TCHAR.

              Virtually all Windows pointer types have LP or P names.

              I know people rail against such naming conventions but I find it
              useful (provided they are accurate).




              Comment

              • Mihajlo Cvetanović

                #8
                Re: LPCTSTR to std::string

                While we're at it, I just found out that PTCHAR isn't the same as
                LPTSTR. The following code won't compile:

                #include <tchar.h>
                #include <windows.h>

                int _tmain(int argc, TCHAR* argv[])
                {
                PTCHAR p = 0;
                }

                If the two include directives change places, OR if LPTSTR stands instead
                of PTCHAR then it will compile.

                Comment

                • Ray

                  #9
                  Re: LPCTSTR to std::string

                  Mihajlo Cvetanović wrote:
                  While we're at it, I just found out that PTCHAR isn't the same as
                  LPTSTR. The following code won't compile:
                  >
                  #include <tchar.h>
                  #include <windows.h>
                  >
                  int _tmain(int argc, TCHAR* argv[])
                  {
                  PTCHAR p = 0;
                  }
                  >
                  If the two include directives change places, OR if LPTSTR stands instead
                  of PTCHAR then it will compile.
                  Ugh... there you have another reason to have a simple TCHAR* instead of
                  these defines.

                  Ray

                  Comment

                  • Tamas Demjen

                    #10
                    Re: LPCTSTR to std::string

                    r norman wrote:
                    "LP" means "long pointer", the "long" referring as mentioned to the
                    days when Intel CPU's used either long (segment-offset) or short
                    (offset only) pointers. The "C" means constant. And, of course,
                    "TSTR" means string of TCHAR.
                    Sure, one can learn how to decode this information, and be able to deal
                    with these typedefs. But the question is not whether you can do it or
                    not, but how long it takes.

                    It takes an instant to understand
                    const TCHAR* b

                    and 15-25 seconds to decrypt
                    LPCTSTR b

                    Tom

                    Comment

                    • r norman

                      #11
                      Re: LPCTSTR to std::string

                      On Wed, 20 Sep 2006 11:15:07 -0700, Tamas Demjen <tdemjen@yahoo. com>
                      wrote:
                      >r norman wrote:
                      >
                      >"LP" means "long pointer", the "long" referring as mentioned to the
                      >days when Intel CPU's used either long (segment-offset) or short
                      >(offset only) pointers. The "C" means constant. And, of course,
                      >"TSTR" means string of TCHAR.
                      >
                      >Sure, one can learn how to decode this information, and be able to deal
                      >with these typedefs. But the question is not whether you can do it or
                      >not, but how long it takes.
                      >
                      >It takes an instant to understand
                      >const TCHAR* b
                      >
                      >and 15-25 seconds to decrypt
                      >LPCTSTR b
                      Compared to learning all the esoterica of Windows and MFC and STL and
                      whatever, that is the least of the problems. If you use Microsoft
                      specific library functions you have to use Microsoft specific
                      terminology. If you use STL, you have to use STL terminology. If you
                      use standard C++ you have to use standard C++ terminology. It is not
                      easy. So don't use LPCTSTR's if you don't like them!


                      Comment

                      Working...