将ANSI转换到Unicode
(1)通过L这个宏来实现,例如: CLSIDFromProgID( L"MAPI.Folder",&clsid);
(2)通过MultiByteToWideChar函数实现转换,例如:
char *szProgID = "MAPI.Folder";
WCHAR szWideProgID[128];
CLSID clsid;
long lLen = MultiByteToWideChar(CP_ACP,
0,szProgID,strlen(szProgID),szWideProgID,sizeof(szWideProgID));
szWideProgID[lLen] = '\0';
(3)通过A2W宏来实现,例如:
USES_CONVERSION;
CLSIDFromProgID( A2W(szProgID),&clsid);
将Unicode转换到ANSI
(1)使用WideCharToMultiByte,例如:
// 假设已经有了一个Unicode 串 wszSomeString...
char szANSIString [MAX_PATH];
WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK,
wszSomeString, -1, szANSIString, sizeof(szANSIString), NULL,
NULL );
(2)使用W2A宏来实现,例如:
USES_CONVERSION;
pTemp=W2A(wszSomeString);
近来做练习,又发现两个不错的转换函数。也写下来吧
Converts a sequence of wide characters to a corresponding sequence of multibyte characters.
size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count );Parameters
- mbstr
- The address of a sequence of multibyte characters.
- wcstr
- The address of a sequence of wide characters.
- count
- The maximum number of bytes that can be stored in the multibyte output string.
//88888888888888888888888888888888888888888888888888888888888888888888888888888
Converts a sequence of multibyte characters to a corresponding sequence of wide characters.
size_t mbstowcs( wchar_t* wcstr,
const char* mbstr,
size_t count );Parameters
- wcstr
- The address of a sequence of wide characters.
- mbstr
- The address of a sequence of multibyte characters.
-
count
The number of multibyte characters to convert.