其他分享
首页 > 其他分享> > ANSI 字符串转换为 Unicode 字符串

ANSI 字符串转换为 Unicode 字符串

作者:互联网

// 将多字节 string 转换为 wchat_t 宽字节字符串
BOOL MyMultiByteToWideChar(LPCCH pMultiByteStr, LPWSTR *lpWideCharStr)
{
    BOOL fOk = FALSE;
    int nLenOfWideCharStr = 0;
    // Calculate the number of characters needed to hold
    // the wiide-character version of the string.
    nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, strlen(pMultiByteStr) + 1, NULL, 0);
  
    // Allocate memory form the process' default heap to
    // accommodate the size of the wide-character string.
    // Don't forget that MultiByteToWideChar return the number of bytes, so
    // you must multiply by the size of a wide character.
    *lpWideCharStr = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfWideCharStr * sizeof(wchar_t));
    if (nLenOfWideCharStr == NULL)
        return fOk;
    // Convert the multibyte string to a wide-charater string.
    if (!MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, strlen(pMultiByteStr) + 1, *lpWideCharStr, nLenOfWideCharStr))
    	return fOk;
    return TRUE;
}

标签:lpWideCharStr,return,string,character,pMultiByteStr,ANSI,Unicode,字符串,nLenOfWideC
来源: https://www.cnblogs.com/funsang/p/16310648.html