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. TCHAR issue

TCHAR issue

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorial
12 Posts 7 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.
  • G goldenrose9

    In Win32 C++ how to convert

    TCHAR* to LPCTSTR
    TCHAR to LPTSTR

    Please help...:confused::confused:

    gold

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

    What do you mean by convert? TCHAR* is merely a pointer to a char or wchar_t depending on the definition of UNICODE. LPCTSTR is a typedef to const TCHAR* so no conversion is necessary; but you may need a cast to satisfy the compiler. TCHAR is a single character so cannot be converted or cast to LPTSTR; if you actually meant TCHAR* then both expressions evaluate to the same thing: a pointer to a character or wide character.

    Just say 'NO' to evaluated arguments for diadic functions! Ash

    G 1 Reply Last reply
    0
    • L Lost User

      What do you mean by convert? TCHAR* is merely a pointer to a char or wchar_t depending on the definition of UNICODE. LPCTSTR is a typedef to const TCHAR* so no conversion is necessary; but you may need a cast to satisfy the compiler. TCHAR is a single character so cannot be converted or cast to LPTSTR; if you actually meant TCHAR* then both expressions evaluate to the same thing: a pointer to a character or wide character.

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      G Offline
      G Offline
      goldenrose9
      wrote on last edited by
      #4

      Thanx, my one doubt is cleared, about TCHAR to LPTSTR My Program deals with UNICODE only a small example will very helpfull for me, of how to convert TCHAR* to LPCTSTR and LPTSTR. Thanx in advance.

      gold

      L 1 Reply Last reply
      0
      • G goldenrose9

        In Win32 C++ how to convert

        TCHAR* to LPCTSTR
        TCHAR to LPTSTR

        Please help...:confused::confused:

        gold

        R Offline
        R Offline
        Roger Broomfield
        wrote on last edited by
        #5

        TCHAR* to LPCTSTR is a simple typecast

        TCHAR* pch;
        LPCTSTR pcch = (LPCTSTR)pch;

        TCHAR to LPTSTR is an address of operator

        TCHAR ch;
        LPTSTR pch = &ch;

        G 1 Reply Last reply
        0
        • G goldenrose9

          Thanx, my one doubt is cleared, about TCHAR to LPTSTR My Program deals with UNICODE only a small example will very helpfull for me, of how to convert TCHAR* to LPCTSTR and LPTSTR. Thanx in advance.

          gold

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

          goldenrose9 wrote:

          a small example will very helpfull for me, of how to convert TCHAR* to LPCTSTR and LPTSTR.

          There is no conversion involved; as I said in my previous post TCHAR* and LPTSTR equate to the same thing, a pointer to a string (character array). LPCTSTR is the same as const TCHAR*, a pointer to a string constant.

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          1 Reply Last reply
          0
          • R Roger Broomfield

            TCHAR* to LPCTSTR is a simple typecast

            TCHAR* pch;
            LPCTSTR pcch = (LPCTSTR)pch;

            TCHAR to LPTSTR is an address of operator

            TCHAR ch;
            LPTSTR pch = &ch;

            G Offline
            G Offline
            goldenrose9
            wrote on last edited by
            #7

            LPTSTR __stdcall gHelp(TCHAR value)
            {
            LPTSTR pch;
            pch = &value;
            return pch;
            }

            compiler gives warning
            warning C4090: 'return' : different 'const' qualifiers

            gold

            L S 2 Replies Last reply
            0
            • G goldenrose9

              In Win32 C++ how to convert

              TCHAR* to LPCTSTR
              TCHAR to LPTSTR

              Please help...:confused::confused:

              gold

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

              Here[^] is an excellent article explaining everything you need to understand C++ strings.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • G goldenrose9

                LPTSTR __stdcall gHelp(TCHAR value)
                {
                LPTSTR pch;
                pch = &value;
                return pch;
                }

                compiler gives warning
                warning C4090: 'return' : different 'const' qualifiers

                gold

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

                You will also have a problem that you are returning a TCHAR* to a TCHAR value, not an array (i.e. string). Do not do this or you will end up with memory overwrites and/or invalid data. As to your compiler warning, you must have a definition of gHelp which reads something like:

                LPCTSTR __stdcall gHelp(TCHAR value);

                Just say 'NO' to evaluated arguments for diadic functions! Ash

                1 Reply Last reply
                0
                • G goldenrose9

                  In Win32 C++ how to convert

                  TCHAR* to LPCTSTR
                  TCHAR to LPTSTR

                  Please help...:confused::confused:

                  gold

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

                  goldenrose9 wrote:

                  TCHAR* to LPCTSTR

                  Usually, no conversion needed. If a function is promising you that it won't modify the contents (thereby expecting an LPCTSTR), you can still pass on a normal TCHAR* to it.

                  goldenrose9 wrote:

                  TCHAR to LPTSTR

                  That makes absolutely no sense. TCHAR is a character (wide or not depends on the build), and LPTSTR is a pointer to one such character. How can you convert one to another?!

                  It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                  G 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    goldenrose9 wrote:

                    TCHAR* to LPCTSTR

                    Usually, no conversion needed. If a function is promising you that it won't modify the contents (thereby expecting an LPCTSTR), you can still pass on a normal TCHAR* to it.

                    goldenrose9 wrote:

                    TCHAR to LPTSTR

                    That makes absolutely no sense. TCHAR is a character (wide or not depends on the build), and LPTSTR is a pointer to one such character. How can you convert one to another?!

                    It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

                    G Offline
                    G Offline
                    goldenrose9
                    wrote on last edited by
                    #11

                    thanx a lot.

                    gold

                    1 Reply Last reply
                    0
                    • G goldenrose9

                      LPTSTR __stdcall gHelp(TCHAR value)
                      {
                      LPTSTR pch;
                      pch = &value;
                      return pch;
                      }

                      compiler gives warning
                      warning C4090: 'return' : different 'const' qualifiers

                      gold

                      S Offline
                      S Offline
                      Stefan_Lang
                      wrote on last edited by
                      #12

                      This won't work. You are passing the TCHAR variable by value, so the parameter value is just a temporary local copy, and the address of it will be invalidated upon return! Pass the TCHAR parameter by reference instead, like this:

                      LPTSTR __stdcall gHelp(TCHAR& value)
                      ...

                      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