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. copy LPSTR to LPWSTR

copy LPSTR to LPWSTR

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 6 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.
  • K Offline
    K Offline
    Kiran Pinjala
    wrote on last edited by
    #1

    How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data. Thank you.

    Saadhinchaali

    S CPalliniC C N 4 Replies Last reply
    0
    • K Kiran Pinjala

      How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data. Thank you.

      Saadhinchaali

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      Saadhinchaali wrote:

      How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data.

      You sure about that? Such an attempt will cause a compiler error.

      LPCSTR pChar = "LPCSTR";
      LPCWSTR pWChar = L"LPCWSTR";
       
      pWChar = pChar; // error C2440: '=' : cannot convert from 'const char *' to 'const unsigned short *'

      Even if that made sense, which it doesn't, it would only copy the pointers. Don't tell me you cast the problem away! Show us some code.

      Steve

      K 1 Reply Last reply
      0
      • K Kiran Pinjala

        How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data. Thank you.

        Saadhinchaali

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

        Saadhinchaali wrote:

        How can i copy a string (LPSTR) to a wide string variable (LPWSTR).

        They are quite different beasts, do you need to convert. :)

        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
        • S Stephen Hewitt

          Saadhinchaali wrote:

          How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data.

          You sure about that? Such an attempt will cause a compiler error.

          LPCSTR pChar = "LPCSTR";
          LPCWSTR pWChar = L"LPCWSTR";
           
          pWChar = pChar; // error C2440: '=' : cannot convert from 'const char *' to 'const unsigned short *'

          Even if that made sense, which it doesn't, it would only copy the pointers. Don't tell me you cast the problem away! Show us some code.

          Steve

          K Offline
          K Offline
          Kiran Pinjala
          wrote on last edited by
          #4

          Stephen Hewitt wrote:

          LPCSTR pChar = "LPCSTR"; LPCWSTR pWChar = L"LPCWSTR"; pWChar = pChar; // error C2440: '=' : cannot convert from 'const char *' to 'const unsigned short *'

          sorry. I casted the problem away

          LPCWSTR pWChar = (LPWSTR) pChar

          Saadhinchaali

          S 1 Reply Last reply
          0
          • K Kiran Pinjala

            Stephen Hewitt wrote:

            LPCSTR pChar = "LPCSTR"; LPCWSTR pWChar = L"LPCWSTR"; pWChar = pChar; // error C2440: '=' : cannot convert from 'const char *' to 'const unsigned short *'

            sorry. I casted the problem away

            LPCWSTR pWChar = (LPWSTR) pChar

            Saadhinchaali

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Saadhinchaali wrote:

            sorry. I casted the problem away

            Don't! The compiler issues an error for a reason! Calling a dog a cat doesn't make it one!

            Steve

            CPalliniC D 2 Replies Last reply
            0
            • S Stephen Hewitt

              Saadhinchaali wrote:

              sorry. I casted the problem away

              Don't! The compiler issues an error for a reason! Calling a dog a cat doesn't make it one!

              Steve

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

              Actually the compiler is happy about it (I suspect it enjoys such mistakes...). :-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

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • K Kiran Pinjala

                How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data. Thank you.

                Saadhinchaali

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

                I suggest you try to understand the concepts that are behind by reading this series of articles[^]. At least, you will understand why you can't cast the pointers directly.

                Cédric Moonen Software developer
                Charting control [v1.4 - Updated]

                1 Reply Last reply
                0
                • K Kiran Pinjala

                  How can i copy a string (LPSTR) to a wide string variable (LPWSTR). When i am assigning directly (=) i am loosing some data. Thank you.

                  Saadhinchaali

                  N Offline
                  N Offline
                  Nitheesh George
                  wrote on last edited by
                  #8

                  Hi, U cannot directly cast a LPSTR to Widechar LPWSTR and if u do that u will lose data. use API MultiByteToWideChar to do it. thanks Nitheesh

                  CPalliniC 1 Reply Last reply
                  0
                  • N Nitheesh George

                    Hi, U cannot directly cast a LPSTR to Widechar LPWSTR and if u do that u will lose data. use API MultiByteToWideChar to do it. thanks Nitheesh

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

                    Actually you don't loose any data. Anyway your conclusion is correct: there must be a conversion to get a corrensponding string as result. :)

                    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
                    • S Stephen Hewitt

                      Saadhinchaali wrote:

                      sorry. I casted the problem away

                      Don't! The compiler issues an error for a reason! Calling a dog a cat doesn't make it one!

                      Steve

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

                      Stephen Hewitt wrote:

                      Calling a dog a cat doesn't make it one!

                      Too funny!

                      "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