char* 转换成 BSTR 中文乱码问题
constchar**devices=linphone_core_get_sound_devices(core);//外部函数调用此函数得到声卡设备,含中文LONGCnt...
const char** devices = linphone_core_get_sound_devices(core);//外部函数调用此函数得到声卡设备,含中文
LONG Cnt = -1;
while (devices[++Cnt] != NULL)
{
//logout devices[Cnt] 正常显示,无乱码
}
psa_soundcards = SafeArrayCreateVector(VT_VARIANT, 0, Cnt);
LONG index;
for (index = 0; index < Cnt; ++index)
{
VARIANT var;
VariantInit(&var);
V_VT(&var) = VT_BSTR;
var.bstrVal = _com_util::ConvertStringToBSTR(devices[index]);//转换成BTST
SafeArrayPutElement(psa_soundcards, &index, &var);
}
问题是:单独打印devices,能得到正常显示的字符串,自定义char* str= "中华人民共和国";然后转换成BTST在vb程序中也正常显示,但是devices转换成btst在vb程序中中便显示乱码,此为com组件中的代码,求大神指点 展开
LONG Cnt = -1;
while (devices[++Cnt] != NULL)
{
//logout devices[Cnt] 正常显示,无乱码
}
psa_soundcards = SafeArrayCreateVector(VT_VARIANT, 0, Cnt);
LONG index;
for (index = 0; index < Cnt; ++index)
{
VARIANT var;
VariantInit(&var);
V_VT(&var) = VT_BSTR;
var.bstrVal = _com_util::ConvertStringToBSTR(devices[index]);//转换成BTST
SafeArrayPutElement(psa_soundcards, &index, &var);
}
问题是:单独打印devices,能得到正常显示的字符串,自定义char* str= "中华人民共和国";然后转换成BTST在vb程序中也正常显示,但是devices转换成btst在vb程序中中便显示乱码,此为com组件中的代码,求大神指点 展开
展开全部
CComVariant var;
var = devices[index];
这样能行吗?
更多追问追答
追答
STDMETHODIMP CCharToBSTRTest::GetBSTRArray(SAFEARRAY** ppVal)
{
const char* devices[3] = { "中华人民共和国","美利坚合众国","日本国" };
SAFEARRAY* psa_soundcards = SafeArrayCreateVector(VT_VARIANT, 0, 3);
LONG index;
HRESULT hr = S_OK;
for(index = 0; index < 3; ++index)
{
CComVariant var = devices[index];
hr = SafeArrayPutElement(psa_soundcards, &index, &var);
if(FAILED(hr))
{
SafeArrayDestroy(psa_soundcards);
return hr;
}
}
*ppVal = psa_soundcards;
return hr;
}
我这里是这样写的
Imports ATLProject1Lib
Public Class Form1
Private c2bstr As ATLProject1Lib.CharToBSTRTest
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sa As Array
sa = c2bstr.GetBSTRArray()
Dim i As Object
For Each i In sa
ListBox1.Items.Add(i.ToString())
Next i
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
c2bstr = New ATLProject1Lib.CharToBSTRTest
End Sub
End Class
vb里是这样写的
运行结果:
没有乱码问题啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询