Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / MFC
Tip/Trick

C++: Converting an MFC CString to a std::string

Rate me:
Please Sign up or sign in to vote.
3.26/5 (21 votes)
29 Mar 2011CPOL 68.2K   17   12
C++: Converting an MFC CString to a std::string
MFC includes its own string class, CString. I have sometimes found it useful to convert a CString to a standard C++ string (std::string). This is a function I wrote to do so:
std::string CStringToSTDStr(const CString& theCStr)
{
	// Convert the CString to a regular char array
	const int theCStrLen = theCStr.GetLength();
	char *buffer = (char*)malloc(sizeof(char)*(theCStrLen+1));
	memset((void*)buffer, 0, sizeof(buffer));
	WideCharToMultiByte(CP_UTF8, 0, static_cast<cstring>(theCStr).GetBuffer(), theCStrLen, buffer, sizeof(char)*(theCStrLen+1), NULL, NULL);
	// Construct a std::string with the char array, free the memory used by the char array, and
	// return the std::string object.
	std::string STDStr(buffer);
	free((void*)buffer);
	return STDStr;
}</cstring>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I was born and raised (and currently still reside in) northwest Oregon, USA. I started using computers at a very young age, maybe 4 or 5 years old, using my dad's computers. I got my own computer when I was 12 (1992). Later, I realized that I really enjoy programming, and in college, I decided to get a degree in software engineering. In 2005, I earned my bachelor's degree in software engineering from Oregon Institute of Technology.

I have been working as a software engineer since October 2003. So far, I've had experience with C++, PHP, JavaScript, Perl, Bash shell scripting, C#, and HTML. I have developed GUIs with MFC, wxWidgets, and WinForms. I also have experience with open-source technologies such as Linux, Apache, MySQL, and PostgreSQL. My favorite Linux text editor is Vim.

In my personal time I enjoy tinkering with computers (building PCs, gaming, occasionally working on personal programming projects), playing music (I enjoy playing guitar and synthesizer/piano), and spending time with family & friends. I also enjoy reading the occasional book (pretty much anything with a good story, but usually sci-fi, drama, programming books, advice books, and scientific non-fiction, such as books written by physicist Michio Kaku). Currently, my favorite TV show is The Office; I also like watching the news when I can. I also enjoy the occasional movie (sci-fi, comedy, action, drama; pretty much anything with a good story).

Comments and Discussions

 
GeneralMy vote of 1 Pin
.:floyd:.27-Feb-16 3:42
.:floyd:.27-Feb-16 3:42 
GeneralReason for my vote of 3 I don't like voting anybody down, ho... Pin
ThatsAlok3-Jan-12 3:16
ThatsAlok3-Jan-12 3:16 
GeneralReason for my vote of 1 Bad skill. Pin
raspopov5-Dec-11 20:38
raspopov5-Dec-11 20:38 
GeneralReason for my vote of 1 This isnt c and this isnt exceptio... Pin
timfosh7-Nov-11 21:42
professionaltimfosh7-Nov-11 21:42 
GeneralReason for my vote of 4 complicated but useful Pin
Warrior_093231-Oct-11 18:53
Warrior_093231-Oct-11 18:53 
GeneralReason for my vote of 1 Too complicate, not exception safe, ... Pin
Philippe Mori25-Oct-11 13:18
Philippe Mori25-Oct-11 13:18 
GeneralReason for my vote of 1 * malloc is C, not C++ * Bad usage o... Pin
Doc Lobster4-Sep-11 21:28
Doc Lobster4-Sep-11 21:28 
Reason for my vote of 1
* malloc is C, not C++
* Bad usage of WideCharToMultiByte(...) - should be called twice, first time to evaluate output buffer size
* Assumes CString is wide-char
* std::string is usually host encoding (on windows some ansi cp), not UTF-8
* Missing exception handling may result in memory leaks

For such a common task as string conversion I expect a better solution
GeneralRe: Agreed, STL also provide std::wstring, so you can convert CS... Pin
ThatsAlok3-Jan-12 3:14
ThatsAlok3-Jan-12 3:14 
GeneralReason for my vote of 2 no need for all this really... Pin
Albert Holguin2-Apr-11 18:49
professionalAlbert Holguin2-Apr-11 18:49 
GeneralThe sample could be improved. std::string is guaranteed to s... Pin
Nemanja Trifunovic30-Mar-11 8:19
Nemanja Trifunovic30-Mar-11 8:19 
GeneralReason for my vote of 1 Why so complicated ? Have a look to ... Pin
jean Davy30-Mar-11 6:57
jean Davy30-Mar-11 6:57 
GeneralReason for my vote of 5 Even though the MFC technology is tr... Pin
DrABELL30-Mar-11 5:49
DrABELL30-Mar-11 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.