【发布时间】:2017-10-14 03:19:01
【问题描述】:
我需要从“BSTR”创建一个“SAFEARRAY 字节”以将其传递给函数。
我尝试了以下方法从 BSTR 创建 BYTE*。
有没有其他方法可以将 BSTR 转换为 BYTE* 而不会导致任何数据丢失?
BSTR bstr = SysAllocString(L"This is a basic string which encoded in UTF-16!!ɏ");
int len = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, NULL, 0, NULL, NULL);
BYTE *pByte = new BYTE[len];
if (len > 0)
{
/*
for(int i=0; i<len; i++)
{
pByte[i] = (BYTE) bstr[i]; //using this way may lead to lose data.
}
*/
WideCharToMultiByte(CP_UTF8, 0, bstr, -1, (LPSTR)&pByte[0], len, NULL, NULL);
//cout << "Byte Array: " << pByte << endl;
}
delete []pByte;
【问题讨论】:
-
第二个
WideCharToMultiByte调用有什么问题?一个完整的、具体的失败示例会很有帮助。 -
您的代码与
SAFEARRAY无关。BSTR是安全的。我猜您只是想将 UTF-16 转换为 UTF-8。错误的 Unicode 转换会导致数据丢失。你不需要BSTR。将SysFreeString放在SysAllocString之后进行清理