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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Thunder041398Commented:
Maybe you can create an array of pointers to the safearrays...
0
NorbertCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.