-
May 4th, 2010, 12:28 AM
#1
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!
-
May 4th, 2010, 01:16 AM
#2
Re: Best non-MFC alternative to CString
Yes, outside of MFC you want to stick to std::string and std::wstring
-
May 4th, 2010, 01:22 AM
#3
Re: Best non-MFC alternative to CString
You don't need MFC to use CString, just include atlstr.h
-
May 4th, 2010, 10:29 AM
#4
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|