Solved

How to create an array of safearrays?

Posted on 1998-04-09
2
976 Views
Last Modified: 2013-11-20
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)?

0
Comment
Question by:tdietz
2 Comments
 

Expert Comment

by:Thunder041398
ID: 1317595
Maybe you can create an array of pointers to the safearrays...
0
 
LVL 3

Accepted Solution

by:
Norbert earned 100 total points
ID: 1317596
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now