My function body is as below
STDMETHODIMP CMyCustomAddin::getArray(SAFEARRAY** pArray)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
try {
CComSafeArray<BSTR> sa(3);
// 'v' is a std::vector<std::wstring>
std::vector<std::string> v;
v.push_back("string1"); v.push_back("string2"); v.push_back("string3");
for (LONG i = 0; i < 3; i++)
{
CComBSTR bstr = ToBstr(v[i]);
HRESULT hr = sa.SetAt(i, bstr.Detach(), FALSE);
if (FAILED(hr))
{
AtlThrow(hr);
}
}
*pArray= sa.Detach();
}
catch (const CAtlException& e)
{
AfxMessageBox(_T("Exception"));
}
return S_OK;
}
And I called this function from another COM module as:
SAFEARRAY** arr;
pMyCUstomAddinObj->getArray(arr);
I am getting access violation exception at
*pArray= sa.Detach();
How to get the array and traverse it?
SAFEARRAY** arr;
pMyCUstomAddinObj->getArray(arr);
With this initialization pArray
points to garbage, thus *pArray = ...
is an error. Perhaps you meant to write:
SAFEARRAY* arr;
pMyCUstomAddinObj->getArray(&arr);