How to create an array of safearrays?

I need to pass an array of 1d variants to an automation object.  Right now I am using SafeArrayCreateVector (VT_VARIANT, 0, 2) to create a 1d array and then calling my AddRow(safeArray*) method over and over.

What I want to do is allocate an array of arrays so I can just call my AddRow function once with an array of safearrays.  This may sound confusing so here is a simple example of my data (queried from a database)

Row1:   UserName    ID
Row2:   UserName    ID

Each time I call AddRow (currently), I am passing an array of variants (safearray) that has 2 VARIANTs (one for UserName and one for ID).  I want to be able to pass all the arrays (row 1 through row n) at once.  

Is this possible?  Also, is it possible to create this array dynamically (I am getting this data from a database, so I really don't know how big it is until it's all done reading)?

tdietzAsked:
Who is Participating?
 
NorbertConnect With a Mentor Commented:
SafeArrayCreateVector can only create 1D arrays.
you need a more dimensional array so you should use SafeArrayCreate
or if you are using VC++ 4.2 or higher the Class COleSafeArray
Using SafeArrayCreate :
build for example a function
SafeArray* CreateMySafeArray(DWORD NumberOfRows)
{
     SafeArray* Psa;
     SAFEARRRAYBOUND* rgsabound=
         new SAFEARRRAYBOUND[NumberOfRows];
     for(DWORD i=0;i<NumberOfRows;i++)
     {
           rgsabound[i].cElements=2;
           rgsabound[i].lbound=0;
     }
     Psa=SafeArrayCreate(VT_VARIANT, NumberOfRows
           , rgsabound);
    delete[] rgsabound;
    return Psa;
}
now the calling function has a pointer to the SafeArray
so the array accessing part of the caller could look like
....
 DWord NumberOfRows=GetRowCount();
 SafeArray* MyArray=CreateMySafeArray( NumberOfRows);
 If(!MyArray)
{
     Error !!!
}
else
{
   // lets fill the array
   Variant *VariantValue;
   for(DWORD i=0;i<NumberOfRows;i++)
   {
           SafeArrayAccessData(MyArray,i,(void**)&VariantValue);
           VariantValue[0].bstrVal=UserName[i].AllocSysString()
          //Assuming UserName is a array of CStrings
          VariantValue[0].vt=VT_BSTR;
          VariantValue[1].lVal=UserID[i];
          VariantValue[1].vt=VT_I4;
          SafeArrayUnaccessData(MyArray);
   }
  //now the array is filled and can delivered to the Automation
  //object as your previous created with SafeArrayCreateVector
}
Using COleSafeArray I have never used for n-Dimensional arrays
so I have no expirience for that
I hope that helps

Regards
   Norbert
PS: Greetings from germany
0
 
Thunder041398Commented:
Maybe you can create an array of pointers to the safearrays...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.