Click here to Skip to main content
Click here to Skip to main content

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

By , 29 Mar 2011
 
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)

About the Author

Nightfox
United States United States
Member
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).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralReason for my vote of 3 I don't like voting anybody down, ho...memberThatsAlok3 Jan '12 - 3:16 
GeneralReason for my vote of 1 Bad skill.memberryo-oh-ki5 Dec '11 - 20:38 
GeneralReason for my vote of 1 This isnt c and this isnt exceptio...membertimfosh7 Nov '11 - 21:42 
GeneralReason for my vote of 4 complicated but usefulmemberWarrior_093231 Oct '11 - 18:53 
GeneralReason for my vote of 1 Too complicate, not exception safe, ...memberPhilippe Mori25 Oct '11 - 13:18 
GeneralReason for my vote of 1 * malloc is C, not C++ * Bad usage o...memberDoc Lobster4 Sep '11 - 21:28 
GeneralRe: Agreed, STL also provide std::wstring, so you can convert CS...memberThatsAlok3 Jan '12 - 3:14 
GeneralReason for my vote of 2 no need for all this really...memberAlbert Holguin2 Apr '11 - 18:49 
GeneralThe sample could be improved. std::string is guaranteed to s...memberNemanja Trifunovic30 Mar '11 - 8:19 
GeneralReason for my vote of 1 Why so complicated ? Have a look to ...memberjean Davy30 Mar '11 - 6:57 
GeneralReason for my vote of 5 Even though the MFC technology is tr...memberDrABELL30 Mar '11 - 5:49 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 30 Mar 2011
Article Copyright 2011 by Nightfox
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid