windows API练习(2)多字节编码转UNICODE编码

wstring MtbToWstr( const string& str, UINT uCodePage = CP_ACP ) { LPWSTR lpwszWide = NULL; DWORD cbszWide = 0U; DWORD dwRet = 0U; wstring wstrRet; /// calculate the require size cbszWide = MultiByteToWideChar( uCodePage, 0U, str.c_str(), -1, lpwszWide, 0 ); if ( 0U == cbszWide ) goto Exit0; /// allocate specify size lpwszWide = (LPWSTR)HeapAlloc( GetProcessHeap(), 0, cbszWide * sizeof( WCHAR ) ); if ( NULL == lpwszWide ) goto Exit0; /// start convert dwRet = MultiByteToWideChar( uCodePage, 0U, str.c_str(), -1, lpwszWide, cbszWide ); if ( 0 == dwRet ) goto Exit0; wstrRet = lpwszWide; Exit0: if ( NULL != lpwszWide ) HeapFree( GetProcessHeap(), 0, lpwszWide ); return wstrRet; }

你可能感兴趣的:(windows,api,null,string)