Best non-MFC alternative to CString
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Best non-MFC alternative to CString

  1. #1
    Join Date
    Mar 2010
    Posts
    42

    Best non-MFC alternative to CString

    Hi all,



    I am working on an application that is non-MFC. I need to use variable types that can be accepted by LPCWSTR. Is there a non-MFC alternative to using CString? I searched around the net and this is what I think is the best alternative I found:



    (1) Use wstring

    (2) Convert wstring to LPCWSTR



    Something like this code:


    std::wstring s2ws(const std::string& s)
    {
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
    }

    std::wstring stemp = s2ws(myString);
    LPCWSTR result = stemp.c_str();


    Or are there any better alternatives besides this?

    Also, if you know any good tutorial about the different kinds of strings in C++, please tell me also so that I know more about them.

    Thank you!

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Best non-MFC alternative to CString

    Yes, outside of MFC you want to stick to std::string and std::wstring

  3. #3
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,308

    Re: Best non-MFC alternative to CString

    You don't need MFC to use CString, just include atlstr.h

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Best non-MFC alternative to CString

    Adding to Arjay:
    Use CStringA for ANSI, CStringW for Unicode. CString would be translated to either of these depending on project settings.
    You can always convert from CString[A] to CString[W].

    Use CStrBuf[A|W] to get C-style strings, which are de-allocated by CStrBuf's destructor.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center