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. std::string to const WCHAR*

std::string to const WCHAR*

Scheduled Pinned Locked Moved C / C++ / MFC
questionwinformsgraphics
30 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.
  • S Stick

    No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.

    G Offline
    G Offline
    George L Jackson
    wrote on last edited by
    #19

    Have you tried "mbstowcs" or "mbstowcs_s" in stdlib.h. std::string mbstr = "The string!"; std::vector<wchar_t> wcstr(mbstr.size() + 1, L'\0'); size_t written = 0; mbstate_t state = {0}; const char* pmbstr = mbstr.c_str(); errno_t result = mbsrtowcs_s(&written, &wcstr[0], wcstr.size(), &pmbstr, mbstr.size(), &state); std::wcout << &wcstr[0] << std::endl; -- modified at 20:19 Thursday 28th September, 2006

    1 Reply Last reply
    0
    • J Jorgen Sigvardsson

      James R. Twine wrote:

      while casting it to WCHAR* will allow the code to compile, it will not work correctly.

      From the context, I think he meant casting the entire string, and not the pointer. :)

      -- Mr. Bender's Wardrobe by ROBOTANY 500

      J Offline
      J Offline
      James R Twine
      wrote on last edited by
      #20

      You mean to create a temporary, as in wstring( sTheString.c_str() )?  I do not think that will work, either...  Unless I am missing the intent of your smiley, that is.    Peace!

      -=- James


      If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
      Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
      DeleteFXPFiles & CheckFavorites (Please rate this post!)

      J 1 Reply Last reply
      0
      • J James R Twine

        You mean to create a temporary, as in wstring( sTheString.c_str() )?  I do not think that will work, either...  Unless I am missing the intent of your smiley, that is.    Peace!

        -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #21

        Naw, more likely using CA2CW or something along those lines. My bet's on that David's a bit too experienced to fall for the (wchar_t*)str.c_str() and similar newbie mistakes. :)

        1 Reply Last reply
        0
        • S Stick

          Don't think I can use ATL and not sure how I would do that anyway yet. I'm programming a win32 dll.

          J Offline
          J Offline
          Jorgen Sigvardsson
          wrote on last edited by
          #22

          If you are using a fairly new version of MFC (7, 8), you can use the conversion classes such as CA2W without any hassles. Just include atlconv.h in stdafx.h, and you're set.

          -- Not a substitute for human interaction

          1 Reply Last reply
          0
          • S Stick

            No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.

            J Offline
            J Offline
            Jorgen Sigvardsson
            wrote on last edited by
            #23

            Stick^ wrote:

            WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL);

            Please use MultiByteToWideChar() instead. You are converting from char to wchar_t, right?

            wchar_t buf[20+1] = { 0 };
            MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_ac.c_str(), m_ac.size(), buf, 20);

            should do the trick.

            -- From the Makers of Futurama

            S 1 Reply Last reply
            0
            • J James R Twine

              The const char* returned by c_str() is not a wide string, while casting it to WCHAR* will allow the code to compile, it will not work correctly.    Peace!

              -=- James


              If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              DeleteFXPFiles & CheckFavorites (Please rate this post!)

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

              James R. Twine wrote:

              The const char* returned by c_str() is not a wide string,...

              So what exactly does std::wstring::c_str() return?


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              J 1 Reply Last reply
              0
              • D David Crow

                James R. Twine wrote:

                The const char* returned by c_str() is not a wide string,...

                So what exactly does std::wstring::c_str() return?


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #25

                std::**w**string::c_str() returns a const WCHAR* (const wchar_t*), but not std::string:c_str(), which is what the OP was asking about.    I think that we (I?) are just getting confused about what "width" of string object is in use here.  The OP is dealing with having a std::string (ANSI) object and needs to get a (presumably valid) const WCHAR* (Unicode) out of it.    Since they cannot simply change the strings from std::string to std::wstring, they are stuck with ANSI string objects, and no amount of casting is going to create a valid wide string from the return of std::string:c_str().   They need to call a conversion function (either directly or indirectly) in order to translate the string correctly.    Peace!

                -=- James


                If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                DeleteFXPFiles & CheckFavorites (Please rate this post!)

                D 1 Reply Last reply
                0
                • J James R Twine

                  std::**w**string::c_str() returns a const WCHAR* (const wchar_t*), but not std::string:c_str(), which is what the OP was asking about.    I think that we (I?) are just getting confused about what "width" of string object is in use here.  The OP is dealing with having a std::string (ANSI) object and needs to get a (presumably valid) const WCHAR* (Unicode) out of it.    Since they cannot simply change the strings from std::string to std::wstring, they are stuck with ANSI string objects, and no amount of casting is going to create a valid wide string from the return of std::string:c_str().   They need to call a conversion function (either directly or indirectly) in order to translate the string correctly.    Peace!

                  -=- James


                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

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

                  James R. Twine wrote:

                  std::wstring::c_str() returns a const WCHAR* (const wchar_t*)...

                  Which is why I suggested it as a better solution here.

                  James R. Twine wrote:

                  ...no amount of casting is going to create a valid wide string from the return of std::string:c_str().

                  Agreed. :-O Blunder on my part.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • S Stick

                    No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #27

                    You are compiling with UNICODE defined, so DrawString is mapped to DrawStringW (which takes wide characters). Since you have a string (which are ANSI characters), you need to convert that string to a wstring (or simply to an array of wchar_t's). You'll need to muse MultiByteToWideChar for that (not WideCharToMultiByte).

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    J 1 Reply Last reply
                    0
                    • S Stick

                      No. I'm given a std::string... here's the declaration: string m_ac; Now, if I attempt: m_graphics->DrawString(m_ac, -1, &font, PointF(8,8), NULL, &grnbrush); I get: Error 1 error C2664: 'Gdiplus::Status Gdiplus::Graphics::DrawString(const WCHAR *,INT,const Gdiplus::Font *,const Gdiplus::RectF &,const Gdiplus::StringFormat *,const Gdiplus::Brush *)' : cannot convert parameter 1 from 'std::string' to 'const WCHAR *' so as you can clearly see, the m_ac is NOT already wide. I have tried to do something like this: WCHAR str[20+1] = L""; WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL); but this does not work either. Nor does just m_ac in parameter 3.

                      S Offline
                      S Offline
                      Stick
                      wrote on last edited by
                      #28

                      Thanks all for your help. I'm such an idiot, using the wrong function! I'll try all these good suggestions and do some more reading and see if I can't figure out more.

                      1 Reply Last reply
                      0
                      • J Jorgen Sigvardsson

                        Stick^ wrote:

                        WideCharToMultiByte(CP_ACP, 0, m_ac.c_str(), -1, str, wcslen(str)+1, NULL, NULL);

                        Please use MultiByteToWideChar() instead. You are converting from char to wchar_t, right?

                        wchar_t buf[20+1] = { 0 };
                        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, m_ac.c_str(), m_ac.size(), buf, 20);

                        should do the trick.

                        -- From the Makers of Futurama

                        S Offline
                        S Offline
                        Stick
                        wrote on last edited by
                        #29

                        Thanks. Would help if I followed everyone's directions!

                        1 Reply Last reply
                        0
                        • Z Zac Howland

                          You are compiling with UNICODE defined, so DrawString is mapped to DrawStringW (which takes wide characters). Since you have a string (which are ANSI characters), you need to convert that string to a wstring (or simply to an array of wchar_t's). You'll need to muse MultiByteToWideChar for that (not WideCharToMultiByte).

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          J Offline
                          J Offline
                          James R Twine
                          wrote on last edited by
                          #30

                          Actually, the OP is using the GDI+ classes, and they tend to always accept wide strings, and not have A or W versions of functions that are aliased-to depending on the build.  For example, GDI+'s three versions of the Graphics::DrawString(...) function all take a const WCHAR* as the string type.    Peace!

                          -=- James


                          If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                          Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                          DeleteFXPFiles & CheckFavorites (Please rate this post!)

                          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