Click here to Skip to main content
12,760,849 members (30,661 online)
Click here to Skip to main content
Add your own
alternative version

Stats

47.8K views
17 bookmarked
Posted 29 Mar 2011

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

, 29 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

CalicoSkies
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).

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 1 Pin
.:floyd:.27-Feb-16 4:42
member.:floyd:.27-Feb-16 4:42 
GeneralReason for my vote of 3 I don't like voting anybody down, ho... Pin
ThatsAlok3-Jan-12 4:16
memberThatsAlok3-Jan-12 4:16 
GeneralReason for my vote of 1 Bad skill. Pin
ryo-oh-ki5-Dec-11 21:38
memberryo-oh-ki5-Dec-11 21:38 
GeneralReason for my vote of 1 This isnt c and this isnt exceptio... Pin
timfosh7-Nov-11 22:42
membertimfosh7-Nov-11 22:42 
GeneralReason for my vote of 4 complicated but useful Pin
Warrior_093231-Oct-11 19:53
memberWarrior_093231-Oct-11 19:53 
GeneralReason for my vote of 1 Too complicate, not exception safe, ... Pin
Philippe Mori25-Oct-11 14:18
memberPhilippe Mori25-Oct-11 14:18 
GeneralReason for my vote of 1 * malloc is C, not C++ * Bad usage o... Pin
Doc Lobster4-Sep-11 22:28
memberDoc Lobster4-Sep-11 22:28 
GeneralRe: Agreed, STL also provide std::wstring, so you can convert CS... Pin
ThatsAlok3-Jan-12 4:14
memberThatsAlok3-Jan-12 4:14 
GeneralReason for my vote of 2 no need for all this really... Pin
Albert Holguin2-Apr-11 19:49
memberAlbert Holguin2-Apr-11 19:49 
GeneralThe sample could be improved. std::string is guaranteed to s... Pin
Nemanja Trifunovic30-Mar-11 9:19
memberNemanja Trifunovic30-Mar-11 9:19 
GeneralReason for my vote of 1 Why so complicated ? Have a look to ... Pin
jean Davy30-Mar-11 7:57
memberjean Davy30-Mar-11 7:57 
GeneralReason for my vote of 5 Even though the MFC technology is tr... Pin
DrABELL30-Mar-11 6:49
memberDrABELL30-Mar-11 6: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 30 Mar 2011
Article Copyright 2011 by CalicoSkies
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid