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. wstring to LPCTSTR

wstring to LPCTSTR

Scheduled Pinned Locked Moved C / C++ / MFC
performancetutorialquestion
11 Posts 5 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.
  • L Luke Murray

    Hey all. I had a function that simply returned a LPCTSTR like LPCTSTR getSomething() { return L"Hello There"; } Using that in GetClassInfoEx, CreateWindowEx, etc worked fine. Though I tried to change it to a wstring like so wstring getSomething() { return L"Hello There"; } or const wstring getSomething() { return L"Hello There"; } both compiled etc, though I always got memory access errors. I am just wondering why this is so, and what I can do to get around it. Mainly WNDCLASSEX.lpszMenuName = getSomething().c_str() would fall over. Any ideas on how to do this correctlly? I did try casting it as a LPCTSTR as well with no luck. Thanks all

    K Offline
    K Offline
    kakan
    wrote on last edited by
    #2

    Hello. This article might help you: http://www.codeproject.com/string/cppstringguide2.asp[^]

    L 1 Reply Last reply
    0
    • L Luke Murray

      Hey all. I had a function that simply returned a LPCTSTR like LPCTSTR getSomething() { return L"Hello There"; } Using that in GetClassInfoEx, CreateWindowEx, etc worked fine. Though I tried to change it to a wstring like so wstring getSomething() { return L"Hello There"; } or const wstring getSomething() { return L"Hello There"; } both compiled etc, though I always got memory access errors. I am just wondering why this is so, and what I can do to get around it. Mainly WNDCLASSEX.lpszMenuName = getSomething().c_str() would fall over. Any ideas on how to do this correctlly? I did try casting it as a LPCTSTR as well with no luck. Thanks all

      2 Offline
      2 Offline
      224917
      wrote on last edited by
      #3

      L"Hello There"; The above line says "Hello There" is wide character string, this means each character takes two bytes internally. LPCTSTR should be pointed only to multibyte character strings (normal string where one character uses one byte internally). So if you want to change a wide char string to multibyte char string you have to use any of the functions like wcstombs/WideCharToMultiByte ...


      suhredayan
      -- modified at 11:46 Tuesday 15th November, 2005

      S 1 Reply Last reply
      0
      • 2 224917

        L"Hello There"; The above line says "Hello There" is wide character string, this means each character takes two bytes internally. LPCTSTR should be pointed only to multibyte character strings (normal string where one character uses one byte internally). So if you want to change a wide char string to multibyte char string you have to use any of the functions like wcstombs/WideCharToMultiByte ...


        suhredayan
        -- modified at 11:46 Tuesday 15th November, 2005

        S Offline
        S Offline
        sunit5
        wrote on last edited by
        #4

        but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise

        T 2 2 Replies Last reply
        0
        • S sunit5

          but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise

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

          sunit5 wrote:

          but LPCTSTR is compatible to both Unicode as well as ANSI

          yes, but not the two ones at the same time... if UNICODE is not defined, LPCTSTR equals to a LPCSTR (which is a conct char*) and so, writing a wstring in it is a mistake !!


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          1 Reply Last reply
          0
          • S sunit5

            but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise

            2 Offline
            2 Offline
            224917
            wrote on last edited by
            #6

            sunit5 wrote:

            but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise

            Sunit5, You are right. Thanks for correcting.


            suhredayan
            There is no spoon.

            L 1 Reply Last reply
            0
            • 2 224917

              sunit5 wrote:

              but LPCTSTR is compatible to both Unicode as well as ANSI An LPCWSTR if UNICODE is defined, an LPCSTR otherwise

              Sunit5, You are right. Thanks for correcting.


              suhredayan
              There is no spoon.

              L Offline
              L Offline
              Luke Murray
              wrote on last edited by
              #7

              Unicode is defined, so the methods are expecting LPCWSTR not LPCSTR and the LPCTSTR is typedef of LPCWSTR. So going from wstring to LPCTSTR with unicode defined should be fine, right?

              2 1 Reply Last reply
              0
              • K kakan

                Hello. This article might help you: http://www.codeproject.com/string/cppstringguide2.asp[^]

                L Offline
                L Offline
                Luke Murray
                wrote on last edited by
                #8

                Thanks, that looks like it may help. I'll have a read tonight

                1 Reply Last reply
                0
                • L Luke Murray

                  Unicode is defined, so the methods are expecting LPCWSTR not LPCSTR and the LPCTSTR is typedef of LPCWSTR. So going from wstring to LPCTSTR with unicode defined should be fine, right?

                  2 Offline
                  2 Offline
                  224917
                  wrote on last edited by
                  #9

                  WNDCLASSEX.lpszMenuName = getSomething().c_str(); in the above line wstring returned is destroyed as soon as the execution passes to the next line. So the pointer to the wstring is invalid after that. But the WNDCLASSEX unaware of this still try to access this pointer. Also from the function getSomething()use the following syntax to return: return wstring(L"my wstring");


                  suhredayan
                  There is no spoon.

                  S 1 Reply Last reply
                  0
                  • 2 224917

                    WNDCLASSEX.lpszMenuName = getSomething().c_str(); in the above line wstring returned is destroyed as soon as the execution passes to the next line. So the pointer to the wstring is invalid after that. But the WNDCLASSEX unaware of this still try to access this pointer. Also from the function getSomething()use the following syntax to return: return wstring(L"my wstring");


                    suhredayan
                    There is no spoon.

                    S Offline
                    S Offline
                    sunit5
                    wrote on last edited by
                    #10

                    wstring wstrMenuName=getSomething().c_str(); WNDCLASSEX.lpszMenuName=(LPCTSTR)wstrMenuName; //remember dont call the destructor of wstrMenuName till the work is not finished

                    L 1 Reply Last reply
                    0
                    • S sunit5

                      wstring wstrMenuName=getSomething().c_str(); WNDCLASSEX.lpszMenuName=(LPCTSTR)wstrMenuName; //remember dont call the destructor of wstrMenuName till the work is not finished

                      L Offline
                      L Offline
                      Luke Murray
                      wrote on last edited by
                      #11

                      Thank you guys that worked great. Thanks all

                      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