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. SetDlgItemTextW problem

SetDlgItemTextW problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
15 Posts 6 Posters 1 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 Suneet 03

    I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

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

    I guess UNICODE is not defined else

    Suneet.03 wrote:

    CString str="Name";

    will not compile.

    T 1 Reply Last reply
    0
    • S Suneet 03

      I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

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

      Suneet.03 wrote:

      CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf);

      CString, strcpy() are not handling unicode... use CStringW and wcscpy_s() instead, and of course, when using literals, type them with a prepending **L**


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      J 1 Reply Last reply
      0
      • S Sam_c

        CString str=_T("name"); SetDlgItemText(IDC_BTN_RESET,str); works fine :) does that solve it?

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

        sonsam wrote:

        CString str=_T("name");

        no. this instead :

        CString str = L"Name";


        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        1 Reply Last reply
        0
        • S spsharma

          I guess UNICODE is not defined else

          Suneet.03 wrote:

          CString str="Name";

          will not compile.

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

          spsharma wrote:

          CString str="Name"; will not compile

          :confused: sure it will, and it does ! but it doesn't work properly if UNICODE and _UNICODE are not defined...


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          S 1 Reply Last reply
          0
          • T toxcct

            spsharma wrote:

            CString str="Name"; will not compile

            :confused: sure it will, and it does ! but it doesn't work properly if UNICODE and _UNICODE are not defined...


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            S Offline
            S Offline
            spsharma
            wrote on last edited by
            #8

            In fact it will not compile if UNICODE is defined.

            1 Reply Last reply
            0
            • T toxcct

              Suneet.03 wrote:

              CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf);

              CString, strcpy() are not handling unicode... use CStringW and wcscpy_s() instead, and of course, when using literals, type them with a prepending **L**


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

              The data is not Unicode until the call to mbstowcs(...) at which point buf (a Unicode/wide character buffer) should contain Unicode data.  At that point, the CString and strcpy(...) are no longer involved.    Most ANSI/MBCS applications are capable of handling Unicode data and vice-versa simpy by using/omitting the L prefix on strings to indicate a wide/narrow string and using the appropriate W/A suffix on the Win32 API.  Your application configuration (UNICODE #defined or not) mostly indicates the default character width and version of the Win32 API to link with.    There is something else going on...    Peace! -- modified at 7:49 Monday 16th July, 2007 (Posted before complete...)

              -=- James
              Please rate this message - let me know if I helped or not! * * * 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!
              See DeleteFXPFiles

              1 Reply Last reply
              0
              • S Suneet 03

                I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

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

                Assuming that pc_char is a suitable char buffer, the conversion to Unicode looks OK from here.    I would try a few things...      1: Remove the CString usage - it is not necessary and only adds time and another point for exception handling.

                LPCTSTR pcData = "Name";
                wchar_t wcaBuf[255];

                ::mbstowcs( wcaBuf, cpData, ::_tcslen( cpData ) );
                ::SetDlgItemTextW( hWnd, IDC_VIEW , wcaBuf );
                // HWND hWndView = ::GetDlgItem( hWnd, IDC_VIEW ); //::SetWindowTextW( hWndView, wcaBuf );

                2: Make sure that the conversion to Unicode was successful by checking the return value from mbstowcs(...)      3: Check the return value of SetDlgItemTextW(...), and maybe try using SetWindowTextW(...) by first obtaining the HWND of the IDC_VIEW control.      4: This may be a stupid question, but are you sure that IDC_VIEW is a "text-able" control (edit, button, static/label, drop-list combobox)?    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * 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!
                See DeleteFXPFiles

                1 Reply Last reply
                0
                • S Suneet 03

                  I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #11

                  Wow. Alot of discussion and 1-voting on this thread :) Isn't this sufficient? CStringW str=L"Name"; SetDlgItemTextW(hWnd,IDC_VIEW,str);

                  Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                  1 Reply Last reply
                  0
                  • S Suneet 03

                    I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #12

                    or this CStringW str("Name"); SetDlgItemTextW(hWnd,IDC_VIEW,str);

                    Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                    J 1 Reply Last reply
                    0
                    • M Mark Salsbery

                      or this CStringW str("Name"); SetDlgItemTextW(hWnd,IDC_VIEW,str);

                      Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

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

                      Or this:

                      SetDlgItemTextW( hWnd, IDC_VIEW, L"Name" );

                      :P    Peace!

                      -=- James
                      Please rate this message - let me know if I helped or not! * * * 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!
                      See DeleteFXPFiles

                      M 1 Reply Last reply
                      0
                      • J James R Twine

                        Or this:

                        SetDlgItemTextW( hWnd, IDC_VIEW, L"Name" );

                        :P    Peace!

                        -=- James
                        Please rate this message - let me know if I helped or not! * * * 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!
                        See DeleteFXPFiles

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #14

                        :doh: ;P

                        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                        1 Reply Last reply
                        0
                        • S Suneet 03

                          I am using VC++ and want to display UNICODE String on a Dialog box Doing it like this : CString str="Name"; wchar_t buf[255]; strcpy(pc_char1,str); mbstowcs(buf,pc_char1,str.GetLength()+1); SetDlgItemTextW(hWnd,IDC_VIEW,buf); and HWND hWnd is declared in .h File The problem is even though code compiles fine but nothing gets displayed.

                          S Offline
                          S Offline
                          Suneet 03
                          wrote on last edited by
                          #15

                          Thanx guys for such quick replies. One more thing , is UTF-8 conversion same as UNICODE conversion? i think its same ...

                          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