The meaning of the commonly used string data type representations under MFC:
L:long P: Pointpointer C:CONST constant W:wchar_t wide character T:TCHAR STR: Stringstring
Look at the definitions of various data types under MFC:
1 typedefchar *LPSTR;2 typedefConstchar *LPCSTR;3LPWSTR; const wchar_t * LPCWSTR; 6 7 8 #ifdef UNICODE 9 Span style= "color: #000000;" > typedef LPCWSTR LPCTSTR; 10 One 12 #else 13 typedef LPCSTR LPCTSTR; 14 typedef char TCHAR;
ATL conversion macros provided under MFC:
T2A t2w t2ca t2cw w2a w2ca w2t w2ct a2w A2CW a2t a2ct
T:TCHAR 2: ToC:CONST A:CHAR (ANSI) W:WCHAR
Go to the Atlconv.h header file and look at the definitions of these macros,
1.uses_conversion: note int _convert; (_convert); This usage is to mask the compiler's warning: unreferenced local variables ...
#ifndef _DEBUG#define USES_CONVERSION int _convert; (_convert); UINT _ACP = ATL::_ATLGETCONVERSIONACP ()/*cp_thread_acp*/; (_ACP); LPCWSTR _LPW; (_LPW); LPCSTR _lpa; (_LPA) #else#define USES_CONVERSION int _convert = 0; (_convert); UINT _ACP = ATL::_ATLGETCONVERSIONACP ()/*cp_thread_acp*/; (_ACP); LPCWSTR _LPW = NULL; (_LPW); LPCSTR _lpa = NULL; (_LPA) #endif
As you can see, the Uses_conversion macro defines some of the variables required for conversion, such as _convert. So when you use those macros, you must add the macro.
2.A2W: The _convert variable defined by the Uses_conversion macro is used here
#define A2W (LPA) (2 ((_lpa = lpa) = = NULL)? NULL: (3 _convert = (Lstrlena (_LPA) +1),4 (int_max/2<_convert)? 5 Atla2whelper ((LPWSTR) alloca (_convert*sizeof (WCHAR)), _lpa, _convert, _ACP)))
3.atla2whelper
1 #define Atla2whelper atla2whelper
4.atla2whelper: Actually still calling MultiByteToWideChar, that is, multi-byte conversion to wide-byte
1 _ret_opt_z_cap_ (nchars) inline lpwstr WINAPI atla2whelper (_OUT_Z_CAP_ (nchars) lpwstr LPW, _in_z_ LPCSTR LPA, _in_int Nchars, _in_ UINT ACP)Throw()2{3 Atlassert (LPA! =NULL);4 Atlassert (LPW! =NULL);5if (LPW = = NULL | | lpa = =NULL)6ReturnNULL;7 *LPW =‘/‘ 8 int ret = MultiByteToWideChar (ACP, 0, LPA,-1if (ret = = 010 {11 Atlassert ( FALSE); 12 return Null;13 14 return LPW; 15}
5.alloca
#define ALLOCA _alloca
6._alloca: The function allocates memory in bytes from the heap, returning void *
void * 3);
Examples of Use methods:
1 uses_conversion; Uses_conversion is a macro definition in ATL that defines some of the local variables required for converting macros 3 WCHAR *pwch = L"wchar* to char*" , which is used for encoding conversions; 4 CHAR *pch = w2a (pwch); 6 TCHAR *ptch = _t ("tchar* to char*"); 7 CHAR *pch2 = T2A (ptch);
Use uses_conversion be careful, they allocate memory from the stack until the function that calls it returns, and the memory is not freed. If in a loop, this macro is repeatedly called tens of thousands of times, it will inevitably produce stackoverflow.
But consider the size of the stack space (the default 2M), the use of a few points to note:
1, only suitable for the conversion of short strings;
2, do not try to be in a more than a number of cycles in the body to convert;
3, do not attempt to convert the character file content, because the file size is generally relatively large;
4. For cases 2 and 3, use MultiByteToWideChar () and WideCharToMultiByte ();
See http://www.cnblogs.com/wind-net/archive/2012/10/31/2718329.html
5. Package character conversion into function
void doa2w (){ uses_conversion; 4 dosomething (a2w ("somestring")); 5}
In addition to this, you can also use the C language <stdlib.h> provided functions:
1char * pstr ="Abc";23 size_t Nlen = _mbstrlen (pstr) +1;4//You can also use Mbstowcs (NULL, pstr, 0), and the first and third arguments are empty to get the length of a non-null character56 wchar_t * Pwstr =NewWchar_t[nlen];7 mbstowcs (Pwstr, Pstr, Nlen);//Multibyte string to Widechar string89 size_t Nwlen = Wcslen (PWSTR) +s; //wcstombs (NULL, pwstr, 0)ten char *pstr2 = new char[Nwlen]; Wcstombs (PSTR2, Pwstr, Nwlen); //Widechar string to multibyte string , delete []pwstr; Pwstr = NULL; Delete []pstr2; PSTR2 = NULL;
Various string types and mutual conversions under MFC