函數如下,使用的時候直接貼在.h文件中就可以了。
說明:
iMultiCharSize 默認為-1的時候,null terminated 被計算。
其他情況下需要將轉換后的結尾置零。
inline int WINAPI MB2WC(LPCSTR lpcszStr, int iMultiCharSize, LPWSTR lpwszStr, int iBufSize)


{
int iMinSize;
ASSERT(lpcszStr != NULL);
ASSERT(lpwszStr != NULL);

// -1: the string is assumed to be null terminated and the length is calculated automatically.
// If this value is zero, the function returns the required buffer size, in wide characters,
// and makes no use of the lpWideCharStr buffer.
//iMinSize = MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, -1, NULL, 0);
iMinSize = MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, iMultiCharSize, NULL, 0);

if(iBufSize < iMinSize)

{
return FALSE;
}

// Change the character string to the wide-character (Unicode) string.
MultiByteToWideChar(g_uConvCodePage, MB_PRECOMPOSED, lpcszStr, iMultiCharSize, lpwszStr, iMinSize);
//iMinSize = MultiByteToWideChar(g_uCodePage, MB_PRECOMPOSED, lpcszStr, -1, lpwszStr, iMinSize);
if(iMultiCharSize > 0) // fix 20090121 because when -1 ,the length is calculated

{
lpwszStr[iMinSize] = 0;
}

return iMinSize;
}
使用實例
// +++ test WC2MB
TCHAR szTest[10] = _T("Zhou");
char chBuffer[256];
WC2MB(szTest, 2, chBuffer, 256);
// +++ end test
嘿嘿。
有時間吧Strdup版本也弄好了。這樣 用起來就更方便了。