CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    Apr 2005
    Posts
    78

    Question How to assign CString to wstring UNICODE compilation

    I have a project originally compiled ANSI, it now needs to support UNICODE.
    The front end is MFC and some of the backend is std C++ stl.

    Currenty under ASCII compilation I have something like:

    std:string val;
    CString src;

    val = src;

    No problem.

    When compile under UNICODE then have

    std::wstring val
    CString src

    val = src;

    Wont compile.

    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CString' (or there is no acceptable conversion)

    Can anyone help please?
    Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to assign CString to wstring UNICODE compilation

    Quote Originally Posted by PRMARJORAM View Post
    ...
    When compile under UNICODE then have
    ...
    How do you compile "under UNICODE"?
    Did you defined both UNICODE and _UNICODE?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2005
    Posts
    78

    Re: How to assign CString to wstring UNICODE compilation

    Quote Originally Posted by VictorN View Post
    How do you compile "under UNICODE"?
    Did you defined both UNICODE and _UNICODE?
    Im using the Visual studio 2005 compiler, so just switched the setting in the general options to the unicode character set and recompiled.

    Although have edited my String typedef from std::string to std::wstring

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to assign CString to wstring UNICODE compilation

    CString is #ifdef'd to be synonymous to CStringA or to CStringW depending on settings. As victor indicates, this could be the problem.

    Try literally changing yoru CString to CStringW in this particular case and retry. if it solves the issue, you need to get your compile settings corrected, if the error remains, then maybe there isn't an available (automatic) conversion available. In that case, you may need to explicitely typecast every CString to stringw conversion.

  5. #5
    Join Date
    Apr 2005
    Posts
    78

    Cool Re: How to assign CString to wstring UNICODE compilation

    I tried CStringW, made no difference.

    putting a typecast in front of the 'CStringW' also generates an error
    I assume you mean

    std::wstring val
    CStringW src

    val = (std::wstring)src;

    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: How to assign CString to wstring UNICODE compilation

    std::wstring val
    CString src

    val = src.c_str();

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to assign CString to wstring UNICODE compilation

    I guess. you meant:
    Code:
    std::wstring val
    CString src
    
    src= val .c_str();
    And from CStringW to std::wstring:
    Code:
    std::wstring val;
    CStringW src;
    
    val = (LPCWSTR)src;
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 2005
    Posts
    78

    Re: How to assign CString to wstring UNICODE compilation

    Quote Originally Posted by 0xC0000005 View Post
    std::wstring val
    CString src

    val = src.c_str();
    CString not have the c_str() method!

    error C2039: 'c_str' : is not a member of 'ATL::CStringT<BaseType,StringTraits>'

  9. #9
    Join Date
    Apr 2005
    Posts
    78

    Re: How to assign CString to wstring UNICODE compilation

    Thanks seems to be working now.

    In the std C++ libs now, got lots of errors where have changed to wstring.

    Got to put an L in front of any string literals.

    And any str function calls got to be changed to wcs version.

    Cant see we will ever go back to ASCII so dont need to control all this via some preprocessor directive.

    But is this all I need to do? Are there any unforseen problems?


    Thanks for your help.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to assign CString to wstring UNICODE compilation

    Victor Nijegorodov

  11. #11
    Join Date
    Apr 2005
    Posts
    78

    Re: How to assign CString to wstring UNICODE compilation

    Yes that is very helpful thanks.
    This clarifies what i have to do on the MFC side of my application to support UNICODE.

    I also have similar but different problems with some libraries that are pure C++ std and STL.

    I mean in there you have to use L"" whereas in MFC side you use _T("")

    Thanks.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to assign CString to wstring UNICODE compilation

    You can also use the conversion macros.


    Code:
     
    USES_CONVERSION;
     
    std::wstring val
    CString src
    
    val = T2W( src );

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to assign CString to wstring UNICODE compilation

    Quote Originally Posted by PRMARJORAM View Post
    Yes that is very helpful thanks.
    This clarifies what i have to do on the MFC side of my application to support UNICODE.

    I also have similar but different problems with some libraries that are pure C++ std and STL.

    I mean in there you have to use L"" whereas in MFC side you use _T("")

    Thanks.
    This really is more of the VC and windows environment than MFC. The bottom line is that if you use _T("") around string literals, it will work for both. If you use "" or L"", you could run into trouble. Also, consider calling api's that take strings, If you call SomeWinApi( _T("xxx") ), it's going to always work. However, without the _T("") macro, you'll need to explicitly call either the SomeWinApiA (ANSI) or SomeWinApiW (UNICODE) api version.

  14. #14
    Join Date
    Apr 2005
    Posts
    78

    Question Re: How to assign CString to wstring UNICODE compilation

    Can you have UNICODE and ASCII in the same program?

    Actually, my specific problem is only that im downloading webpages. Most are western so no problem, but now some are not. specifically have a webpage using cyrillic text.

    Im displaying a subset of the text from these webpages in a CListCtrl. When its cyrillic nothing is displayed. Im assuming here recompiling it all too UNICODE and then i should see cyrillic text in my CListCtrl? Its only in this instance i really need to use unicode - when displaying text from a non-western website?

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to assign CString to wstring UNICODE compilation

    While you *can* have both ANSI and UNICODE in a single program, I'd recommened that you compile your program for all UNICODE. If you have data coming in (or going out) that's then convert the data to UNICODE immediately so it's in one format throughout your program.

    The main reason for doing this is that the Windows Operating System uses UNICODE internally and any ANSI calls you make, convert the strings to UNICODE and then just call the UNICODE version of the api. So there is extra overhead while interacting with the OS if your program is primarily in ANSI.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured