In Windows programming, conversion between strings is often encountered. Conversion from char * To lpcwstr is also a common conversion. The following lists several commonly used conversion methods.
1. multibytetowidechar Function Conversion
The multibytetowidechar function is an API function that converts multiple bytes into a wide byte. Its prototype is as follows:
int MultiByteToWideChar( UINT CodePage, // code page DWORD dwFlags, // character-type options LPCSTR lpMultiByteStr, // string to map int cbMultiByte, // number of bytes in string LPWSTR lpWideCharStr, // wide-character buffer int cchWideChar // size of buffer);
The lpcwstr type is actually const wchar *.
Char * szstr = "test string"; wchar wszclassname [256]; memset (wszclassname, 0, sizeof (wszclassname); multibytetowidechar (cp_acp, 0, szstr, strlen (szstr) + 1, wszclassname, sizeof (wszclassname)/sizeof (wszclassname [0]);
2. Convert macros with T2W
Char * szstr = "test string"; cstring STR = cstring (szstr); uses_conversion; lpcwstr wszclassname = new wchar [Str. getlength () + 1]; wcscpy (lptstr) wszclassname, T2W (lptstr) Str. getbuffer (null); Str. releasebuffer ();
3. a2cw Conversion
Char * szstr = "test string"; cstring STR = cstring (szstr); uses_conversion; lpcwstr wszclassname = a2cw (w2a (STR); Str. releasebuffer ();
The above methods are all tested in the Unicode environment.