The reference document is as follows:
L the original article cannot be found because of the widely circulated "cstring skills" written by Daniel on the Internet. The reposted address is as follows::Http://blog.csdn.net/coolstar14/archive/2004/07/15/41885.aspx
L "cstring managment" address in the previous article of codeproject:Http://www.codeproject.com/KB/string/cstringmgmt.aspx
L codeproject FAQ in some of the answers: http://www.codeproject.com/KB/cpp/cppforumfaq.aspx#mfc_cstrtopchar
L description and example of the widechartomultibyte () function in msdn.
Background:
1. Unicode Environment
2. The csmtp class is a class for downloading from the Internet, which enables sending emails using the SMTP protocol. There are many ways to set the account name and password. The corresponding method is csmtp: setlogin (const char *), csmtp: setpassword (const char *), and so on. Because it is a work of someone else, I cannot rewrite the parameter types of these functions.
Problem
1. cstring --> const char *: saves the string entered by the user in the edit control to the control variable of the cstring type. AfterProcessing,Then use this string as the function m_csmtpmail.setlogin (const
Char.
2. Const char * --> lpctstr: Convert the result of capturing the exception function E. geterrortext (). c_str () to lpctstr as a parameter of the MessageBox (lpctstr) function.
Solution:
For the first question:
1: Convert the cstring variable to the Unicode Character pointer, that is, tchar *
2: Use the widechartomultibyte () function to convert Unicode strings to ANSI strings.
3. Pass the converted result string to the setlogin (const char *) function. Based on the parameter type requirements,AutomaticallyChar * to const char *
The Code is as follows:
1 // m_editaccount is the control variable
2 // m_csmtpmail is a csmtp object
3tchar * tempact = m_editaccount.getbuffer ();
4 char mbtempact [256];
5 widechartomultibyte (100, tempact,-1, mbtempact, null, null );
6m_csmtpmail.setlogin (mbtempact );
For the second question:
1. E. geterrortext (). c_str () is actually the first address pointer of a character array and a const char * type. The cstring constructor can easily convert a character pointer to the cstring type.
2. Pass the cstring type to MessageBox (). Based on the parameter type requirements, MFC uses the lpctstr operator,AutomaticallyCstring to the type of lpctstr. The conversion between lpctstr and cstring is so simple, which can be found in many articles on cstring.
3. Compared to the first problem, the conversion process from ANSI encoding to unicode encoding is missing. This should be because the cstring class is well encapsulated. It is automatically completed in the constrfunction cstring str4 (E. geterrortext (). c_str.
The Code is as follows:
1 cstring cstr4 (E. geterrortext (). c_str ());
2 MessageBox (cstr4 );