#include
//在一個函數的循環體中使用A2W等字符轉換宏可能引起棧溢出(內存未釋放)
//參見對應宏的定義
//Ascii -> wchar
USES_CONVERSION
DoSomething(A2W(aStr));
//wchar -> Ascii
USES_CONVERSION
DoSomething(W2A(aStr));
A2W:
//atlconv.h
#define A2W(lpa) (\
((_lpa = lpa) == NULL) ? NULL : (\
_convert = (lstrlenA(_lpa)+1),\
ATLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert)))
#define ATLA2WHELPER AtlA2WHelper
inline LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars, UINT acp)
{
ATLASSERT(lpa != NULL);
ATLASSERT(lpw != NULL);
// verify that no illegal character present
// since lpw was allocated based on the size of lpa
// don't worry about the number of chars
lpw[0] = '\0';
MultiByteToWideChar(acp, 0, lpa, -1, lpw, nChars);
return lpw;
}
//atlconv.h
#define W2A(lpw) (\
((_lpw = lpw) == NULL) ? NULL : (\
(_convert = (ATL::lstrlenW(_lpw)+1), \
(_convert>INT_MAX/2) ? NULL : \
ATLW2AHELPER((LPSTR) alloca(_convert*sizeof(WCHAR)), _lpw, _convert*sizeof(WCHAR), _acp))))