//This is a chunk of code from Biginning ATL Com programming From WROX
//It is a good book and I recommend it for beginners.
//I hope this helps

//The vector is declared in the header file as a vector that can hold CComBSTR objects.
std::vector<CComBSTR> m_vecColors;



//The following is in the cpp file
//Now load the vector with strings
HRESULT CColorObject::FinalConstruct()
{
m_vecColors.push_back(CComBSTR(L"Red"));
m_vecColors.push_back(CComBSTR(L"Orange"));
m_vecColors.push_back(CComBSTR(L"Yellow"));
m_vecColors.push_back(CComBSTR(L"Green"));
m_vecColors.push_back(CComBSTR(L"Blue"));
m_vecColors.push_back(CComBSTR(L"Indigo"));
m_vecColors.push_back(CComBSTR(L"Violet"));


//call this function from your client when you need to get the array of strings
STDMETHODIMP CColorObject::get_Colors(VARIANT *pVal)
{
// TODO: Add your implementation code here
VariantInit(pVal);
pVal->vt = VT_ARRAY | VT_BSTR;
SAFEARRAY* psa;
SAFEARRAYBOUND bounds = {m_vecColors.size(), 0};
psa = SafeArrayCreate(VT_BSTR, 1, &bounds);

BSTR* bstrArray;

SafeArrayAccessData(psa, (void**)&bstrArray);
std::vector<CComBSTR>::iterator it;
int i = 0;

for (it = m_vecColors.begin(); it != m_vecColors.end(); it++, i++)
{
bstrArray[i] = SysAllocString((*it).m_str);
}

SafeArrayUnaccessData(psa);
pVal->parray = psa;


ATLTRACE(_T("Returning SafeArray which has these members:\n"));
DumpVector();

return S_OK;
}


//call this function from your client when you need to send an array of strings
//to the com object

STDMETHODIMP CColorObject:ut_Colors(VARIANT newVal)
{
// TODO: Add your implementation code here
if((newVal.vt & VT_ARRAY) == 0)
return E_INVALIDARG; //Not an array

if((newVal.vt & VT_BSTR) == 0)
return E_INVALIDARG;

m_vecColors.clear();
SAFEARRAY* psa = newVal.parray;
BSTR* bstrArray;
SafeArrayAccessData(psa, (void**)&bstrArray);

for(int i =0; i < psa->rgsabound->cElements; i++)
{
m_vecColors.push_back(CComBSTR(bstrArray[i]));
}

SafeArrayUnaccessData(psa);

ATLTRACE(_T("Changing SafeArray to have these members:\n"));
DumpVector();

return S_OK;
}

//VB client code

Option Explicit

Private itf As COLORSLib.IColorObject
'add a color string to the server array
Private Sub cmdAddItem_Click()
If txtNewItem.Text = "" Then
Exit Sub
End If

' Read data into an array
ReDim ColorArray(lstColors.ListCount) As String
Dim x As Integer
For x = 0 To lstColors.ListCount - 1
ColorArray(x) = lstColors.List(x)
Next
ColorArray(lstColors.ListCount) = txtNewItem.Text
itf.Colors = ColorArray
cmdShowObj_Click

End Sub

'Get array from server
Private Sub cmdShowObj_Click()
Dim Size As Long
Dim ColorArray As Variant
ColorArray = itf.Colors
lstColors.Clear
Size = UBound(ColorArray) - LBound(ColorArray) + 1
lblMsg.Caption = "There are " & Str$(Size) _
& " elements in " & TypeName(ColorArray)
Dim x As Integer
For x = LBound(ColorArray) To UBound(ColorArray)
lstColors.AddItem ColorArray(x)
Next

End Sub

Private Sub Form_Load()
Set itf = New COLORSLib.ColorObject
End Sub