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. string conversion

string conversion

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

    Hi All! just wanted to ask, if i have a function which expects a LPCTSTR.. is it ok to call it as shown below func(LPCTSTR lp) { .... } 1) CString str="something"; func(str); 2) LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type func((LPCTSTR)str); thanks! me

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

    ginjikun wrote:

    is it ok to call it as shown below

    Coding schemes aside, yes. If it wasn't, the compiler would surely complain.

    "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

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

    1 Reply Last reply
    0
    • G ginjikun

      Hi All! just wanted to ask, if i have a function which expects a LPCTSTR.. is it ok to call it as shown below func(LPCTSTR lp) { .... } 1) CString str="something"; func(str); 2) LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type func((LPCTSTR)str); thanks! me

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #3
      1. one is OK. 2) is bad, since it will work only on UNICODE builds. it is better:

      LPTSTR str = _T("something");
      func(str);

      BTW: The L prefix does the magic for wide character literals, for instance

      LPWSTR str = L"something"

      :)

      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

      In testa che avete, signor di Ceprano?

      G 1 Reply Last reply
      0
      • G ginjikun

        Hi All! just wanted to ask, if i have a function which expects a LPCTSTR.. is it ok to call it as shown below func(LPCTSTR lp) { .... } 1) CString str="something"; func(str); 2) LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type func((LPCTSTR)str); thanks! me

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

        ginjikun wrote:

        1. CString str="something"; func(str);

        ok.

        ginjikun wrote:

        1. LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type

        LPWSTR str = L"something"; LPCWSTR str = L"something";

        ginjikun wrote:

        func((LPCTSTR)str);

        requires care. in this case if your project is unicode, it is safe.

        G 1 Reply Last reply
        0
        • R Rajkumar R

          ginjikun wrote:

          1. CString str="something"; func(str);

          ok.

          ginjikun wrote:

          1. LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type

          LPWSTR str = L"something"; LPCWSTR str = L"something";

          ginjikun wrote:

          func((LPCTSTR)str);

          requires care. in this case if your project is unicode, it is safe.

          G Offline
          G Offline
          ginjikun
          wrote on last edited by
          #5

          hi Rajkumar R, what if the project is compile with _MBCS? will it still be ok? thanks

          R 1 Reply Last reply
          0
          • CPalliniC CPallini
            1. one is OK. 2) is bad, since it will work only on UNICODE builds. it is better:

            LPTSTR str = _T("something");
            func(str);

            BTW: The L prefix does the magic for wide character literals, for instance

            LPWSTR str = L"something"

            :)

            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

            G Offline
            G Offline
            ginjikun
            wrote on last edited by
            #6

            Hi, thanks for the reply! the assignment is just a sample.. however i am more concern with how FUNC is being called. you mentioned it will work only on UNICODE. what if the project is compiled with _MBCS, will FUNC((LPCTSTR)str) still work? thanks! me

            CPalliniC 1 Reply Last reply
            0
            • G ginjikun

              Hi, thanks for the reply! the assignment is just a sample.. however i am more concern with how FUNC is being called. you mentioned it will work only on UNICODE. what if the project is compiled with _MBCS, will FUNC((LPCTSTR)str) still work? thanks! me

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

              if you define _MBCS instead of _UNICODE then FUNC((LPCTSTR)str) expects const char * but you're passing instead a const wchar_t *. the pointer types are quite different and you're forcing the latter into the former with static cast, i.e. mistake. :)

              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

              In testa che avete, signor di Ceprano?

              G 1 Reply Last reply
              0
              • G ginjikun

                hi Rajkumar R, what if the project is compile with _MBCS? will it still be ok? thanks

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

                No, it is not ok. My previous statements is still correct. func((LPCTSTR)str); requires care. in this case if your project is unicode, it is safe.

                ginjikun wrote:

                what if the project is compile with _MBCS? will it still be ok?

                with the project settings, whenever the LPCTSTR is defined as LPCWSTR it is safe, as the str is of type LPWSTR. AFAIK, MBCS is not wide character, so passing wide character to function taking MBCS is not safe. mostly, Wide character and single character is used without any precautions simply using generic text mapping functions, but MBCS requires additional handling like (You must keep track of which bytes are lead bytes).

                1 Reply Last reply
                0
                • G ginjikun

                  Hi All! just wanted to ask, if i have a function which expects a LPCTSTR.. is it ok to call it as shown below func(LPCTSTR lp) { .... } 1) CString str="something"; func(str); 2) LPWSTR str="something"; <-- just a sample, not sure how to set a LPWSTR type func((LPCTSTR)str); thanks! me

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

                  And see The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^].

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    if you define _MBCS instead of _UNICODE then FUNC((LPCTSTR)str) expects const char * but you're passing instead a const wchar_t *. the pointer types are quite different and you're forcing the latter into the former with static cast, i.e. mistake. :)

                    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

                    G Offline
                    G Offline
                    ginjikun
                    wrote on last edited by
                    #10

                    Hi, thanks again. since it is an mfc project i just cast it to CString instead. func((CString)LPWSTR) i think this should be ok... ??? me

                    CPalliniC 1 Reply Last reply
                    0
                    • G ginjikun

                      Hi, thanks again. since it is an mfc project i just cast it to CString instead. func((CString)LPWSTR) i think this should be ok... ??? me

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

                      ginjikun wrote:

                      i think this should be ok... ???

                      That works thanks to temporary CString object's constructor (it perforforms the conversion). :)

                      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

                      In testa che avete, signor di Ceprano?

                      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