描述:
CComSafeArrayBound bound[2];
bound[0].SetLowerBound (0);
bound[0].SetCount (3);
bound[1].SetLowerBound (2);
bound[1].SetCount (4);
// (0,1,2) * (2,3,4,5)
CComSafeArray<BSTR> csaTemp(psa);
const LONG i[] = {0, 2};
const BSTR s = ::SysAllocString (L"first");
std::wcout << s << std::endl; // 正常输出啊
hr = csaTemp.MultiDimSetAt (i, s);
if (FAILED (hr)) // 测试,没有失败啊
{
printf ("Set 99 failed!\n");
}
// BSTR ss = ::SysAllocString (L"");;
BSTR ss;
hr = csaTemp.MultiDimGetAt (i1, ss);
if (FAILED (hr)) // 也没有失败啊
{
printf ("GET 99 failed!\n");
}
else
{
std::wcout << L"string:" << ss << std::endl; // 这里的问题想得头大了。
// 他输出是: string:
// ss 为空?一路成功到这里为空?
// 如果我换为 CComSafeArray<LONG>,同样的处理,能得到结果啊
// 看来我的错误是在BSTR的处理上。可是,就这两行程序,还有什么错啊?
}
解决方案1:
你的ss没有分配内存,注意BSTR的定义是:typedef LPWSTR BSTR;仅仅一个指针而已。
加上:
BSTR ss = ::SysAllocString (L" ");
即可。用完即得free哦。